Skip to content

Instantly share code, notes, and snippets.

@PVince81
Created January 19, 2013 18:07
Show Gist options
  • Save PVince81/4574016 to your computer and use it in GitHub Desktop.
Save PVince81/4574016 to your computer and use it in GitHub Desktop.
Making sense of multitouch on Android. The purpose is being able to track drag event for multiple pointers. There is a major confusion between pointer ID, action index and how they should be used.
public boolean onTouchEvent(MotionEvent m) {
int pointerCount = m.getPointerCount();
int action = m.getActionMasked();
String actionString;
if ( action == MotionEvent.ACTION_MOVE ){
// we don't know which one moved, so process them all
for ( int i = 0; i < pointerCount; i++ ){
int pointerId = m.getPointerId(i);
int x = (int)m.getX(i);
int y = (int)m.getY(i);
engine.dragMove(pointerId, x, y);
}
}
else{
int actionIndex = m.getActionIndex();
int pointerId = m.getPointerId(actionIndex);
switch (action)
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
int x = (int) m.getX(actionIndex);
int y = (int) m.getY(actionIndex);
engine.dragStart(pointerId, x, y);
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
engine.dragStop(pointerId);
break;
}
}
return true;
}
@behelit
Copy link

behelit commented Apr 9, 2018

Did you ever work it out? I'm struggling with this at the moment. Your post was 5 years ago so I'm not hopeful.
My problem is the pointer Id's used in MOVE events, don't correlate to the pointer Id's of normal events.

i.e.
1st finger DOWN = pointer 0
2nd finger DOWN = pointer 1
2nd finger MOVE = pointer 1
1st finger UP = pointer 0
2nd finger MOVE = pointer 0 <------ Once a finger is lifted, all the pointers shift, however move's still report as the original pointer Id.
Even worse, if you return the 1st finger, the pointers shift back up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment