在下面的代码中,我正在测试多点触控。当第一根手指触摸屏幕时,我会得到那个手指的 ID,如果我也用第二根手指触摸屏幕,我也会得到那个 ID。但是当我移动手指时,我只得到 ID 0 而不是 ID 1,第二根手指!稍后我需要将 id 传递给存储 X 和 Y 坐标的方法,并且我需要知道在屏幕上绘制的 ID。
如何获得运动的正确 ID?
@Override
public boolean onTouch(View v, MotionEvent event) {
synchronized (gameLoop) {
for (int i = 0; i < event.getPointerCount() && i < 2; i++) {
id = event.getPointerCount();
// Check if finger touch screen
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);
}
// Check if finger moves on screen
if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_MOVE)) {
int id = event.getPointerId(i);
String sId = Integer.toString(id);
Log.i("Test", " Moving ID: " + sId);
}
// Check if finger leave screen
if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP || event.getActionMasked() == MotionEvent.ACTION_UP)) {
int k = event.getPointerCount();
int id = event.getPointerId(i);
String numUp = Integer.toString(k);
String sId = Integer.toString(id);
Log.i("Test", "Number UP: " + numUp + " ID: " + sId);
}
}
}
return true;
}