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

前端 未结 3 1895
夕颜
夕颜 2020-12-14 00:49

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

相关标签:
3条回答
  • 2020-12-14 01:18

    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.

    The solution is discussed here:

    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.

    0 讨论(0)
  • 2020-12-14 01:38

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

    MotionEvent.getRawX()

    MotionEvent.getRawY()

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

    0 讨论(0)
  • 2020-12-14 01:40

    This is a limitation on the Android platform.

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题