繁体   English   中英

了解Android上的多点触控

[英]Understanding multi-touch on android

我正在尝试跟踪Android上的多点触摸事件(我需要跟踪每个手指的“触摸向下”和“触摸向上”)。 问题是PointerId似乎不一致,或者我在将“向下”事件链接到“向上”事件时遇到问题,结果我错过了一些“向上”事件。

这是我用来处理MotionEvents的代码:

int pointerIndex = me.getActionIndex();
int pointerId = me.getPointerId(pointerIndex);
final int action = me.getActionMasked();

switch (action)
{
case MotionEvent.ACTION_POINTER_DOWN:
{
    if (action == MotionEvent.ACTION_POINTER_DOWN)
    {
        System.out.println("ACTION_POINTER_DOWN Index: " + pointerIndex + ", ID: " + pointerId);
    }
}
case MotionEvent.ACTION_DOWN:
{
    if (action == MotionEvent.ACTION_DOWN)
    {
        System.out.println("Initial ACTION_DOWN Index: " + pointerIndex + ", ID: " + pointerId);
        pointerIndex = 0;
        pointerId = me.getPointerId(pointerIndex);
        System.out.println("ACTION_DOWN Index: " + pointerIndex + ", ID: " + pointerId);
    }
    if (!idPressed[pointerId])
    {
        idGenerator++;
        idOffset[pointerId] = idGenerator;
        mView.DoTouchEvent(0, mePos[pointerId * 2] - location[0], mePos[pointerId * 2 + 1] - location[1], idOffset[pointerId]);
        idPressed[pointerId] = true;
        //System.out.println("ACTION_DOWN Index: " + pointerIndex + ", ID: " + pointerId + " Gen: " + idOffset[pointerId]);
    }
    break;
}
    case MotionEvent.ACTION_POINTER_UP:
    {
        if (action == MotionEvent.ACTION_POINTER_UP)
        {
            System.out.println("ACTION_POINTER_UP Index: " + pointerIndex + ", ID: " + pointerId);
        }
    }
    case MotionEvent.ACTION_UP:
    {
        if (action == MotionEvent.ACTION_UP)
        {
            System.out.println("Initial ACTION_UP Index: " + pointerIndex + ", ID: " + pointerId);
            pointerIndex = 0;
            pointerId = me.getPointerId(pointerIndex);
            System.out.println("ACTION_UP Index: " + pointerIndex + ", ID: " + pointerId);
        }
        if (idPressed[pointerId])
        {
            mView.DoTouchEvent(1, me.getX(pointerIndex) - location[0], me.getY(pointerIndex) - location[1], idOffset[pointerId]);
            idPressed[pointerId] = false;
            //System.out.println("ACTION_UP Index: " + pointerIndex + ", ID: " + pointerId + " Gen: " + idOffset[pointerId]);
        }
        break;
    }

这是上面的代码记录的日志:

    08-24 12:13:32.603: I/System.out(3570): Initial ACTION_DOWN Index: 0, ID: 0
08-24 12:13:32.603: I/System.out(3570): ACTION_DOWN Index: 0, ID: 0
08-24 12:13:32.943: I/System.out(3570): ACTION_POINTER_DOWN Index: 1, ID: 1
08-24 12:13:32.943: I/System.out(3570): ACTION_POINTER_DOWN Index: 1, ID: 1
08-24 12:13:33.013: I/System.out(3570): ACTION_POINTER_UP Index: 1, ID: 1
08-24 12:13:33.013: I/System.out(3570): ACTION_POINTER_UP Index: 1, ID: 1
08-24 12:13:33.103: I/System.out(3570): ACTION_POINTER_DOWN Index: 1, ID: 1
08-24 12:13:33.103: I/System.out(3570): ACTION_POINTER_DOWN Index: 1, ID: 1
08-24 12:13:33.174: I/System.out(3570): ACTION_POINTER_UP Index: 1, ID: 1
08-24 12:13:33.174: I/System.out(3570): ACTION_POINTER_UP Index: 1, ID: 1
08-24 12:13:33.174: I/System.out(3570): ACTION_POINTER_UP Index: 1, ID: 1
08-24 12:13:33.214: I/System.out(3570): ACTION_POINTER_DOWN Index: 1, ID: 1
08-24 12:13:33.214: I/System.out(3570): ACTION_POINTER_DOWN Index: 1, ID: 1
08-24 12:13:34.115: I/System.out(3570): Initial ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): Initial ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): Initial ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): Initial ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): Initial ACTION_UP Index: 0, ID: 0
08-24 12:13:34.115: I/System.out(3570): ACTION_UP Index: 0, ID: 0

在此日志的末尾,我没有手指在屏幕上,但是如您所见,ID没有发生“向上”事件:1.(同样,我也不知道为什么在事件之后会出现多个相同类型的事件。其他)。

我想念什么吗? 有人有什么主意吗?

谢谢,/ COsmin

我终于找到了问题所在,并在这里向可能有相同问题的其他人发布,因此:问题是我没有在接收到MotionEvent时就对其进行处理,而是将其放在队列中并进行处理下一个更新循环中的所有内容。 问题是我实际上持有一个指向MotionEvent的指针,并且在某些情况下,通过快速/多次触摸,这些对象的内容在处理循环之前被替换了(很可能系统使用了一个MotionEvent对象池,因此对象会不时地被重用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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