简体   繁体   English

如何从X / Y坐标获取视图

[英]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? 有没有一种方法可以获取屏幕的绝对(X,Y)坐标并返回其对应的视图? Basically, "if the user touched here, which View would receive the event?" 基本上,“如果用户触摸此处,哪个View将接收事件?”

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. 但是,MotionEvent仅报告所触摸的初始View,以及指针现在所在位置的X / Y坐标。 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. 我想要一种简单的方法来检索它们当前悬停的View,而不必遍历所有View来根据坐标手动检查其边界框。

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 您可以让视图处理事件,但从返回false

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 http://developer.android.com/reference/android/view/View.html#onTouchEvent%28android.view.MotionEvent%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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