Created
September 9, 2015 22:03
-
-
Save mattshapiro/e148000d977559d88dd2 to your computer and use it in GitHub Desktop.
Stupid, simple, Android view dragging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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