Android getX / getY alternates relative / absolute coordinates

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.

+3
source share
1 answer

I found out that on galaxy s4 getY and getRawY are both wrong. But they change orthogonally. Thus, you can get the correct value using the following code:

 rawY = event.getRawY() - spaceOverLayout; normalY = event.getY(); y = 0F; float prozentPosition = ((rawY + normalY) / 2) / height; y = (normalY * (1 - prozentPosition)) + (rawY * prozentPosition); 

hope this helps.

0
source

Source: https://habr.com/ru/post/948958/


All Articles