本文介绍了Android版的getX /交错的getY相对/绝对坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多的讨论,MotionEvent.getX / .getY是如何不可靠(或其他方面),我们应该使用这些电话的原始版本,以获得坐标。

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.

在我的Nexus 7,我发现.getX / .getY可靠地返回交错的绝对和相对坐标。换句话说,说当你调用.getX和.getY给定ACTION_MOVE事件返回绝对坐标。那么接下来ACTION_MOVE活动将在其.getX和.getY调用返回相对坐标。

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.

这不可能是偶然的行为。这也使我相信一定有辨别一个给定的ACTION_MOVE是否将返回绝对或相对坐标的方式。

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.

有谁知道如何检查一个给定的MotionEvent对象,以确定它返回绝对和它.getX和相对坐标.getY电话?

编辑:按照您的要求,这里的code。没什么特别的,只是抢坐标和移动View对象:

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);

}

该Log.d电话和独立的花车是没有必要使code的工作,但他们可以让你看到价值的交织在LogCat中的窗口。

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.

推荐答案

我已经找到了,这对GALAXY S 4和的getY都getRawY都是错误的。但是,他们在正交的方式改变。所以,你可以通过下面的code得到正确的值:

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.

这篇关于Android版的getX /交错的getY相对/绝对坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 03:19