本文介绍一个自定义的图片剪裁控件
该控件由另一篇博文:Android 图片拖拽、放大缩小的自定义控件 扩展而来
如图:
思路:在一个自定义View上绘制一张图片(参照前面提到的另一篇博文),在该自定义View上绘制一个自定义的FloatDrawable,也就是图中的浮层。绘制图片和FloatDrawable的交集的补集部分灰色阴影(这个其实很简单,就一句话)。在自定义View的touch中去处理具体的拖动事件和FloatDrawable的变换。图片的绘制和FloatDrawable的绘制以及变换最终其实就是在操作各自的Rect而已,Rect就是一个有矩形,有四个坐标,图片和FloatDrawable就是按照坐标去绘制的。
CropImageView.java
该类继承View
功能:在onDraw方法中画图片、浮层,处理touch事件,最后根据坐标对图片进行剪裁。
public class CropImageView extends View {
// 在touch重要用到的点,
private float mX_1 = 0;
private float mY_1 = 0;
// 触摸事件判断
private final int STATUS_SINGLE = 1;
private final int STATUS_MULTI_START = 2;
private final int STATUS_MULTI_TOUCHING = 3;
// 当前状态
private int mStatus = STATUS_SINGLE;
// 默认裁剪的宽高
private int cropWidth;
private int cropHeight;
// 浮层Drawable的四个点
private final int EDGE_LT = 1;
private final int EDGE_RT = 2;
private final int EDGE_LB = 3;
private final int EDGE_RB = 4;
private final int EDGE_MOVE_IN = 5;
private final int EDGE_MOVE_OUT = 6;
private final int EDGE_NONE = 7;
public int currentEdge = EDGE_NONE;
protected float oriRationWH = 0;
protected final float maxZoomOut = 5.0f;
protected final float minZoomIn = 0.333333f;
protected Drawable mDrawable;
protected FloatDrawable mFloatDrawable;
protected Rect mDrawableSrc = new Rect();// 图片Rect变换时的Rect
protected Rect mDrawableDst = new Rect();// 图片Rect
protected Rect mDrawableFloat = new Rect();// 浮层的Rect
protected boolean isFrist = true;
private boolean isTouchInSquare = true;
protected Context mContext;
public CropImageView(Context context) {
super(context);
init(context);
}
public CropImageView(Context context,