1,找到图片,设置图片的触摸事件。
2,指定图片的缩放类型。
清单文件代码:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <!-- android:scaleType="matrix" 指定图片的缩放类型 --> 7 8 <ImageView 9 android:id="@+id/iv" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:scaleType="matrix" 13 android:src="@drawable/happiness" /> 14 15 </LinearLayout>
具体代码:
1 package com.android.hzy.dragscale; 2 3 import android.app.Activity; 4 import android.graphics.Matrix; 5 import android.graphics.PointF; 6 import android.os.Bundle; 7 import android.util.FloatMath; 8 import android.view.MotionEvent; 9 import android.view.View; 10 import android.view.View.OnTouchListener; 11 import android.widget.ImageView; 12 13 public class MainActivity extends Activity { 14 15 private ImageView iv; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 iv = (ImageView) findViewById(R.id.iv); 23 iv.setOnTouchListener(new MyOnTouchListener()); 24 } 25 26 private final class MyOnTouchListener implements OnTouchListener { 27 28 private PointF startPoint = new PointF(); // 初始点 29 private Matrix mCurrentMatrix = new Matrix(); // 图片初始的matrix值 30 private Matrix mMatrix = new Matrix(); // 一个临时的matrix 31 32 private PointF midPointF; // 中心点 33 private float midDistance; // 两点间的距离 34 35 private int type = 0; 36 private final static int DRAG = 1; 37 private final static int SCALE = 2; 38 39 @Override 40 public boolean onTouch(View v, MotionEvent event) { 41 // TODO Auto-generated method stub 42 int action = event.getAction() & MotionEvent.ACTION_MASK; // 得到动作的类型 43 // 只有低8位有效,&255即可 44 switch (action) { 45 case MotionEvent.ACTION_DOWN: // 手指按下 46 float x = event.getX(); 47 float y = event.getY(); 48 startPoint.set(x, y); 49 mCurrentMatrix.set(iv.getImageMatrix()); // 图片没有移动的位置 50 type = DRAG; 51 break; 52 53 case MotionEvent.ACTION_POINTER_1_DOWN:// 屏幕上已经有一根手指,再按下一根手指 54 mCurrentMatrix.set(iv.getImageMatrix()); // 图片没有缩放的大小 55 midPointF = getMidPointF(event); 56 midDistance = getMidDistance(event); 57 type = SCALE; 58 break; 59 60 case MotionEvent.ACTION_MOVE: // 手指一动 61 if (type == DRAG) { // 移动 62 // 将原先没有移动的matrix放进临时的matrix 63 mMatrix.set(mCurrentMatrix); 64 float dx = event.getX() - startPoint.x; 65 float dy = event.getY() - startPoint.y; 66 mMatrix.postTranslate(dx, dy); 67 } else if (type == SCALE) { // 缩放 68 mMatrix.set(mCurrentMatrix); // 设置当前的 69 float distance = getMidDistance(event); // 得到当前两个手指间的距离 70 float sx = distance / midDistance; // 得到缩放的倍数 71 mMatrix.postScale(sx, sx, midPointF.x, midPointF.y); 72 } 73 break; 74 75 case MotionEvent.ACTION_UP: // 手指弹起 76 case MotionEvent.ACTION_POINTER_1_UP: // 弹起一根手指,屏幕上还有一根手指 77 type = 0; // 手指弹起就置为0 78 break; 79 80 default: 81 break; 82 } 83 iv.setImageMatrix(mMatrix); // 将拖动后的matrix设置到图片 84 return true; 85 } 86 } 87 88 /** 89 * 获取中心点 90 * 91 * @param event 92 * @return 93 */ 94 private PointF getMidPointF(MotionEvent event) { 95 // (后面的手指 + 前面的手指)/2 96 float x = (event.getX(1) + event.getX(0)) / 2; 97 float y = (event.getY(1) + event.getY(0)) / 2; 98 return new PointF(x, y); 99 } 100 101 /** 102 * 计算两点之间的距离 103 * 104 * @param event 105 * @return 106 */ 107 private float getMidDistance(MotionEvent event) { 108 109 float dx = event.getX(1) - event.getX(0); 110 float dy = event.getX(1) - event.getX(0); 111 112 return FloatMath.sqrt(dx * dx + dy * dy); 113 } 114 115 }