【问题标题】:Understanding multi-touch on android了解android上的多点触控
【发布时间】:2014-08-24 10:20:55
【问题描述】:

我正在尝试跟踪 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

【问题讨论】:

    标签: android multi-touch


    【解决方案1】:

    我终于找到了这个问题,我在这里为可能有同样问题的其他人发帖,所以:问题是我在收到 MotionEvent 时没有正确处理它,而是将它放在在下一个更新循环中排队并处理所有内容。问题是我实际上持有一个指向 MotionEvent 的指针,并且在某些情况下具有快速/多次触摸,这些对象的内容在处理循环之前被替换(很可能有系统使用的 MotionEvent 对象池等一个对象会不时被重用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多