我试图在Android上跟踪多点触控事件(我需要跟踪"触及""触摸"每个手指)。问题是PointerId似乎不一致或者我在链接" down"活动到" up"因此,我错过了一些" up"事件
以下是我用来处理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
在这个日志的最后,我没有手指在屏幕上,但你可以看到没有" up" ID的事件:1。(我也不知道为什么我会一个接一个地获得相同类型的多个事件)。
我错过了什么吗?任何人都有任何想法?
谢谢, /科斯明
答案 0 :(得分:0)
我终于找到了这个问题,我在这里发布了其他可能有同样问题的人,所以:问题是我收到时没有处理MotionEvent,而是我把它放在一个在下一个更新循环中排队并处理所有内容。问题是我实际上拿着一个指向MotionEvent的指针,并且在某些情况下有快速/多次触摸,这些对象的内容在处理循环之前被替换(很可能系统使用了一个MotionEvent对象池,所以一个对象将不时重复使用。)