Java – Android Multi-Touch Event Index

Android Multi-Touch Event Index… here is a solution to the problem.

Android Multi-Touch Event Index

I’m running into an issue where a multi-touch event causes the touch index to be released while the finger is still pressed.

Pasted my Java wrapper code below. The function passes an activePointer integer to a C++ function that stores the up/down state in an array using the activePointer value as the array index. The down event sets the array value to true, and the up event sets the array value to false.

The behavior is as follows:

  • If you press a finger, touch index 0 is true.
  • If you press two fingers, touch indices 0 and 1 are true.
  • If you then remove the second finger, touch index 0 is true and touch index 1 is false as expected.
  • If instead,

  • the first finger moves away while the second finger remains touched, both touch indices 0 and 1 are false. I want touch index 0 to be false and touch index 1 to still be true.

Here is my Java wrapper code:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Integer activePointer = (event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT);
    Float x = event.getX(activePointer);
    Float y = event.getY(activePointer);

switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
         Log.i("touchtest", "action_down " + activePointer.toString() +
         "(" + x + "," + y + ")");
        androidRenderer.touch(x, y, activePointer);
        break;
    case MotionEvent.ACTION_MOVE:
         Log.i("touchtest", "action_move " + activePointer.toString() +
         "(" + x + "," + y + ")");
        androidRenderer.move(x, y, activePointer);
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
         Log.i("touchtest", "action_up " + activePointer.toString() + "("
         + x + "," + y + ")");
        androidRenderer.up(x, y, activePointer);
        break;
    }
    return true;
}

I’ve read the doc here, but it’s not very clear.

I messed with the event.findPointerIndex function, but couldn’t deduce any pattern it actually executed.

I can’t imagine this being a rare problem. Can someone with experience in this area provide your advice? Thank you.

Solution

I don’t handle multi-touch events much, but including a default case in your switch statement might help.

Related Problems and Solutions