这篇文章主要介绍“android怎么实现简单的矩形裁剪框”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“android怎么实现简单的矩形裁剪框”文章能帮助大家解决问题。
正常模式是这样的
简单的添加了等比例裁剪
贴代码
public class CutView extends View {
float downX;
float downY;
boolean isLeft;
boolean isRight;
boolean isTop;
boolean isBottom;
boolean isMove;
boolean isSlideLeft;
boolean isSlideRight;
boolean isSlideTop;
boolean isSlideBottom;
float rectLeft;
float rectRight;
float rectTop;
float rectBottom;
private int measuredWidth;
private int measuredHeight;
private Paint paint;
private int dp3;
private int cornerLength;
private int dp1;
private float aspect = -1;
public CutView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public CutView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CutView(Context context) {
super(context);
init();
}
private void init() {
dp3 = (int) getResources().getDimension(R.dimen.dp3);
dp1 = (int) getResources().getDimension(R.dimen.dp1);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
if(downX >= rectLeft && downX <= rectRight && downY >= rectTop && downY <= rectBottom){
//判断手指的范围在左面还是右面
int w = (int) ((rectRight - rectLeft)/3);
if (downX >= rectLeft && downX <= rectLeft+w) {
isLeft = true;
} else if (downX <= rectRight && downX >= rectRight - w) {
isRight = true;
}
//判断手指的范围在上面还是下面
int h = (int) ((rectBottom - rectTop)/3);
if (downY >= rectTop && downY <= rectTop+h) {
isTop = true;
} else if (downY <= rectBottom && downY >= rectBottom - h) {
isBottom = true;
}
//如果手指范围没有在任何边界位置, 那么我们就认为用户是想拖拽框体
if (!isLeft && !isTop && !isRight && !isBottom) {
isMove = true;
}
}
break;
case MotionEvent.ACTION_MOVE:
float moveX = event.getX();
float moveY = event.getY();
//得到手指移动距离
float slideX = moveX - downX ;
float slideY = moveY - downY;
if (isMove) {//判断是否是拖拽模式
rectLeft += slideX;
rectRight += slideX;
rectTop += slideY;
rectBottom += slideY;
//同时改变left和right值, 达到左右移动的效果
if (rectLeft < 0 || rectRight > measuredWidth) {//判断x轴的移动边界
rectLeft -= slideX;
rectRight -= slideX;
}
//同时改变top和bottom值, 达到上下移动的效果
if (rectTop < 0 || rectBottom > measuredHeight ) {//判断y轴的移动边界
rectTop -= slideY;
rectBottom -= slideY;
}
//实时触发onDraw()方法
invalidate();
downX = moveX;
downY = moveY;
} else {
if(aspect != -1){
if(isLeft && (isTop || isBottom)){
if(!isSlideLeft && !isSlideTop && !isSlideBottom){
float x = Math.abs(slideX);
float y = Math.abs(slideY);
if(x > y && x > 10){
isSlideLeft = true;
}else if(x < y && y >10){
if(isTop){
isSlideTop = true;
}else{
isSlideBottom = true;
}
}
}
}else if (isRight && (isTop || isBottom)){
if(!isSlideRight && !isSlideTop && !isSlideBottom){
float x = Math.abs(slideX);
float y = Math.abs(slideY);
if(x > y && x > 10){
isSlideRight = true;
}else if(x < y && y >10){
if(isTop){
isSlideTop = true;
}else{
isSlideBottom = true;
}
}
}
}else if(isLeft && !isSlideLeft){
isSlideLeft = true;
}else if(isRight && !isSlideLeft){
isSlideRight = true;
}else if(isTop && !isSlideTop){
isSlideTop = true;
}else if(isBottom && !isSlideBottom){
isSlideBottom = true;
}
if (isSlideLeft) {
rectLeft += slideX;
if (rectLeft < 0) rectLeft = 0;
float w = rectRight - rectLeft;
if(w < cornerLength * 2){
w = cornerLength * 2;
rectLeft = rectRight - w;
}
float h = w/aspect;
if(h < cornerLength * 2){
h = cornerLength * 2;
w = h *aspect;
rectLeft = rectRight - w;
}
if(isTop){
rectBottom = rectTop + h;
}else if(isBottom){
rectTop = rectBottom - h;
}else{
float rh = rectBottom - rectTop;
float t = (rh - h)/2;
rectTop += t;
rectBottom -= t;
}
if(rectTop < 0){
rectTop = 0;
rectBottom = h;
if(rectBottom > measuredHeight){
rectBottom = measuredHeight;
}
w = rectBottom *aspect;
rectLeft = rectRight - w;
}else if(rectBottom > measuredHeight){
rectBottom = measuredHeight;
rectTop = measuredHeight - h;
if(rectTop < 0){
rectTop = 0;
}
w = (rectBottom - rectTop) *aspect;
rectLeft = rectRight - w;
}
invalidate();
downX = moveX;
downY = moveY;
} else if (isSlideRight) {
rectRight += slideX;
if (rectRight > measuredWidth )
rectRight = measuredWidth;
float w = rectRight - rectLeft;
if(w < cornerLength * 2){
w = cornerLength * 2;
rectRight = rectLeft + w;
}
float h = w/aspect;
if(h < cornerLength * 2){
h = cornerLength * 2;
w = h *aspect;
rectRight = rectLeft + w;
}
if(isTop){
rectBottom = rectTop + h;
}else if(isBottom){
rectTop = rectBottom - h;
}else{
float rh = rectBottom - rectTop;
float t = (rh - h)/2;
rectTop += t;
rectBottom -= t;
}
if(rectTop < 0){
rectTop = 0;
rectBottom = h;
if(rectBottom > measuredHeight){
rectBottom = measuredHeight;
}
w = rectBottom *aspect;
rectRight = rectLeft + w;
}else if(rectBottom > measuredHeight){
rectBottom = measuredHeight;
rectTop = measuredHeight - h;
if(rectTop < 0){
rectTop = 0;
}
w = (rectBottom - rectTop) *aspect;
rectRight = rectLeft + w;
}
invalidate();
downX = moveX;
downY = moveY;
}else if (isSlideTop) {
rectTop += slideY;
if (rectTop < 0) rectTop = 0;
float h = rectBottom - rectTop;
if(h < cornerLength * 2){
h = cornerLength * 2;
rectTop = rectBottom - h