Android 我如何知道 MotionEvent 是相对的还是绝对的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1410885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:03:40  来源:igfitidea点击:

How do I know if a MotionEvent is relative or absolute?

androidtouch

提问by PeyloW

I am implementing OnTouchListenerand am receiving MotionEvent objects. Some ACTION_MOVEevents reports absolute X/Y coordinates, while some reports relative coordinates.

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

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

我怎么能问MotionEvent它目前代表什么样的坐标?

回答by Dimitar Dimitrov

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 Viewthat dispatched them.

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

回答by PeyloW

This is a limitation on the Android platform.

这是 Android 平台上的限制。

MotionEventwill 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_DOWNwill always be absolute, all other events will vary. There is no way to ask the MotionEventfor 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()是保证返回绝对坐标,相对于设备屏幕。

回答by dyerjo

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.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 坐标。