避免在Android中合并ACTION_MOVE MotionEvents

时间:2012-10-06 11:42:13

标签: java android motionevent

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

我在屏幕上滑动手指的测试表明,在初始ACTION_DOWN之后,几个MotionEvent未传递给听众。只有在进行了足够大的移动之后,才会发送具有相对较大增量的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上进行了测试。

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

2 个答案:

答案 0 :(得分:3)

我讨厌打破它,但这完全是三星的错,只会发生在Galaxy S2上。基本上他们在输入驱动程序中添加了软件触摸阈值,没有任何实际原因。

我曾试图说服他们取消门槛,但他们似乎并不理解这个问题,并告诉我那是故意!尽管如此,他们已经将它固定在Galaxy S3中。

幸运的是,这不是硬件限制,所以如果你有一个带根电话,或者安装一个自定义ROM,你可以绕过它。

答案 1 :(得分:1)

我在某些应用程序中使用MotionEvent,necer遇到了这个问题。

如果您使用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”),以便进行确认。

祝你好运。