Android multitouch: no ACTION_POINTER_DOWN and UP events

609 views Asked by At

Here is my code:

public boolean onTouch(View v, MotionEvent event) {

    switch(event.getActionMasked()) {

    case MotionEvent.ACTION_DOWN:
        Log.d("getButtonCode", "catch ACTION_DOWN");
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        Log.d("getButtonCode", "catch ACTION_POINTER_DOWN");
        break;
    case MotionEvent.ACTION_POINTER_UP:
        Log.d("getButtonCode", "catch ACTION_POINTER_UP");
        break;
    case MotionEvent.ACTION_UP:
        Log.d("getButtonCode", "catch ACTION_UP");
    case MotionEvent.ACTION_CANCEL:
        Log.d("getButtonCode", "catch ACTION_CANCEL");
    case MotionEvent.ACTION_MOVE:
        break;
    default:
        break;
    }
    return true;
}

and I never catch ACTION_POINTER_DOWN and ACTION_POINTER_UP. If I remove breakfrom ACTION_DOWN, I catch ACTION_POINTER_DOWN always after ACTION_DOWN, even if it is first pointer. I think it should be so: first pointer catch only ACTION_DOWN, every next pointer only ACTION_POINTER_DOWN, if non-primary pointer has gone up, I should catch ACTION_POINTER_UP.

But it doesn't work. What is wrong in my code?

PS: I saw other similar questions, but no answer helped me.

1

There are 1 answers

0
Glen Pierce On

Instead of using a switch statement, I flowed through a series of if statements. This let me test against event.getAction() or event.getActionMasked() and it allowed multiple possible actions through at once.

public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                startingX = (int) event.getX();
                startingY = (int) event.getY();
            }
            if(event.getAction() == MotionEvent.ACTION_MOVE) {
                int scaleX = 1;
                int scaleY = 1;
                if(touchStateScale == true){
                    scaleX = (int) (event.getX(0) - event.getX(1));
                    scaleY = (int) (event.getY(0) - event.getY(1));
                }
                int endingX = (int) event.getX();
                int endingY = (int) event.getY();

                int newX = endingX-startingX;
                int newY = endingY-startingY;


            ((ImageView) v).setImageBitmap(drawNewImageScaled(baseImage, newImageResourceId, newX, newY, scaleX, scaleY));
            }
            if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
                Log.i(TAG, "second pointer detected");
                touchStateScale = true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                touchStateScale = false;
                return false;
            }
            return true;
        }
    };