解决方案 »

  1.   

    将多个button放到layout里,实现layout的移动
      

  2.   

    声明一个List<View> 集合,然后findViewById()以后把需要移动的控件放进去,然后在你拖动时候遍历就可以了
      

  3.   

    public class MainActivity extends Activity {
    private ImageView i;
    private int width,height,defaultx,defaulty;
    private float mx,my;
    private int intscreenx,intscreeny;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics dm=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    intscreenx=dm.widthPixels;
    intscreeny=dm.heightPixels;
    width=100;
    height=100;
    i=(ImageView) findViewById(R.id.imageView1);
    i.setImageResource(R.drawable.ic_launcher);
    RestoreButton();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
    float x=event.getX();
    float y=event.getY();
    try{
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
    picMove(x,y);
    break;
    case MotionEvent.ACTION_MOVE:
    picMove(x,y);
    break;
    case MotionEvent.ACTION_UP:
    picMove(x,y);
    break;
    }
    }
    catch(Exception e){
    e.printStackTrace();
    }
    return true;

    }

    private void picMove(float x,float y){
    mx=x-(width/2);
    my=y-(height/2);
    if((mx+width)>intscreenx){
    mx=intscreenx-width;
    }
    else if(mx<0){
    mx=0;
    }
    else if((my+height)>intscreeny){
    my=intscreeny-height;
    }
    else if(my<0){
    my=0;
    }
    i.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,(int)(mx),(int)(my)));
    } public void RestoreButton(){
    defaultx=((intscreenx-width)/2);
    defaulty=((intscreeny-height)/2);
    i.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,defaultx,defaulty));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }}
    不要说是我告诉你的,记得给分就行了,,,