버튼이 하나 있고 그 버튼을 드래그 하면 마우스를 따라서 이동하게 하고 싶습니다.

 

간단하게 짜 보았는데 막상 해보니 위치가 제멋대로 이동합니다.

 

답변 부탁드리겠습니다.

 

public class ZzzzzActivity extends Activity {
    /** Called when the activity is first created. */
 Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button) findViewById(R.id.button1);
        btn.setOnTouchListener(new MyListener());

       
    }
   
    boolean ismoved=false;
    class MyListener implements OnTouchListener,OnLongClickListener{  
  public boolean onLongClick(View arg0) {
   // TODO Auto-generated method stub
   
   ismoved=true;
   return false;
  }

  public boolean onTouch(View v, MotionEvent event) {
   // TODO Auto-generated method stub
   switch(event.getAction()){
   case MotionEvent.ACTION_DOWN:
    ismoved=true;
    break;
   case MotionEvent.ACTION_MOVE:
    if(ismoved==true){
     int x=(int) event.getX();
     int y=(int) event.getY();
     AbsoluteLayout.LayoutParams p=new AbsoluteLayout.LayoutParams(btn.getWidth(),btn.getHeight(),x,y);
     btn.setLayoutParams(p);
    }
    break;
   case MotionEvent.ACTION_UP:
    ismoved=false;
    break;
   }
   
   return false;
  }
  
 }
}