本文实例为大家分享了Android自定义View拖拽缩放矩形框的具体代码,供大家参考,具体内容如下
在开发项目中,需要一个矩形框来实现截屏功能,并且还需要可以任意拖拽和缩放,这就需要自定义View来实现了,具体功能如下:
1.自定义View
package com.xinrui.screenshot.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
public class CropRectView extends View {
// 绘制 损害框和损害名称
private Paint mPaint;
private RectF mRectF;
// 边缘字体
// private BorderedText mBorderedText;
// 标题 或 名字
private String mTitle;
// 概率
private float mConfidence;
// 矩形框 corner 的角度:直角、圆角
private int mCornerAngle;
//直角 默认
public static final int RIGHT_CORNER = 0;
//圆角
public static final int ROUND_CORNER = 1;
// Remove Rect
private int MODE;
private static final int MODE_OUTSIDE = 0x000000aa;/*170*/
private static final int MODE_INSIDE = 0x000000bb;/*187*/
private static final int MODE_POINT = 0X000000cc;/*204*/
private static final int MODE_ILLEGAL = 0X000000dd;/*221*/
private float startX;/*start X location*/
private float startY;/*start Y location*/
private float endX;/*end X location*/
private float endY;/*end Y location*/
private float currentX;/*X coordinate values while finger press*/
private float currentY;/*Y coordinate values while finger press*/
private float memoryX;/*the last time the coordinate values of X*/
private float memoryY;/*the last time the coordinate values of Y*/
private float mCoverWidth;/*width of selection box*/
private float mCoverHeight;/*height of selection box*/
private static final int ACCURACY = 100;/*touch accuracy*/
private int pointPosition;/*vertex of a rectangle*/
private static final float minWidth = 100.0f;/*the minimum width of the rectangle*/
private static final float minHeight = 200.0f;/*the minimum height of the rectangle*/
private onLocationListener mLocationListener;/*listen to the Rect */
private static final float EDGE_WIDTH = 1.8f;
public MoveAndCropRectView(Context context) {
this(context, null);
}
public MoveAndCropRectView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MoveAndCropRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initDatas(context);
}
priv