Reputation: 6532
Is it possible to create a method that receive an event and a view and determines whether the event happened in within the view coordinates?
Something along the lines of:
private boolean isEventOnTopOfView(MotionEvent event, View view) {...}
Upvotes: 3
Views: 1086
Reputation: 1468
Slightly simpler implementation in Kotlin:
private fun isEventOnTopOfView(event: MotionEvent, view: View): Boolean {
return Rect().apply(view::getGlobalVisibleRect)
.contains(event.rawX.toInt(), event.rawY.toInt())
}
Upvotes: 2
Reputation: 6532
This implementation seems to work for me:
private boolean isEventOnTopOfView(MotionEvent event, View view) {
return isViewContains(view,(int)event.getRawX(),(int)event.getRawY());
}
private boolean isViewContains(View view, int rx, int ry) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
int w = view.getWidth();
int h = view.getHeight();
if (rx < x || rx > x + w || ry < y || ry > y + h) {
return false;
}
return true;
}
Upvotes: 2