Handle Touch Event on both segments of the screen simultaneously
Asked Answered
P

1

0

So I have a touch event and it handles ACTION_DOWN and ACTION_UP one by one. For example if I click on the left half of the screen ACTION_DOWN works but not ACTION_UP and same for the right side of the screen. i want to handle both of them simultaneously. If I click on left side and right side at the same time both the event should perform. Can anyone please help me on this. My code is

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //if user press the screen
            if (event.getX() < screenX / 2f) {
                flight.isGoingUp = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            flight.isGoingUp = false;
            if (event.getX() > screenX / 2f) {
                flight.toShoot++;
            }
            break;
    }
    return true;
}

Edit 1 The screen is split into two half. The left side of the screen is used for flight.isGoingUp = true; and when we release the left side or tap the right helg of the screen flight.isGoingUp = false; and if (event.getX() > screenX / 2f) { flight.toShoot++; } I want ot do such a code that both of them are handled at the same time.

Periodontal answered 21/7, 2020 at 22:6 Comment(0)
C
1

here is the docs to handle multi touch gestures.

https://developer.android.com/training/gestures/multi#java

Here is a very simple example to handle multitouch based on google docs.

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getActionMasked();
    int index = event.getActionIndex();
    System.out.println("The finger # " + index + " is " + getAction(action));
    return true;
}

public static String getAction(int action) {
    switch (action) {
        case MotionEvent.ACTION_DOWN: return "Down";
        case MotionEvent.ACTION_MOVE: return "Move";
        case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
        case MotionEvent.ACTION_UP: return "Up";
        case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
        case MotionEvent.ACTION_OUTSIDE: return "Outside";
        case MotionEvent.ACTION_CANCEL: return "Cancel";
        default: return "Unknown";
    }
}
Cressy answered 21/7, 2020 at 22:15 Comment(3)
Yes I already read that document but i;m not getting how to do it!Periodontal
First See my question. The screen is split into two half. The left side of the screen is used for flight.isGoingUp = true; and when we release the left side or tap the right helg of the screen flight.isGoingUp = false; and if (event.getX() > screenX / 2f) { flight.toShoot++; } I want ot do such a code that both of them are handled at the same time.Periodontal
You can get the x and y position of the point and validates with your requirements, if the x and y position is located in the left screen then do this, if is located on the right position do that.Cressy

© 2022 - 2024 — McMap. All rights reserved.