触摸监听器不起作用。我需要将图像拖到屏幕上并放到上一个位置本身。
为此,我编写如下代码:
class Mylistener implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
layoutParams2 = (RelativeLayout.LayoutParams) v.getLayoutParams();
//int left=0,top=0;
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
startx=(int) event.getRawX();
starty=(int) event.getRawY();
System.out.println("ACTION DOWN: "+startx+","+starty);
break;
case MotionEvent.ACTION_MOVE:
int left = (int) event.getRawX() - (v.getWidth() / 2);
int top = (int) event.getRawY() - (v.getHeight());
marginParams.setMargins(left, top, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_UP:
marginParams.setMargins(startx, starty, 0, 0);
layoutParams2 = new RelativeLayout.LayoutParams(marginParams);
System.out.println("ACTION UP: "+startx+","+starty);
v.setLayoutParams(layoutParams2);
default:
break;
}
return true;
}
}
但它在屏幕上拖动而不是放在以前的位置。请给我任何帮助