Skip to content

Instantly share code, notes, and snippets.

@mattshapiro
Created September 9, 2015 22:03
Show Gist options
  • Save mattshapiro/e148000d977559d88dd2 to your computer and use it in GitHub Desktop.
Save mattshapiro/e148000d977559d88dd2 to your computer and use it in GitHub Desktop.
Stupid, simple, Android view dragging
public class SelectionHandleListener implements View.OnTouchListener {
float x, y;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float rx = motionEvent.getRawX();
float ry = motionEvent.getRawY();
switch(motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
x = rx;
y = ry;
break;
}
float dx = rx - x;
float dy = ry - y;
view.setX(view.getX() + dx);
view.setY(view.getY() + dy);
x = rx;
y = ry;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment