我怎么知道MotionEvent是相对的还是绝对的?

时间:2023-01-30 16:27:34

I am implementing OnTouchListener and am receiving MotionEvent objects. Some ACTION_MOVE events reports absolute X/Y coordinates, while some reports relative coordinates.

我正在实现OnTouchListener并且正在接收MotionEvent对象。一些ACTION_MOVE事件报告绝对X / Y坐标,而一些报告相对坐标。

How can I ask a MotionEvent what kind of coordinates it currently represents?

我如何询问MotionEvent它目前代表什么样的坐标?

3 个解决方案

#1


62  

You may want to use these for absolute coordinates (absolute, regarding the screen of the device):

您可能希望将这些用于绝对坐标(绝对值,关于设备的屏幕):

MotionEvent.getRawX()

MotionEvent.getRawX()

MotionEvent.getRawY()

MotionEvent.getRawY()

The other methods, getX() and getY(), should return you coordinates relative to the View that dispatched them.

其他方法getX()和getY()应返回相对于调度它们的View的坐标。

#2


35  

This is a limitation on the Android platform.

这是对Android平台的限制。

MotionEvent will sometimes return absolute X and Y coordinates relative to the view, and sometimes relative coordinates to the previous motion event.

MotionEvent有时会返回相对于视图的绝对X和Y坐标,有时还会返回前一个运动事件的相对坐标。

An event sent as ACTION_DOWN will always be absolute, all other events will vary. There is no way to ask the MotionEvent for the current type of coordinates.

作为ACTION_DOWN发送的事件将始终是绝对的,所有其他事件将变化。无法向MotionEvent询问当前的坐标类型。

This means that in practice getX() and getY() are useless for many use cases, and you should base your application logic on getRawX() and getRawY() that is guaranteed to return absolute coordinates, relative to the device screen.

这意味着实际上getX()和getY()在许多用例中都是无用的,你应该将你的应用程序逻辑基于getRawX()和getRawY(),保证相对于设备屏幕返回绝对坐标。

#3


4  

When using the MapView, I was able to get the relative X and Y coordinates by subtracting the View.getLeft() and View.getTop() of the Window's content view (Window.ID_ANDROID_CONTENT) from the MotionEvent.getRawX() and MotionEvent.getRawY(), respectively.

使用MapView时,我能够通过从MotionEvent.getRawX()和MotionEvent中减去Window的内容视图(Window.ID_ANDROID_CONTENT)的View.getLeft()和View.getTop()来获取相对的X和Y坐标。 getRawY(),分别。

The solution is discussed here:

这里讨论解决方案:

http://andmobidev.blogspot.com/2010/01/getting-relative-coordinates-from.html

http://andmobidev.blogspot.com/2010/01/getting-relative-coordinates-from.html

This should work for determining relative X and Y coordinates in the main layout view.

这应该用于确定主布局视图中的相对X和Y坐标。

#1


62  

You may want to use these for absolute coordinates (absolute, regarding the screen of the device):

您可能希望将这些用于绝对坐标(绝对值,关于设备的屏幕):

MotionEvent.getRawX()

MotionEvent.getRawX()

MotionEvent.getRawY()

MotionEvent.getRawY()

The other methods, getX() and getY(), should return you coordinates relative to the View that dispatched them.

其他方法getX()和getY()应返回相对于调度它们的View的坐标。

#2


35  

This is a limitation on the Android platform.

这是对Android平台的限制。

MotionEvent will sometimes return absolute X and Y coordinates relative to the view, and sometimes relative coordinates to the previous motion event.

MotionEvent有时会返回相对于视图的绝对X和Y坐标,有时还会返回前一个运动事件的相对坐标。

An event sent as ACTION_DOWN will always be absolute, all other events will vary. There is no way to ask the MotionEvent for the current type of coordinates.

作为ACTION_DOWN发送的事件将始终是绝对的,所有其他事件将变化。无法向MotionEvent询问当前的坐标类型。

This means that in practice getX() and getY() are useless for many use cases, and you should base your application logic on getRawX() and getRawY() that is guaranteed to return absolute coordinates, relative to the device screen.

这意味着实际上getX()和getY()在许多用例中都是无用的,你应该将你的应用程序逻辑基于getRawX()和getRawY(),保证相对于设备屏幕返回绝对坐标。

#3


4  

When using the MapView, I was able to get the relative X and Y coordinates by subtracting the View.getLeft() and View.getTop() of the Window's content view (Window.ID_ANDROID_CONTENT) from the MotionEvent.getRawX() and MotionEvent.getRawY(), respectively.

使用MapView时,我能够通过从MotionEvent.getRawX()和MotionEvent中减去Window的内容视图(Window.ID_ANDROID_CONTENT)的View.getLeft()和View.getTop()来获取相对的X和Y坐标。 getRawY(),分别。

The solution is discussed here:

这里讨论解决方案:

http://andmobidev.blogspot.com/2010/01/getting-relative-coordinates-from.html

http://andmobidev.blogspot.com/2010/01/getting-relative-coordinates-from.html

This should work for determining relative X and Y coordinates in the main layout view.

这应该用于确定主布局视图中的相对X和Y坐标。