2011-05-02 16 views
3

我試圖讓事件MotionEvent.ACTION_MOVE發生時的指針ID。Android MotionEvent.getActionIndex()和MultiTouch

我正在通過調用event.getActionIndex()來做到這一點,但它總是返回0爲第二,第三,第四和第五手指。

我使用薑餅2.3.3的Galaxy S I9000

這裏是我的代碼

switch (event.getActionMasked()) { 
case MotionEvent.ACTION_MOVE: { 
    Log.d("D"," getActionIndex()="+event.getActionIndex()); 
    };break; 
} 

這是調試結果

05-02 19:20:08.628: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=1 
05-02 19:20:08.781: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=1 
05-02 19:20:08.820: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=1 
05-02 19:20:08.914: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=1 
05-02 19:20:09.070: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:09.187: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:09.324: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:09.460: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:09.523: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:09.542: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:09.679: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=3 
05-02 19:20:09.703: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=3 
05-02 19:20:09.847: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=3 
05-02 19:20:10.117: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=3 
05-02 19:20:10.261: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.281: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.304: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.371: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.410: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.433: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.519: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.558: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=4 
05-02 19:20:10.613: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=3 
05-02 19:20:10.640: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=2 
05-02 19:20:10.656: DEBUG/D(4534): getActionIndex()=0 getPointerCount()=1 

回答

2

我使用此代碼傾倒事件,它工作得很好。

/** Show an event in the LogCat view, for debugging */ 
private void dumpEvent(MotionEvent event) { 
    String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" , 
     "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" }; 
    StringBuilder sb = new StringBuilder(); 
    int action = event.getAction(); 
    int actionCode = action & MotionEvent.ACTION_MASK; 
    sb.append("event ACTION_").append(names[actionCode]); 
    if (actionCode == MotionEvent.ACTION_POINTER_DOWN 
     || actionCode == MotionEvent.ACTION_POINTER_UP) { 
     sb.append("(pid ").append(
     action >> MotionEvent.ACTION_POINTER_ID_SHIFT); 
     sb.append(")"); 
    } 
    sb.append("["); 
    for (int i = 0; i < event.getPointerCount(); i++) { 
     sb.append("#").append(i); 
     sb.append("(pid ").append(event.getPointerId(i)); 
     sb.append(")=").append((int) event.getX(i)); 
     sb.append(",").append((int) event.getY(i)); 
     if (i + 1 < event.getPointerCount()) 
     sb.append(";"); 
    } 
    sb.append("]"); 
    Log.d(TAG, sb.toString()); 
} 

我把它關閉this ZDNET article,所以我不能把它的信用。我在Android 3.0上運行,但示例代碼最初是爲較早版本的Android編寫的,因此它也適用於您。

它看起來像你需要調用getPointerId而不是getActionIndex。

+0

該代碼是從[這裏](http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding- touch-events/1775?tag = mantle_skin; content) – 2011-05-02 19:03:01

+1

謝謝Jim,我用你提供給我的代碼,它工作得很好,除非它不提供指針ID導致ACTION_MOVE事件,這就是我得到的(05 -02 20:15:38.863:DEBUG/MTView(5033):event ACTION_MOVE [#0(pid 0)= 61,52;#1(pid 1)= 265,360])和(05-02 20:15:38.878 :DEBUG/MTView(5033):event ACTION_MOVE [#0(pid 0)= 61,52;#1(pid 1)= 256,368]),因爲我移動了第二根手指,這兩個事件是連續的。謝謝你,吉姆。 – kamelbox 2011-05-02 19:37:05

+0

不客氣,是的,它並沒有告訴你哪個手指引起了事件。它只是告訴你每個手指的當前位置。當然,如果您將當前位置與以前的位置進行比較,則可以推斷出哪根手指導致了這種情況。 – 2011-05-02 20:02:38

2

我使用3種方法委託鼠標事件的責任。它可以在我的2.3 HTC Desire S上正常工作,它可以捕獲多個觸摸事件。

public void processMouseMove(int mouseX, int mouseY, int pid) 
public void processMouseDown(int mouseX, int mouseY, int pid) 
public void processMouseUp(int mouseX, int mouseY, int pid) 


public boolean onTouch(View v, MotionEvent event) { 
    int p = event.getActionIndex(); 
    switch(event.getActionMasked()){ 
     case MotionEvent.ACTION_DOWN: 
     case MotionEvent.ACTION_POINTER_DOWN: 
      processMouseDown((int)event.getX(p), (int)event.getY(p), event.getPointerId(p));     
     break;    
     case MotionEvent.ACTION_POINTER_UP:     
     case MotionEvent.ACTION_UP: 
      processMouseUp((int)event.getX(p), (int)event.getY(p), event.getPointerId(p)); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      final int historySize = event.getHistorySize(); 
      final int pointerCount = event.getPointerCount(); 
      for (int h = 0; h < historySize; h++) { 
       for (int p1 = 0; p1 < pointerCount; p1++) { 
        processMouseMove((int)event.getHistoricalX(p1, h), (int)event.getHistoricalY(p1, h), event.getPointerId(p1)); 
       } 
      } 
      for (int p1 = 0; p1 < event.getPointerCount(); p1++) { 

       processMouseMove((int)event.getX(p1), (int)event.getY(p1), event.getPointerId(p1)); 
      } 
    } 
    return true; 
} 
1

使用MotionEventCompat。

// get pointer index from the event object 

int pointerIndex = MotionEventCompat.getActionIndex(event); 

// get masked (not specific to a pointer) action 

int maskedAction = MotionEventCompat.getActionMasked(event); 
相關問題