android - 如何获取对应于整个屏幕的屏幕坐标,而不是触摸的 View ?

标签 android screen coordinates android-view

通过处理一个 View 项的触摸事件,我在从屏幕上获取真实的 (x,y) 时遇到了一些麻烦。

我有这个:

enter image description here

当我触摸 View 项目时,它会给我坐标,原点位于他的左上角,如果我将手指滑出 View ,它会给我负坐标(向上或向左滑动时)。 enter image description here enter image description here

我想获取View在屏幕中的位置,把它加到我得到的(x,y)坐标上,所以最后的结果是(0,0)在屏幕的左上角整个屏幕。 enter image description here

我还需要总屏幕大小,以便我知道手指何时位于右下角。

我怎样才能做到这一点?

谢谢。

最佳答案

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/

相关文章:

wpf - 将 Popup 绑定(bind)到另一个控件的相对屏幕位置

java - 凸性缺陷坐标 java android

gps - 如何计算两个地理/GPS 坐标之间的角度?

java - Android 中的 CSV 文件无法写入?

android - 如何获取电子邮件 ID 推特集成

android - 使用 GWT 的 Android 应用架构

vue.js - 使用 vuejs 设置响应式(Reactive)屏幕宽度

android - 以编程方式打开屏幕

performance - 关于在 Postgresql 中存储 Lat/Lng 坐标(列类型)

Androidx Constraintlayout 对 Android Studio 没有响应