0

I wrote this code for zoom in/out . it works but even with one finger it zoom in/out while it supposed to work only with 2 fingers (multitouch). any knows why ?

    boolean surfaceTouchEvent(MotionEvent event) {
      pointNum=event.getPointerCount();
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        //User is pressing on finger
        x0=event.getX(0);
        y0=event.getY(0);
        mode = false; //DRAG
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        x1=event.getX(0);
        x2=event.getX(1);
        y1=event.getY(0);
        y2=event.getY(1);
        z4 = dist(x1, y1, x2, y2);
        mode = true; // pinch
        break;
    case MotionEvent.ACTION_UP:
     mode = false;
      case MotionEvent.ACTION_POINTER_UP:
        // User is released one of the fingers.
      mode = false;

        break;
      case MotionEvent.ACTION_MOVE:
        if (mode = false) {
         // x1=event.getX(0)-x0;
          //y1=event.getY(0)-y0;
        }
        if (mode = true) {
          x1=event.getX(0);
          x2=event.getX(1);
          y1=event.getY(0);
          y2=event.getY(1);
          float z3 = dist(x1, y1, x2, y2);
          if ( z3 > z4 ) { 
            zoom += 0.1;
          }
          else if ( z3 < z4) {
            zoom -= 0.1;
          }
        }
        break;
      }
      return super.surfaceTouchEvent(event);
    }

I edit the code as below but this time it's not working at all ,at least the first code works but the zoom reacts weird , please help

    float x1,x2,y1,y2 = -1;
    boolean surfaceTouchEvent(MotionEvent event) {
      pointNum=event.getPointerCount();
      if (pointNum >=2) {
        x1=event.getX(0);
        x2=event.getX(1);
        y1=event.getY(0);
        y2=event.getY(1);
        z4 = dist(x1, y1, x2, y2);
        if ( z3 > z4 ) {
          zoom += z3/z4;
        }
        else if ( z3 < z4) {
          zoom -= z3/z4;
        }
      }
      else {
        return false;
      }
      z3 = z4;
      return super.surfaceTouchEvent(event);
    }
Aida E
  • 1,108
  • 2
  • 22
  • 40

1 Answers1

0

You should only execute this when there are more than one finger(s). Just add this if before your code:

boolean surfaceTouchEvent(MotionEvent event) {
  pointNum=event.getPointerCount();
  if(pointNum >= 2) {
    // your code here
  } else {
    return false;
  }
}
Tim
  • 6,692
  • 2
  • 25
  • 30
  • but first I need to save old positions and then to compare the old positions with new one ( when two fingers moves for zoom) to define the zoom thing . where should I define old positions in this case? – Aida E Jun 18 '12 at 09:35
  • define them as instance variables lastX1, lastX2 and lastY1, lastY2 and initialize them with a value like -1 that cannot be assigned by using the touch screen. assign them in your code. – Tim Jun 18 '12 at 09:48