双指拖动缩放图片

iv = (ImageView) findViewById(R.id.iv); 
iv.setOnTouchListener(new MulitPointTouchListener(iv)); 
iv.setScaleType(ScaleType.CENTER_INSIDE); 
Java代码  10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; } break; case MotionEvent.ACTION_UP: Log.w("FLAG", "ACTION_UP"); case MotionEvent.ACTION_POINTER_UP: Log.w("FLAG", "ACTION_POINTER_UP"); mode = NONE; break; case MotionEvent.ACTION_MOVE: Log.w("FLAG", "ACTION_MOVE"); if (mode == DRAG) { matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); } else if (mode == ZOOM) { float newDist = spacing(event); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); } } break; } view.setImageMatrix(matrix); return true; } private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return FloatMath.sqrt(x * x + y * y); } private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2); } } " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 收藏代码
  1. public class MulitPointTouchListener implements OnTouchListener {  
  2.   
  3.         Matrix matrix = new Matrix();  
  4.         Matrix savedMatrix = new Matrix();  
  5.   
  6.         public ImageView image;  
  7.         static final int NONE = 0;  
  8.         static final int DRAG = 1;  
  9.         static final int ZOOM = 2;  
  10.         int mode = NONE;  
  11.   
  12.         PointF start = new PointF();  
  13.         PointF mid = new PointF();  
  14.         float oldDist = 1f;  
  15.   
  16.         public MulitPointTouchListener(ImageView image) {  
  17.             super();  
  18.             this.image = image;  
  19.         }  
  20.   
  21.         @Override  
  22.         public boolean onTouch(View v, MotionEvent event) {  
  23.             this.image.setScaleType(ScaleType.MATRIX);  
  24.   
  25.             ImageView view = (ImageView) v;  
  26.             // dumpEvent(event);  
  27.   
  28.             switch (event.getAction() & MotionEvent.ACTION_MASK) {  
  29.   
  30.             case MotionEvent.ACTION_DOWN:  
  31.   
  32.                 Log.w("FLAG""ACTION_DOWN");  
  33.                 matrix.set(view.getImageMatrix());  
  34.                 savedMatrix.set(matrix);  
  35.                 start.set(event.getX(), event.getY());  
  36.                 mode = DRAG;  
  37.                 break;  
  38.             case MotionEvent.ACTION_POINTER_DOWN:  
  39.                 Log.w("FLAG""ACTION_POINTER_DOWN");  
  40.                 oldDist = spacing(event);  
  41.                 if (oldDist > 10f) {  
  42.                     savedMatrix.set(matrix);  
  43.                     midPoint(mid, event);  
  44.                     mode = ZOOM;  
  45.                 }  
  46.                 break;  
  47.             case MotionEvent.ACTION_UP:  
  48.                 Log.w("FLAG""ACTION_UP");  
  49.             case MotionEvent.ACTION_POINTER_UP:  
  50.                 Log.w("FLAG""ACTION_POINTER_UP");  
  51.                 mode = NONE;  
  52.                 break;  
  53.             case MotionEvent.ACTION_MOVE:  
  54.                 Log.w("FLAG""ACTION_MOVE");  
  55.                 if (mode == DRAG) {  
  56.                     matrix.set(savedMatrix);  
  57.                     matrix.postTranslate(event.getX() - start.x, event.getY()  
  58.                             - start.y);  
  59.                 } else if (mode == ZOOM) {  
  60.                     float newDist = spacing(event);  
  61.                     if (newDist > 10f) {  
  62.                         matrix.set(savedMatrix);  
  63.                         float scale = newDist / oldDist;  
  64.                         matrix.postScale(scale, scale, mid.x, mid.y);  
  65.                     }  
  66.                 }  
  67.                 break;  
  68.             }  
  69.   
  70.             view.setImageMatrix(matrix);  
  71.             return true;  
  72.         }  
  73.   
  74.         private float spacing(MotionEvent event) {  
  75.             float x = event.getX(0) - event.getX(1);  
  76.             float y = event.getY(0) - event.getY(1);  
  77.             return FloatMath.sqrt(x * x + y * y);  
  78.         }  
  79.   
  80.         private void midPoint(PointF point, MotionEvent event) {  
  81.             float x = event.getX(0) + event.getX(1);  
  82.             float y = event.getY(0) + event.getY(1);  
  83.             point.set(x / 2, y / 2);  
  84.         }  
  85.     }  

你可能感兴趣的:(双指拖动缩放图片)