[转] android 多点触摸 实现图片缩放 Image Zoom

  1. import android.graphics.Matrix;   
  2. import android.graphics.PointF;   
  3. import android.util.FloatMath;   
  4. import android.view.MotionEvent;   
  5. import android.view.View;   
  6. import android.view.View.OnTouchListener;   
  7. import android.widget.ImageView;   
  8. public class MulitPointTouchListener implements OnTouchListener {   
  9.         Matrix matrix = new Matrix();   
  10.         Matrix savedMatrix = new Matrix();   
  11.         static final int NONE = 0;   
  12.         static final int DRAG = 1;   
  13.         static final int ZOOM = 2;   
  14.         int mode = NONE;   
  15.         PointF start = new PointF();   
  16.         PointF mid = new PointF();   
  17.         float oldDist = 1f;   
  18.         @Override   
  19.         public boolean onTouch(View v, MotionEvent event) {   
  20.                 ImageView view = (ImageView) v;   
  21.                 switch (event.getAction() & MotionEvent.ACTION_MASK) {   
  22.                 case MotionEvent.ACTION_DOWN:   
  23.                         matrix.set(view.getImageMatrix());   
  24.                         savedMatrix.set(matrix);   
  25.                         start.set(event.getX(), event.getY());   
  26.                         mode = DRAG;   
  27.                          
  28.                         break;   
  29.                 case MotionEvent.ACTION_POINTER_DOWN:   
  30.                         oldDist = spacing(event);   
  31.                         if (oldDist > 10f) {   
  32.                                 savedMatrix.set(matrix);   
  33.                                 midPoint(mid, event);   
  34.                                 mode = ZOOM;   
  35.                         }   
  36.                         break;   
  37.                 case MotionEvent.ACTION_UP:   
  38.                 case MotionEvent.ACTION_POINTER_UP:   
  39.                         mode = NONE;   
  40.                         break;   
  41.                 case MotionEvent.ACTION_MOVE:   
  42.                         if (mode == DRAG) {   
  43.                                 matrix.set(savedMatrix);   
  44.                                 matrix.postTranslate(event.getX() - start.x, event.getY()   
  45.                                                 - start.y);   
  46.                         } else if (mode == ZOOM) {   
  47.                                 float newDist = spacing(event);   
  48.                                 if (newDist > 10f) {   
  49.                                         matrix.set(savedMatrix);   
  50.                                         float scale = newDist / oldDist;   
  51.                                         matrix.postScale(scale, scale, mid.x, mid.y);   
  52.                                 }   
  53.                         }   
  54.                         break;   
  55.                 }   
  56.                 view.setImageMatrix(matrix);   
  57.                 return true;   
  58.         }   
  59.           
  60.          
  61.         private float spacing(MotionEvent event) {   
  62.                 float x = event.getX(0) - event.getX(1);   
  63.                 float y = event.getY(0) - event.getY(1);   
  64.                 return FloatMath.sqrt(x * x + y * y);   
  65.         }   
  66.          
  67.         private void midPoint(PointF point, MotionEvent event) {   
  68.                 float x = event.getX(0) + event.getX(1);   
  69.                 float y = event.getY(0) + event.getY(1);   
  70.                 point.set(x / 2, y / 2);   
  71.         }   
  72. }   
  73. imageView.setOnTouchListener(new MulitPointTouchListener ());在xml中要将图片的缩放格式改成Matrix   
  74.   
  75. 注:android:scaleType="matrix"   
  76. 注:此文档为转载  

posted on 2012-05-18 21:57  fuxy  阅读(609)  评论(0编辑  收藏  举报

导航