在下面的代码中,我正在我的设备上测试多点触控。我遇到的问题是移动动作。如果我用一根手指触摸屏幕,我会在 Logcat 中获得触摸输入的 ID,当我用第二根手指触摸屏幕并开始移动它时,我会获得两根手指的 ID,尽管第一根手指不是动不动!我很困惑,需要一些帮助来改进我的代码。
@Override
public boolean onTouch(View v, MotionEvent event) {
int pointerId;
int pointerIndex;
synchronized (gameLoop) {
for (int i = 0; i < event.getPointerCount() && i < 2; i++) {
num = event.getPointerCount();
//id = event.getPointerId(i);
// Down
if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN || event.getActionMasked() == MotionEvent.ACTION_DOWN)) {
int j = event.getPointerCount();
int id = event.getPointerId(i);
String numDown = Integer.toString(j);
String sId = Integer.toString(id);
Log.i("Test", "Number DOWN: " + numDown + " ID: " + sId);
}
// Move
if(event.getActionMasked() == MotionEvent.ACTION_MOVE) {
int j = event.getPointerCount();
int id = event.getPointerId(i);
String numDown = Integer.toString(j);
String sId = Integer.toString(id);
Log.i("Test", "Moving: " + numDown + " ID: " + sId);
}
// Up
if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP || event.getActionMasked() == MotionEvent.ACTION_UP)) {
int k = event.getPointerCount();
int kid = event.getPointerId(i);
String numUp = Integer.toString(k);
String ssId = Integer.toString(kid);
Log.i("Test", "Number UP: " + numUp + " ID: " + ssId);
}
}
}
return true;
}