Android MotionEvent.getActionIndex () and MultiTouch

I am trying to get the identifier of a pointer when the MotionEvent.ACTION_MOVE event occurs.

I do this by calling event.getActionIndex (), but always returns 0 for the second, third, fourth and fifth fingers.

I am using Gingerbread 2.3.3 on Galaxy S I9000

here is my code

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

These are the debugging results.

 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 
+3
android events motion multi-touch
source share
3 answers

I use this code to fire events and it works very well.

 /** 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()); } 

I got this this ZDNET article , so I can not take advantage of this. I work on Android 3.0, but the sample code was originally written for earlier versions of Android, so it should work for you too.

It looks like you need to call getPointerId, not getActionIndex.

+2
source share

I use 3 methods to delegate responsibility for mouse events. It works great on my 2.3 HTC Desire S. it is capable of capturing multiple touch events.

 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; } 
+2
source share

Use 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); 
+1
source share

All Articles