0

我为放大/缩小编写了这段代码。它可以工作,但即使用一根手指也可以放大/缩小,而它应该只能用两根手指(多点触控)工作。有谁知道为什么?

    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);
    }

我编辑代码如下,但这次它根本不起作用,至少第一个代码有效,但缩放反应很奇怪,请帮忙

    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);
    }
4

1 回答 1

0

仅当有多个手指时才应执行此操作。如果在你的代码之前添加这个:

boolean surfaceTouchEvent(MotionEvent event) {
  pointNum=event.getPointerCount();
  if(pointNum >= 2) {
    // your code here
  } else {
    return false;
  }
}
于 2012-06-18T08:04:44.333 回答