If you want to implement a drag gesture, you can use onTouchListener and set a padding in your MotionEvent.ACTION_MOVE using setPadding for this view.
Here is a sample code:
case MotionEvent.ACTION_MOVE: yourView.setPadding((int)event.getRawX(), (int)event.getRawY(), 0, 0);
Example for onTouchListener: -
OnTouchListener drag= new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction() & MotionEvent.ACTION_MASK){ case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_MOVE: img.setPadding((int)event.getRawX(), (int)event.getRawY(), 0, 0); break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_POINTER_UP: break; } return true; } };
You need to create a drag and drop image at runtime and adjust its position. In this code example, I did not adjust the position, so the dragged image is not created at the touch position. You also need to consider the height and width of the image to be dragged.
Hope this helps!
source share