【发布时间】: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