37

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

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

PeyloW
  • 36,742
  • 12
  • 80
  • 99

4 Answers4

70

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.

Dimitar Dimitrov
  • 16,032
  • 5
  • 53
  • 55
  • 7
    I have solved my problem by using said getRawX() and getRawY(). I do however find it to be a depressing solution as it suggest that getX() and getY() are useless, even though it feels like they should be the primate methods for the MotionEvent class. There MUST be a more elegant solution. – PeyloW Sep 12 '09 at 20:58
  • 1
    Excellent, you saved me a future headache. – Tom Jun 08 '11 at 12:25
  • Peylow - motionEvent.getRawX() only returns absolute coords inside the view. It will NOT return coords outside the View. To get coords outside of the view one has to add the _absolute_ coordinates of an ACTION_DOWN event to the _relative_ coordinates of the corresponding ACTION_MOVE or ACTION_UP MotionEvents – Ian Aug 31 '17 at 19:38
37

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.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
  • 3
    Is this still the case? I'm finding getX an getY to work just fine for relative coordinates... – jjxtra Jun 27 '15 at 21:51
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.

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.

dyerjo
  • 86
  • 2
0

I found a way to not need the raw coordinates.

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

It's correct. But in general we want coordinates relative to container View, not dispatched view.

float xRelativeToParent = view.getX() + motionEvent.getX();
float yRelativeToParent = view.getY() + motionEvent.getY();

Then, to move the center of the touched view to a moving pointer position, do:

view.setX(xRelativeToParent - view.getWidth()/2f);
view.setY(yRelativeToParent - view.getHeight()/2f);