How to get View from X/Y coordinates
Is there a method that will take an absolute (X, Y) screen coordi开发者_高级运维nate and return the View it corresponds to? Basically, "if the user touched here, which View would receive the event?"
I'm writing a game that involves a gesture that allows the player to draw a path through many different widgets, affecting them as they go. However, the MotionEvents only report the initial View that was touched, along with X/Y coordinates of where the pointer is now. I'd like an easy way to retrieve the View they're currently hovering over, without having to loop through all Views to manually check their bounding box against the coordinates.
Have you considered trying it the other way around. Such as having the views report the touch events?
You can have the views process the event, but return false
from
onTouchEvent (MotionEvent event)
This will let the framework pass the event along to any other view that might want to process it.
http://developer.android.com/reference/android/view/View.html#onTouchEvent%28android.view.MotionEvent%29
精彩评论