rotation and scaling using multi touch in android

前端 未结 1 1504
礼貌的吻别
礼貌的吻别 2020-12-24 09:25

i want to rotate and scale the image on multi touch event, i am able to drag, scale the image but i can\'t understand the rotation of image. i am facing problem so please he

相关标签:
1条回答
  • 2020-12-24 10:14
       float[] lastEvent = null;
       float d = 0f;
       float newRot = 0f;
       @Override
       public boolean onTouch(View v, MotionEvent event) {
          ImageView view = (ImageView) v;
    
          // Dump touch event to log
          dumpEvent(event);
    
          // Handle touch events here...
          switch (event.getAction() & MotionEvent.ACTION_MASK) {
          case MotionEvent.ACTION_DOWN:
             savedMatrix.set(matrix);
         start.set(event.getX(), event.getY());
         if (Constant.TRACE) Log.d(TAG, "mode=DRAG");
         mode = DRAG;
         lastEvent = null;
         break;
      case MotionEvent.ACTION_POINTER_DOWN:
         oldDist = spacing(event);         
         savedMatrix.set(matrix);
         midPoint(mid, event);
         mode = ZOOM;
         if (Constant.TRACE) Log.d(TAG, "mode=ZOOM");
    
         lastEvent = new float[4];
         lastEvent[0] = event.getX(0);
         lastEvent[1] = event.getX(1);
         lastEvent[2] = event.getY(0);
         lastEvent[3] = event.getY(1);
         d = rotation(event);
         break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
         mode = NONE;
         lastEvent = null;
         if (Constant.TRACE) Log.d(TAG, "mode=NONE");         
         break;
      case MotionEvent.ACTION_MOVE:
         if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
         }
         else if (mode == ZOOM && event.getPointerCount()==2) {
            float newDist = spacing(event);
            if (Constant.TRACE) Log.d(TAG, "Count=" + event.getPointerCount());
            if (Constant.TRACE) Log.d(TAG, "newDist=" + newDist);
            matrix.set(savedMatrix);
            if (newDist > 10f) {              
               float scale = newDist / oldDist;
               matrix.postScale(scale, scale, mid.x, mid.y);
            } 
            if (lastEvent!=null){
                newRot = rotation(event); 
                if (Constant.TRACE) Log.d("Degreeeeeeeeeee", "newRot="+(newRot));
                float r = newRot-d;
                matrix.postRotate(r, imgView.getMeasuredWidth()/2, imgView.getMeasuredHeight()/2);  
            }
         }
         break;
      }
    
      view.setImageMatrix(matrix);
      return true; // indicate event was handled
    

    }

    /** Determine the degree between the first two fingers */
       private float rotation(MotionEvent event) {  
           double delta_x = (event.getX(0) - event.getX(1));
           double delta_y = (event.getY(0) - event.getY(1));
           double radians = Math.atan2(delta_y, delta_x);       
           if (Constant.TRACE) Log.d("Rotation ~~~~~~~~~~~~~~~~~", delta_x+" ## "+delta_y+" ## "+radians+" ## "
                            +Math.toDegrees(radians));
           return (float) Math.toDegrees(radians);
       }
    

    Try this code, but remember some old phone have problem with rotation..

    0 讨论(0)
提交回复
热议问题