Search code examples
javaandroidmulti-touchmotioneventontouch

Android multi-touch: Not detecting MotionEvent.ACTION_POINTER_DOWN


Here is a part of my code. I don't understand why MotionEvent.ACTION_POINTER_DOWN (a second finger touching the screen) isn't being detected. Can someone explain why this isn't working?

Single player works fine. Two player (With multi-touch) isn't.

What is working for two player -When the user presses only one of either halfs of the screen, it calls the goUp()

What doesn't work for two player -When the user presses one half of the screen, then presses the other half, the other half doesn't call goUp()

Also, is there a way to simulate multiple touches at once on an emulator so i can see the logs in the logcat?

private int pointerId = -1, pointerId2 = -1;

@Override
public boolean onTouch(View view, MotionEvent event) {

    // Depending on the touch, depends on the direction of the ricer (up or down)
    if(!TWO_PLAYER_MODE){
        switch (event.getActionMasked()) {

        // When finger touches the screen, ricer will accelerate up
        case MotionEvent.ACTION_DOWN:
            ricerView.goUp();
            break;

            // When finger is lifted from the screen, ricer will accelerate down
        case MotionEvent.ACTION_UP:
            ricerView.goDown();
        }
    }
    else{

        float touchY = event.getY();
        int pointerIndex = event.getActionIndex();
        int pointerID = event.getPointerId(pointerIndex);

        switch (event.getActionMasked()) {
        // When finger touches the screen, ricer will accelerate up
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            if( touchOnTopOfScreen(touchY) ){
                pointerId = pointerID;
                ricerView.goUp();
                log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
            }
            else{
                pointerId2 = pointerID;
                ricerView2.goUp();
                log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
            }
            break;

            // When finger is lifted from the screen, ricer will accelerate down
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            if( pointerId == pointerID ){
                ricerView.goDown();
                pointerId = -1;
                log("RV1 up Id=" + pointerID + " Y-pos=" + touchY);
            }
            if( pointerId2 == pointerID ){
                ricerView2.goDown();
                pointerId2 = -1;
                log("RV2 up Id=" + pointerID + " Y-pos=" + touchY);
            }
        }
    }

    // Delegate the touch to the gestureDetector 
    mGestureDetector.onTouchEvent(event);

    return true;

}   // End of onTouchEvent

private boolean touchOnTopOfScreen(float y){
    if(mDisplayHeight/2 > y)
        return true;
    else
        return false;
}

Solution

  • After giving up on this for a few weeks, i came back to this and found my solution which was actually a very easy fix. It all works now! It seems that when you try to get the x or y value of the event, you need to use event.getY(pointerIndex) rather than event.getY() The only change i made was:

    int pointerIndex = event.getActionIndex();
    int pointerID = event.getPointerId(pointerIndex);
    float touchY = event.getY(pointerIndex);