在下面的代码中,我正在测试多点触控。当第一根手指触摸屏幕时,我得到那个手指的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;
}
答案 0 :(得分:1)
你不需要for-loop
public final int getPointerId(int pointerIndex)
参数pointerIndex要检索的指针的原始索引。价值可能是 从0(向下的第一个指针)到getPointerCount() - 1。
第一根手指(event.getX(0),event.getY(0))
第二根手指(event.getX(1),event.getY(1))