2011-05-02 2 views
3

MotionEvent.ACTION_MOVE 이벤트가 발생할 때 포인터 ID를 얻으려고합니다.Android MotionEvent.getActionIndex() 및 MultiTouch

나는 event.getActionIndex()를 호출하여이 작업을 수행하지만 두 번째, 세 번째, 네 번째 및 다섯 번째 손가락에 항상 0을 반환합니다.

내가 갤럭시 S I9000 여기에

를 진저 브레드 2.3.3을 사용하고이 디버그 내가 이벤트를 덤프하기 위해이 코드를 사용하고

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

결과입니다

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

내 코드입니다 , 그리고 그것은 꽤 잘 작동합니다.

/** 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 이전 버전 용으로 작성되었으므로 잘 작동합니다.

getActionIndex 대신 getPointerId를 호출해야하는 것처럼 보입니다.

+0

코드 http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding- ([여기] 내지 touch event/1775? tag = mantle_skin; content) –

+1

Jim, 나에게 제공 한 코드를 사용했는데, ACTION_MOVE 이벤트를 일으키는 포인터 ID를 제공하지 않는다는 것을 제외하고는 꽤 잘 작동한다. -02 20 : 15 : 38.863 : DEBUG/MTView (5033) : 이벤트 ACTION_MOVE [# 0 (PID 0) = 61,52; # 1 (PID 1) = 265,360]) 및 (05-02 20 : 15 : 38.878 : DEBUG/MTView (5033) : 이벤트 ACTION_MOVE [# 0 (pid 0) = 61,52; # 1 (pid 1) = 256,368]), 두 번째 손가락을 움직이면 두 이벤트가 연속됩니다. 짐 많이 빌려 줘서 고마워. – kamelbox

+0

오신 것을 환영합니다. 그리고 어떤 손가락이 사건을 일으켰는지 알려주지 않습니다. 각 손가락의 현재 위치를 알려줍니다. 물론 현재 위치를 이전 위치와 비교하면 어떤 손가락이 그것을 초래했는지 추측 할 수 있습니다. –

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);