There is a lot of discussion about how MotionEvent.getX / .getY are "unreliable" (or other terms) and that we must use the Raw versions of these calls to get the coordinates.
On my Nexus 7, I found that .getX / .getY reliably return interleaved absolute and relative coordinates. In other words, let's say that this ACTION_MOVE event returns the absolute coordinates when .getX and .getY are called. The next ACTION_MOVE event will then return relative coordinates on .getX and .getY calls.
This cannot be random behavior. It also makes me think there should be a way to determine if a given ACTION_MOVE will return absolute or relative coordinates.
Does anyone know how to check a given MotionEvent object to see if it returns absolute and relative coordinates for its .getX and .getY calls?
EDIT: Upon your request here is the code. This is nothing special, just take the coordinates and move the View object:
public boolean onTouch(View v,MotionEvent event) { boolean bExitValue = true; float fX; float fY; int iAction; iAction = event.getActionMasked(); if (MotionEvent.ACTION_MOVE == iAction) { fX = event.getX(); fY = event.getY(); v.setX(fX); v.setY(fY); Log.d("",("X: " + fX + ", Y: " + fY)); } else if (MotionEvent.ACTION_DOWN != iAction) { bExitValue = false; } return(bExitValue); }
The Log.d call and stand-alone floats are not needed to make the code work, but they allow you to see the alternation of values ββin the LogCat window.
source share