Created
November 5, 2016 17:28
-
-
Save vincent1086/f9e0feb3035d77699e6f67bddd7dcc39 to your computer and use it in GitHub Desktop.
Check Touch outside target view
This file contains 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
// Reference : http://stackoverflow.com/questions/6410200/android-detect-if-user-touches-and-drags-out-of-button-region | |
private Rect rect; // Variable rect to hold the bounds of the view | |
public boolean onTouch(View v, MotionEvent event) { | |
if(event.getAction() == MotionEvent.ACTION_DOWN){ | |
// Construct a rect of the view's bounds | |
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); | |
} | |
if(event.getAction() == MotionEvent.ACTION_MOVE){ | |
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){ | |
// User moved outside bounds | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment