首页 > 代码库 > ImageView根据手势进行缩放,拖动
ImageView根据手势进行缩放,拖动
package com.example.scale;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* @author wuxif_000 ImageView根据手势进行缩放,拖动........
*
*/
public class TouchImageView extends ImageView {
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
/**
* 对imageView进行上、下、左、右拖动..........
*/
static final int DRAG = 1;
/**
* 对imageView进行缩放...........
*/
static final int ZOOM = 2;
int mode = NONE;
/**
* MotionEvent.ACTION_DOWN的坐标
*/
PointF start = new PointF();
/**
* 第一次感觉到两点的时候,获取两点间的中点坐标,以此点进行缩放,如果是多点触控的话........
*/
PointF mid = new PointF();
/**
* 第一次感觉到两点的时候,两点间的距离
*/
float oldDistance = 1f;
/**
* 对图片进行缩放的最大比例
*/
private final int maxScale=5;
public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//第一个手指down的时候
matrix.set(getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
//第二个手指down时,两点间的距离.......
oldDistance=spacing(event);
if (oldDistance > 10f) {
matrix.set(savedMatrix);
//保存两点间的中点........
midPoint(mid, event);
//对图片进行缩放
mode = ZOOM;
}
break;
case MotionEvent.ACTION_MOVE:
if(mode==DRAG){
matrix.set(savedMatrix);
//一个手指进行上、下、左、右拖动...........
//x,y移动的距离计算...........
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
}else if(mode==ZOOM){
//对imageView进行缩放........
//两点之间的距离
float newDistance = spacing(event);
//如果两点间的距离>10就进行缩放..........
if (newDistance > 10f) {
matrix.set(savedMatrix);
//缩放最大为5
float f = newDistance / oldDistance;
float scale = f>maxScale?maxScale:f;
//在最初两点间的中点,以该点进行缩放........
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_UP:
mode=NONE;
//点击事件处理,旋转180度............
if (Math.abs(event.getX()-start.x) < 10 & Math.abs(event.getY()-start.y) < 10) {
matrix.set(savedMatrix);
matrix.postRotate(180, getWidth()/2, getHeight()/2);
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode=NONE;
break;
default:
break;
}
setScaleType(ImageView.ScaleType.MATRIX);
setImageMatrix(matrix);
//return super.onTouchEvent(event);
//要设为true,不然setImageMatrix(matrix)不起作用
//要设为true,不然setImageMatrix(matrix)不起作用
//要设为true,不然setImageMatrix(matrix)不起作用
return true;
}
/**
* 两点的距离 Determine the space between the first two fingers
*/
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);
}
/**
* 两点的中点 Calculate the mid point of the first two fingers
* */
private void midPoint(PointF point, MotionEvent event) {
//1,2的中点是(1+2) /2=1.5
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
ImageView根据手势进行缩放,拖动