简体   繁体   中英

Android getX/getY interleaves relative/absolute coordinates

There are a lot of discussions of how MotionEvent.getX/.getY are "unreliable" (or other terms) and that we should use the Raw versions of these calls to get coordinates.

On my Nexus 7, I have discovered that .getX/.getY are reliably returning interleaved absolute and relative coordinates. In other words, say a given ACTION_MOVE event returns absolute coordinates when you call .getX and .getY. The next ACTION_MOVE event will then return relative coordinates on its .getX and .getY calls.

This cannot be accidental behavior. It also leads me to believe there must be a way to discern whether a given ACTION_MOVE will be returning absolute or relative coordinates.

Does anyone know how to check a given MotionEvent object to see if it is returning absolute vs. relative coordinates on its .getX and .getY calls?

EDIT: Per your request, here's the code. It's nothing special, just grab 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 standalone floats aren't necessary to make the code work, but they do allow you to see the interleaving of values in the LogCat window.

I have found out, that on the galaxy s 4 getY and getRawY both are wrong. But they change in an orthogonal way. So you can get the right value by the following code:

rawY = event.getRawY() - spaceOverLayout;
normalY = event.getY();
y = 0F;

float prozentPosition = ((rawY + normalY) / 2) / height;
y = (normalY * (1 - prozentPosition)) + (rawY * prozentPosition);

hopefully it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM