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
How do I know if a MotionEvent is relative or absolute?
提问by PeyloW
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
它目前代表什么样的坐标?
回答by Dimitar Dimitrov
You may want to use these for absolute coordinates (absolute, regarding the screen of the device):
您可能希望将这些用于绝对坐标(绝对,关于设备的屏幕):
The other methods, getX()
and getY()
, should return you coordinates relative to the View
that dispatched them.
其他方法getX()
和getY()
,应该返回相对于View
调度它们的坐标。
回答by PeyloW
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()
是保证返回绝对坐标,相对于设备屏幕。
回答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 坐标。