Skip to content

Instantly share code, notes, and snippets.

@vincent1086
Created November 5, 2016 17:28
Show Gist options
  • Save vincent1086/f9e0feb3035d77699e6f67bddd7dcc39 to your computer and use it in GitHub Desktop.
Save vincent1086/f9e0feb3035d77699e6f67bddd7dcc39 to your computer and use it in GitHub Desktop.
Check Touch outside target view
// 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