如果你还没有看过 Android瀑布流照片墙实现,体验不规则排列的美感 这篇文章,请尽量先去阅读完再来看本篇文章,因为这次的代码完全是在上次的基础上进行开发的。
相关阅读:
本文源码下载:
具体下载目录在 /2013年资料/9月/23日/Android多点触控技术实战,自由地对图片进行缩放和移动
那我们现在就开始动手吧,首先打开上次的PhotoWallFallsDemo项目,在里面加入一个ZoomImageView类,这个类就是用于进行大图展示和多点触控缩放的,代码如下所示:
public class ZoomImageView extends View {
/**
* 初始化状态常量
*/
public static final int STATUS_INIT = 1;
/**
* 图片放大状态常量
*/
public static final int STATUS_ZOOM_OUT = 2;
/**
* 图片缩小状态常量
*/
public static final int STATUS_ZOOM_IN = 3;
/**
* 图片拖动状态常量
*/
public static final int STATUS_MOVE = 4;
/**
* 用于对图片进行移动和缩放变换的矩阵
*/
private Matrix matrix = new Matrix();
/**
* 待展示的Bitmap对象
*/
private Bitmap sourceBitmap;
/**
* 记录当前操作的状态,可选值为STATUS_INIT、STATUS_ZOOM_OUT、STATUS_ZOOM_IN和STATUS_MOVE
*/
private int currentStatus;
/**
* ZoomImageView控件的宽度
*/
private int width;
/**
* ZoomImageView控件的高度
*/
private int height;
/**
* 记录两指同时放在屏幕上时,中心点的横坐标值
*/
private float centerPointX;
/**
* 记录两指同时放在屏幕上时,中心点的纵坐标值
*/
private float centerPointY;
/**
* 记录当前图片的宽度,图片被缩放时,这个值会一起变动
*/
private float currentBitmapWidth;
/**
* 记录当前图片的高度,图片被缩放时,这个值会一起变动
*/
private float currentBitmapHeight;
/**
* 记录上次手指移动时的横坐标
*/
private float lastXMove = -1;
/**
* 记录上次手指移动时的纵坐标
*/
private float lastYMove = -1;
/**
* 记录手指在横坐标方向上的移动距离
*/
private float movedDistanceX;
/**
* 记录手指在纵坐标方向上的移动距离
*/
private float movedDistanceY;
/**
* 记录图片在矩阵上的横向偏移值
*/
private float totalTranslateX;
/**
* 记录图片在矩阵上的纵向偏移值
*/
private float totalTranslateY;
/**
* 记录图片在矩阵上的总缩放比例
*/
private float totalRatio;
/**
* 记录手指移动的距离所造成的缩放比例
*/
private float scaledRatio;
/**
* 记录图片初始化时的缩放比例
*/
private float initRatio;
/**
* 记录上次两指之间的距离
*/
private double lastFingerDis;
/**
* ZoomImageView构造函数,将当前操作状态设为STATUS_INIT。
*
* @param context
* @param attrs
*/
public ZoomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
currentStatus = STATUS_INIT;
}
/**
* 将待展示的图片设置进来。
*
* @param bitmap
* 待展示的Bitmap对象
*/
public void setImageBitmap(Bitmap bitmap) {
sourceBitmap = bitmap;
invalidate();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed) {
// 分别获取到ZoomImageView的宽度和高度
width = getWidth();
height = getHeight();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
if (event.getPointerCount() == 2) {
// 当有两个手指按在屏幕上时,计算两指之间的距离
lastFingerDis = distanceBetweenFingers(event);
}
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 1) {
// 只有单指按在屏幕上移动时,为拖动状态
float xMove = event.getX();
float yMove = event.getY();
if (lastXMove == -1 && lastYMove == -1) {
lastXMove = xMove;
lastYMove = yMove;
}
currentStatus = STATUS_MOVE;
movedDistanceX = xMove - lastXMove;
movedDistanceY = yMove - lastYMove;
// 进行边界检查,不允许将图片拖出边界
if (totalTranslateX + movedDistanceX > 0) {
movedDistanceX = 0;
} else if (width - (totalTranslateX + movedDistanceX) > currentBitmapWidth) {
movedDistanceX = 0;
}
if (totalTranslateY + movedDistanceY > 0) {
movedDistanceY = 0;
} else if (height - (totalTranslateY + movedDistanceY) > currentBitmapHeight) {
movedDistanceY = 0;
}
// 调用onDraw()方法绘制图片
invalidate();
lastXMove = xMove;
lastYMove = yMove;