通过处理一个 View 项的触摸事件,我在从屏幕上获取真实的 (x,y) 时遇到了一些麻烦。
我有这个:
当我触摸 View 项目时,它会给我坐标,原点位于他的左上角,如果我将手指滑出 View ,它会给我负坐标(向上或向左滑动时)。
我想获取View在屏幕中的位置,把它加到我得到的(x,y)坐标上,所以最后的结果是(0,0)在屏幕的左上角整个屏幕。
我还需要总屏幕大小,以便我知道手指何时位于右下角。
我怎样才能做到这一点?
谢谢。
最佳答案
MotionEvent 类有 getX()
和 getY()
方法,正如您所发现的,它们返回相对于 View 的坐标。
但是它也有 getRawX()
和 getRawY()
方法,根据文档说:
Returns the original raw X (or Y) coordinate of this event. For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.
听起来这就是您想要的。
关于android - 如何获取对应于整个屏幕的屏幕坐标,而不是触摸的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832333/