La même chose que @ Alex Karshin Je change un peu de réponse.
public class MovingObject implements OnTouchListener {
private RelativeLayout.LayoutParams lParams;
private PointF viewPoint, prePoint, currPoint;
public MovingObject() {
lParams = null;
viewPoint = new PointF();
prePoint = new PointF();
currPoint = new PointF();
}
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
viewPoint.set(view.getX(), view.getY());
prePoint.set(event.getRawX(), event.getRawY());
lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
currPoint.set(event.getRawX(), event.getRawY());
moveToCurrentPoint(view);
break;
}
view.invalidate();
return true;
}
private void moveToCurrentPoint(View view) {
float dx = currPoint.x - prePoint.x - prePoint.x + viewPoint.x;
float dy = currPoint.y - prePoint.y - prePoint.y + viewPoint.y;
lParams.leftMargin = (int) (prePoint.x + dx);
lParams.topMargin = (int) (prePoint.y + dy);
view.setLayoutParams(lParams);
}
}
2 votes
Voici un exemple thegeekyland.blogspot.com/2015/12/