2

我试图准确地从AndroidACTION_MOVE中的 s 序列中读取增量。MotionEvent

然而,我用手指在屏幕上滑动的测试表明,在最初的 s 之后ACTION_DOWN,几个MotionEvents 没有传递给听众。只有在进行了足够大的移动之后,ACTION_MOVE才会以相对较大的增量传递事件。在这第一个ACTION_MOVE事件之后,所有增量都再次变得非常小。

或者,也可以触摸屏幕 ( ACTION_DOWN) 并稍微移动一下,根本不会ACTION_MOVE传递任何事件。

我知道对于许多用途,具有一定的移动(内)敏感性是有益的。但是,有没有办法避免ACTION_MOVE事件合并,使得每一个小的初始动作都被传递?ACTION_DOWN是否可以在位置和第一个位置之间的最小距离上设置阈值ACTION_MOVE

这是一些示例代码:

private float lastX;
private float lastY;

public boolean onTouch(View v, MotionEvent ev) {

    final float y = ev.getX();
    final float x = ev.getY();

    final int hs = ev.getHistorySize();

    final int ac = ev.getAction();
    switch(ac)
    {
        case MotionEvent.ACTION_DOWN:
        {
            this.lastX = x;
            this.lastY = y;
            Log.i("TEST", "Down at " + x + "/" + y + " (History size="+hs+")");
            return true;
        }
        case MotionEvent.ACTION_MOVE:
        {
            Log.i("TEST", "Move at " + x + "/" + y + ": Delta="+(lastX-x)+"/"+(lastY-y) + " (History size="+hs+")");
            this.lastX = x;
            this.lastY = y;
            return true;
        }
        case MotionEvent.ACTION_UP:
        {
            Log.i("TEST", "Up at " + x + "/" + y + " (History size="+hs+")");
            return true;
        }
    } 
}

以及相应的输出:

Down at 376.0/259.0 (History size=0)
Move at 392.0/268.0: Delta=-16.0/-9.0 (History size=0)
Move at 394.0/268.0: Delta=-2.0/0.0 (History size=0)
Move at 395.0/269.0: Delta=-1.0/-1.0 (History size=0)
Move at 394.0/268.0: Delta=1.0/1.0 (History size=0)
Move at 396.0/269.0: Delta=-2.0/-1.0 (History size=0)
Move at 399.0/271.0: Delta=-3.0/-2.0 (History size=1)
Move at 401.0/272.0: Delta=-2.0/-1.0 (History size=0)
Move at 403.0/273.0: Delta=-2.0/-1.0 (History size=0)
Move at 404.0/273.0: Delta=-1.0/0.0 (History size=0)
Move at 405.0/274.0: Delta=-1.0/-1.0 (History size=0)
Move at 406.0/275.0: Delta=-1.0/-1.0 (History size=0)
Up at 406.0/275.0 (History size=0)

换句话说,我想避免第二行(-16/-9)的大增量,但接收 1 或 2 的增量...

我在三星 Galaxy S2 和 Note 上进行了测试。

任何帮助或建议都非常感谢!

4

2 回答 2

4

我不想告诉你,但这完全是三星的错,只会发生在 Galaxy S2 上。基本上,他们无缘无故地在输入驱动程序中添加了软件触摸阈值。

我曾经试图说服他们取消门槛,但他们似乎没有理解问题并告诉我这是故意的!尽管如此,他们还是在 Galaxy S3 中修复了它。

幸运的是,这不是硬件限制,因此如果您有一部有根手机或安装自定义 ROM,您可以绕过它。

于 2012-10-16T10:30:16.077 回答
1

我在某些应用程序中使用 MotionEvent 并没有遇到这个问题。

如果您使用的 SDK 版本 > 4,则 MotionEvent 支持多点触控。我正在我的应用程序上对此进行测试,并使用修改后的侦听器来获取触摸事件。代码如下:

private static PointF touchScreenStartPtArr[] = new PointF[10]; 
private static PointF touchScreenStopPtArr[] = new PointF[10]; 
private static PointF touchScreenCurrPtArr[] = new PointF[10]; 

OnTouchListener onTouchListenerMulti = new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

        int action = event.getAction() & MotionEvent.ACTION_MASK; 
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
        int fingerId = event.getPointerId(pointerIndex); 

        switch (action) { 
        case MotionEvent.ACTION_DOWN: 
        case MotionEvent.ACTION_POINTER_DOWN: 
            touchScreenStartPtArr[fingerId].x = event.getX(pointerIndex); 
            touchScreenStartPtArr[fingerId].y = event.getY(pointerIndex); 
            break; 
        case MotionEvent.ACTION_UP: 
        case MotionEvent.ACTION_POINTER_UP: 
        case MotionEvent.ACTION_CANCEL: 
            touchScreenStopPtArr[fingerId].x = event.getX(pointerIndex); 
            touchScreenStopPtArr[fingerId].y  = event.getX(pointerIndex); 
            break; 
        case MotionEvent.ACTION_MOVE: 
            int pointerCount = event.getPointerCount(); 
            for (int i = 0; i < pointerCount; i++) { 
                touchScreenCurrPtArr[fingerId].x = event.getX(i); 
                touchScreenCurrPtArr[fingerId].y = event.getY(i); 
            } 
            break; 
        } 
        return true; 
    } 
}; 

- 编辑 -

在华为 X5 设备上运行代码的结果:

Down at 387.29388/247.27272 (History size=0)
Move at 387.29388/247.27272: Delta=0.0/0.0 (History size=0)
Move at 387.29388/247.27272: Delta=0.0/0.0 (History size=0)
Move at 387.29388/245.39589: Delta=0.0/1.876831 (History size=0)
Move at 387.29388/244.92668: Delta=0.0/0.46920776 (History size=0)
Move at 387.29388/244.45747: Delta=0.0/0.46920776 (History size=0)
Move at 387.29388/243.98827: Delta=0.0/0.46920776 (History size=0)
Move at 387.29388/243.51906: Delta=0.0/0.46920776 (History size=0)
Move at 389.01614/243.51906: Delta=-1.7222595/0.0 (History size=0)
Move at 389.8773/243.51906: Delta=-0.861145/0.0 (History size=0)
Move at 389.8773/243.04985: Delta=0.0/0.46920776 (History size=0)
Move at 390.73843/243.04985: Delta=-0.861145/0.0 (History size=0)
Move at 390.73843/242.58064: Delta=0.0/0.46920776 (History size=0)
Move at 391.59958/242.58064: Delta=-0.861145/0.0 (History size=0)
Move at 391.59958/242.11143: Delta=0.0/0.46920776 (History size=0)
Move at 392.46072/242.11143: Delta=-0.861145/0.0 (History size=0)
Move at 392.46072/241.64223: Delta=0.0/0.46920776 (History size=0)
Move at 393.32187/241.64223: Delta=-0.861145/0.0 (History size=0)
Move at 394.183/241.64223: Delta=-0.861145/0.0 (History size=0)
Move at 394.183/241.17302: Delta=0.0/0.46920776 (History size=0)
Move at 395.04413/241.17302: Delta=-0.8611145/0.0 (History size=0)
Move at 395.04413/242.11143: Delta=0.0/-0.9384155 (History size=0)
Move at 395.04413/242.58064: Delta=0.0/-0.46920776 (History size=0)
Up at 395.04413/242.58064 (History size=0)

看起来您可能有另一个侦听器从这个侦听器窃取事件。

我建议您在清洁项目中检查您的代码(您需要在最后添加“return false”),以确认它。

祝你好运。

于 2012-10-06T15:05:06.707 回答