0

I'm trying to make something where doing a pinch rotates an image, using the following code:

public boolean onTouchEvent(MotionEvent e) {
    if(e.getPointerCount() > 1) {
      if (e.getActionMasked()== MotionEvent.ACTION_MOVE) {
            previousX1 = curX1;
            previousY1 = curY1;
            currentX1 = e.getX(1);
            currentY1 = e.getY(1);

            previousX0 = curX0;
            previousY0 = curY0;
            currentX0 = e.getX(0);
            currentY0 = e.getY(0);

            double prevDist = Math.hypot(prevX1 - prevX2, prevY1 - prevY2);
            double dist = Math.hypot(curX1 - curX2, curY1 - curY2);
            angle += ((dist - prevDist) * 0.01);

        }
    }
   return true;
}

Now, this works fine for the most part, except that I want to update the currentX1, currentX2, etc. values as soon as the pinch begins, that is, the MotionEvent in which the second finger touches (or lifts up from) the screen while the first finger is already held down. However, the Android API is rather confusing, and I can't figure out how to detect when this is happening. Using (e.getActionMasked() == MotionEvent.ACTION_DOWN) or (e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) as a condition does not seem to work.

Help is greatly appreciated. Thanks!

1 Answer 1

0

If you want to test only with TWO fingers on the screen, you can use this :

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

              final int action = event.getAction();
              float xPosition = 0;
              float yPosition = 0;


                  switch (action){
                        case MotionEvent.ACTION_DOWN :
                                xPosition = event.getX();
                                yPosition = event.getY();
                            break;
                        case MotionEvent.ACTION_POINTER_2_DOWN :
                                xPosition = event.getX(1);
                                yPosition = event.getY(1);
                            break;  
                        default:
                            break;
                    }
}
1
  • But isn't ACTION_POINTER_2_DOWN deprecated? There's no better way of doing it?
    – Jon W
    Aug 23, 2012 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.