简体   繁体   中英

How to get View from X/Y coordinates

Is there a method that will take an absolute (X, Y) screen coordinate 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM