2013-06-18 85 views
0

在下面的代碼中,我正在測試多點觸控。當第一根手指觸摸屏幕時,我得到該手指的ID,如果我還用另一個手指觸摸屏幕,我也會得到該ID。但是當我移動手指時,我只能得到ID 0,沒有ID 1,第二根手指!我需要稍後將ID傳遞給一個存儲X和Y座標的方法,並且我需要知道在屏幕上繪製哪個ID。如何獲取移動手指的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; 
} 

回答

1

你並不需要爲環

公衆最終詮釋getPointerId(INT pointerIndex)指針

參數pointerIndex生指數檢索。從0(第一個指向下的指針)到getPointerCount() - 1的值可能爲 。

先以手指(event.getX(0),event.getY(0))

二手指(event.getX(1),event.getY(1))