package com.example.tupian_moxing;
import android.os.Bundle;
import android.app.Activity;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.graphics.Matrix;
import android.graphics.PointF;
public class MainActivity extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)this.findViewById(R.id.img);
img.setOnTouchListener(new OnTouchListener() {
/** 记录是拖拉照片模式还是放大缩小照片模式 */
private int mode = 0;// 初始状态
/** 拖拉照片模式 */
private static final int MODE_DRAG = 1;
/** 放大缩小照片模式 */
private static final int MODE_ZOOM = 2;
/** 用于记录开始时候的坐标位置 */
private PointF startPoint = new PointF();
/** 用于记录拖拉图片移动的坐标位置 */
private Matrix oldmatrix = new Matrix();
/** 用于记录图片要进行拖拉时候的坐标位置 */
private Matrix newMatrix = new Matrix();
/** 两个手指的开始距离 */
private float startDis;
/** 两个手指的中间点 */
private PointF midPoint;
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
switch (arg1.getAction()&MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = MODE_DRAG;
oldmatrix.set(img.getImageMatrix());
startPoint.set(arg1.getX(),arg1.getY());
break;
case MotionEvent.ACTION_MOVE:
if(mode==MODE_DRAG){
float dx=arg1.getX()-startPoint.x;
float dy=arg1.getY()-startPoint.y;
newMatrix.set(oldmatrix);
newMatrix.postTranslate(dx, dy);
}else if(mode==MODE_ZOOM){
float end=distance(arg1);
if(end>10f){
float scale=end/startDis;
newMatrix.set(oldmatrix);
newMatrix.postScale(scale,scale,midPoint.x,midPoint.y);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode=0;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode=MODE_ZOOM;
startDis=distance(arg1);
if (startDis > 10f) { // 两个手指并拢在一起的时候像素大于10
midPoint = mid(arg1);
//记录当前ImageView的缩放倍数
oldmatrix.set(img.getImageMatrix());
}
break;
}
img.setImageMatrix(newMatrix);
return true;
}
/** 计算两个手指间的距离 */
private float distance(MotionEvent event) {
float dx = event.getX(1) - event.getX(0);
float dy = event.getY(1) - event.getY(0);
/** 使用勾股定理返回两点之间的距离 */
return FloatMath.sqrt(dx * dx + dy * dy);
}
/** 计算两个手指间的中间点 */
private PointF mid(MotionEvent event) {
float midX = (event.getX(1)+event.getX(0))/2;
float midY = (event.getY(1)+event.getY(0))/2;
return new PointF(midX, midY);
}
});
}
import android.os.Bundle;
import android.app.Activity;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.graphics.Matrix;
import android.graphics.PointF;
public class MainActivity extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)this.findViewById(R.id.img);
img.setOnTouchListener(new OnTouchListener() {
/** 记录是拖拉照片模式还是放大缩小照片模式 */
private int mode = 0;// 初始状态
/** 拖拉照片模式 */
private static final int MODE_DRAG = 1;
/** 放大缩小照片模式 */
private static final int MODE_ZOOM = 2;
/** 用于记录开始时候的坐标位置 */
private PointF startPoint = new PointF();
/** 用于记录拖拉图片移动的坐标位置 */
private Matrix oldmatrix = new Matrix();
/** 用于记录图片要进行拖拉时候的坐标位置 */
private Matrix newMatrix = new Matrix();
/** 两个手指的开始距离 */
private float startDis;
/** 两个手指的中间点 */
private PointF midPoint;
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
switch (arg1.getAction()&MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = MODE_DRAG;
oldmatrix.set(img.getImageMatrix());
startPoint.set(arg1.getX(),arg1.getY());
break;
case MotionEvent.ACTION_MOVE:
if(mode==MODE_DRAG){
float dx=arg1.getX()-startPoint.x;
float dy=arg1.getY()-startPoint.y;
newMatrix.set(oldmatrix);
newMatrix.postTranslate(dx, dy);
}else if(mode==MODE_ZOOM){
float end=distance(arg1);
if(end>10f){
float scale=end/startDis;
newMatrix.set(oldmatrix);
newMatrix.postScale(scale,scale,midPoint.x,midPoint.y);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode=0;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode=MODE_ZOOM;
startDis=distance(arg1);
if (startDis > 10f) { // 两个手指并拢在一起的时候像素大于10
midPoint = mid(arg1);
//记录当前ImageView的缩放倍数
oldmatrix.set(img.getImageMatrix());
}
break;
}
img.setImageMatrix(newMatrix);
return true;
}
/** 计算两个手指间的距离 */
private float distance(MotionEvent event) {
float dx = event.getX(1) - event.getX(0);
float dy = event.getY(1) - event.getY(0);
/** 使用勾股定理返回两点之间的距离 */
return FloatMath.sqrt(dx * dx + dy * dy);
}
/** 计算两个手指间的中间点 */
private PointF mid(MotionEvent event) {
float midX = (event.getX(1)+event.getX(0))/2;
float midY = (event.getY(1)+event.getY(0))/2;
return new PointF(midX, midY);
}
});
}
}
//布局文件
<ImageView
android:scaleType="matrix"
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/haha"
/>