Android自定义View实现照片裁剪框与照片裁剪功能

Android自定义View实现照片裁剪框与照片裁剪功能,第1张

概述本文所需要实现的就是这样一种有逼格的效果:右上角加了个图片框,按下确定可以裁剪正方形区域里的图片并显示在右上角。

本文所需要实现的就是这样一种有逼格的效果:

右上角加了个图片框,按下确定可以裁剪正方形区域里的图片并显示在右上角。

实现思路:

1:首先需要自定义一个ZoomImageVIEw来显示我们需要的图片,这个VIEw需要让图片能够以合适的位置展现在当前布局的图片展示区域内(合适的位置值的是:如果图片长度大于屏幕,则压缩图片长度至屏幕宽度,高度等比压缩并居中显示,如果图片高度大于屏幕,则压缩图片高度至屏幕高度,长度等比压缩并居中显示。);

2:然后需要实现这个拖动的框框,该框框实现的功能有四点:拖动、扩大缩小、触摸时显示基准线、截图。

首先是布局设计image_details.xml:

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="55dp" androID:background="#323441"> <Imagebutton androID:ID="@+ID/certification_returnbtn" androID:layout_wIDth="55dp" androID:layout_height="55dp" androID:background="@androID:color/transparent" androID:padding="15dp" androID:scaleType="fitCenter" androID:src="@drawable/topbar_returnbtn"/> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_marginleft="10dp" androID:layout_toRightOf="@ID/certification_returnbtn" androID:gravity="center_vertical" androID:text="裁剪图片" androID:textcolor="@androID:color/white" androID:textSize="20sp"/> <!-- <Imagebutton androID:layout_wIDth="55dp" androID:layout_height="55dp" androID:layout_alignParentRight="true" androID:layout_marginRight="10dp" androID:background="@androID:color/transparent" androID:padding="16dp" androID:scaleType="fitCenter" androID:src="@drawable/ic_rotate_24dp"/>--> <ImageVIEw androID:ID="@+ID/testimg" androID:layout_alignParentRight="true" androID:layout_marginRight="10dp" androID:layout_wIDth="55dp" androID:layout_height="55dp"/> </relativeLayout> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1"> <com.whale.nangua.pubuliuzhaopianqiang.ZoomImageVIEw androID:ID="@+ID/zoom_image_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#303030"/> <com.whale.nangua.pubuliuzhaopianqiang.ChoiceborderVIEw androID:ID="@+ID/zoom_choiceborder_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/> <button androID:ID="@+ID/image_details_subbtn" androID:text="确定" androID:background="@drawable/image_details_submitbtn_shape" androID:layout_marginBottom="20dp" androID:layout_wIDth="180dp" androID:layout_height="40dp" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true"/> </relativeLayout> </linearLayout>

布局比较简单,两个VIEw互相层叠。

自定义图片大小控制视图:ZoomImageVIEw.java

代码注释很详细就不解释了。

package com.whale.nangua.pubuliuzhaopianqiang; import androID.content.Context; import androID.graphics.Bitmap; import androID.graphics.Canvas; import androID.graphics.Matrix; import androID.util.AttributeSet; import androID.util.Log; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; /** * Created by nangua on 2016/7/20. */ public class ZoomImageVIEw extends VIEw { /** * 初始化状态常量 */ public static final int STATUS_INIT = 1; /** * 用于对图片进行移动和缩放变换的矩阵 */ 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; /** * 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(); } } /** * 根据currentStatus的值来决定对图片进行什么样的绘制 *** 作。 */ @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); initBitmap(canvas); canvas.drawBitmap(sourceBitmap,matrix,null); } float translateY;//Y轴偏移量 float translateX;//X轴偏移量 /** * @param canvas * @autohr nangua * 对图片进行初始化 *** 作,包括让图片居中,以及当图片大于屏幕宽高时对图片进行压缩。 * <p> * 1.当图片宽度大于显示宽度、图片高度小于显示宽度: * 设置图片宽度为显示宽度,高度缩放*(图片宽度/显示宽度) * <p> * 2.当图片宽度小于显示宽度、图片高度大于显示宽度: * 设置图片高度为显示高度,宽度缩放*(图片高度/显示高 度) * <p> * 3.当图片宽度大于显示宽度,图片高度大于显示宽度: * 选取差度更大的一边进行压缩,另一边等比缩放 * <p> * 4.当图片宽度小于显示宽度,图片高度小于显示宽度: * 选取差度更大的一边进行压缩,另一边等比缩放 */ private voID initBitmap(Canvas canvas) { if (sourceBitmap != null) { matrix.reset(); //重置矩阵 int bitmapWIDth = sourceBitmap.getWIDth(); //得到源图片宽 int bitmapHeight = sourceBitmap.getHeight(); //得到源图片高 //如果原图片大小大于控件宽高 if (bitmapWIDth > wIDth || bitmapHeight > height) { //如果宽和高都比屏幕大,选择差度大的一边缩小,另一边等比缩小 if (bitmapWIDth > wIDth && bitmapHeight > height) { int distanceX = Math.abs(wIDth - bitmapWIDth); int distanceY = Math.abs(height - bitmapHeight); float ratio; //找出差值大的一边,进行缩小 if (distanceX >= distanceY) { ratio = wIDth / (bitmapWIDth * 1.0f); matrix.postscale(ratio,ratio); //此时横轴铺满,只需要对竖轴进行平移 translateY = (height - sourceBitmap.getHeight() * ratio) / 2f; matrix.postTranslate(0,translateY); } else { ratio = height / (bitmapHeight * 1.0f); matrix.postscale(ratio,ratio); //此时竖轴铺满,只需要对横轴进行平移 translateX = (wIDth - sourceBitmap.getWIDth() * ratio) / 2f; matrix.postTranslate(translateX,0); //在横纵轴上进行平移 } //当图片宽度大于显示宽度、图片高度小于显示宽度: } else if (bitmapWIDth > wIDth) { // 当图片宽度大于屏幕宽度时,将图片等比例压缩,使它可以完全显示出来 float ratio = wIDth / (bitmapWIDth * 1.0f); //压缩比例 matrix.postscale(ratio,ratio); translateY = (height - (bitmapHeight * ratio)) / 2f; // 在纵坐标方向上进行偏移,以保证图片居中显示 matrix.postTranslate(0,translateY); //当图片宽度小于显示宽度、图片高度大于显示宽度: } else if (bitmapHeight > height) { // 当图片高度大于屏幕高度时,将图片等比例压缩,使它可以完全显示出来 float ratio = height / (bitmapHeight * 1.0f); //压缩比例 matrix.postscale(ratio,ratio); translateX = (wIDth - (bitmapWIDth * ratio)) / 2f; // 在横坐标方向上进行偏移,以保证图片居中显示 matrix.postTranslate(translateX,0); } } else { // 当图片的宽高都小于屏幕宽高时,选择差度小的一边铺满,另一边等比扩大 //计算长和宽的差值 int distanceX = Math.abs(wIDth - bitmapWIDth); int distanceY = Math.abs(height - bitmapHeight); float ratio; //找出差值小的一边,进行扩大 if (distanceX <= distanceY) { ratio = wIDth / (bitmapWIDth * 1.0f); matrix.postscale(ratio,0); //在横纵轴上进行平移 } } //进行绘制 canvas.drawBitmap(sourceBitmap,null); } } }

重点来了,相册选取框视图:ChoiceborderVIEw.java

package com.whale.nangua.pubuliuzhaopianqiang; import androID.content.Context; import androID.graphics.Canvas; import androID.graphics.color; import androID.graphics.Paint; import androID.util.AttributeSet; import androID.util.Log; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.Widget.Toast; /** * 相册选择框的VIEw * Created by nangua on 2016/7/21. */ public class ChoiceborderVIEw extends VIEw { private int scale = (int) this.getResources().getdisplayMetrics().density; //屏幕像素密度 private float borderHeight; //总高 private float borderWith; //总宽 private float borderLength = 200 * scale; //边框长度 private int RECT_border_WITH = 3 * scale; //长方形框框粗 private int RECT_CORNER_WITH = 6 * scale; //四个角的粗 private int RECT_CORNER_HEIGHT = 20 * scale; //四个角的长度 //四个点坐标 private static float[][] four_corner_coordinate_positions; private static int Now_MOVE_STATE = 1; //移动状态,默认为1,Y轴=1,X轴=2 private static boolean MOVE_OR_ZOOM_STATE = true; //移动或缩放状态, true 为移动 public ChoiceborderVIEw(Context context,attrs); this.setFocusable(true); this.setFocusableIntouchMode(true); init(); } /** * 初始化布局 * @param changed * @param left * @param top * @param right * @param bottom */ @OverrIDe protected voID onLayout(boolean changed,bottom); borderHeight = this.getHeight(); borderWith = this.getWIDth(); //初始化四个点的坐标 four_corner_coordinate_positions = new float[][]{ {(borderWith - borderLength) / 2,(borderHeight - borderLength) / 2},//左上 {(borderWith + borderLength) / 2,//右上 {(borderWith - borderLength) / 2,(borderHeight + borderLength) / 2},//左下 {(borderWith + borderLength) / 2,//右上 }; } private voID init() { } private int temp1 = (RECT_CORNER_WITH - RECT_border_WITH) / 2; //长方形的粗半距 private int temp2 = (RECT_CORNER_WITH + RECT_border_WITH) / 2; //四个角的粗半距 /** * RECT_CORNER_WITH = 6 * RECT_border_WITH =3 * * @param canvas */ @OverrIDe protected voID onDraw(Canvas canvas) { Paint paintRect = new Paint(); //初始化画笔 //画边框的画笔 paintRect.setcolor(getResources().getcolor(R.color.bordercolor)); //颜色 paintRect.setstrokeWIDth(RECT_border_WITH); //宽度 paintRect.setAntiAlias(true); //抗锯齿 paintRect.setStyle(Paint.Style.stroke); //设置空心 canvas.drawRect(four_corner_coordinate_positions[0][0],four_corner_coordinate_positions[0][1],four_corner_coordinate_positions[3][0],four_corner_coordinate_positions[3][1],paintRect); //画四个角的画笔 paintRect.setcolor(color.WHITE); paintRect.setstrokeWIDth(RECT_CORNER_WITH); paintRect.setAntiAlias(true); //左上角的两根 canvas.drawline(four_corner_coordinate_positions[0][0] - temp2,four_corner_coordinate_positions[0][1] - temp1,four_corner_coordinate_positions[0][0] - temp1 + RECT_CORNER_HEIGHT,paintRect); canvas.drawline(four_corner_coordinate_positions[0][0] - temp1,four_corner_coordinate_positions[0][1] - temp2,four_corner_coordinate_positions[0][0] - temp1,four_corner_coordinate_positions[0][1] - temp1 + RECT_CORNER_HEIGHT,paintRect); //左下角的两根 canvas.drawline(four_corner_coordinate_positions[2][0] - temp2,four_corner_coordinate_positions[2][1] + temp1,four_corner_coordinate_positions[2][0] - temp1 + RECT_CORNER_HEIGHT,paintRect); canvas.drawline(four_corner_coordinate_positions[2][0] - temp1,four_corner_coordinate_positions[2][0] - temp1,four_corner_coordinate_positions[2][1] + temp1 - RECT_CORNER_HEIGHT,paintRect); //右上角的两根 canvas.drawline(four_corner_coordinate_positions[1][0] + temp1,four_corner_coordinate_positions[1][1] - temp1,four_corner_coordinate_positions[1][0] + temp1 - RECT_CORNER_HEIGHT,paintRect); canvas.drawline(four_corner_coordinate_positions[1][0] + temp1,four_corner_coordinate_positions[1][1] - temp2,four_corner_coordinate_positions[1][0] + temp1,four_corner_coordinate_positions[1][1] - temp1 + RECT_CORNER_HEIGHT,paintRect); //右下角的两根 canvas.drawline(four_corner_coordinate_positions[3][0] + temp2,four_corner_coordinate_positions[3][1] + temp1,four_corner_coordinate_positions[3][0] + temp1 - RECT_CORNER_HEIGHT,paintRect); canvas.drawline(four_corner_coordinate_positions[3][0] + temp1,four_corner_coordinate_positions[3][0] + temp1,four_corner_coordinate_positions[3][1] + temp1 - RECT_CORNER_HEIGHT,paintRect); //画扫描线 if (IF_SCANNING_SHOW) { paintRect.setcolor(color.WHITE); paintRect.setstrokeWIDth(1); paintRect.setAntiAlias(true); paintRect.setStyle(Paint.Style.stroke); //共四根线 //竖1 canvas.drawline(four_corner_coordinate_positions[0][0] + borderLength / 3,four_corner_coordinate_positions[0][1] + temp1,four_corner_coordinate_positions[2][0] + borderLength / 3,four_corner_coordinate_positions[2][1] - temp1,paintRect); //竖2 canvas.drawline(four_corner_coordinate_positions[1][0] - borderLength / 3,four_corner_coordinate_positions[1][1] + temp1,four_corner_coordinate_positions[3][0] - borderLength / 3,four_corner_coordinate_positions[3][1] - temp1,paintRect); //横1 canvas.drawline(four_corner_coordinate_positions[0][0] + temp1,four_corner_coordinate_positions[0][1] + borderLength / 3,four_corner_coordinate_positions[1][0] - temp1,four_corner_coordinate_positions[1][1] + borderLength / 3,paintRect); //横2 canvas.drawline(four_corner_coordinate_positions[2][0] + temp1,four_corner_coordinate_positions[2][1] - borderLength / 3,four_corner_coordinate_positions[3][0] - temp1,four_corner_coordinate_positions[3][1] - borderLength / 3,paintRect); } } private boolean IF_SCANNING_SHOW = false; private int lastX = 0; //上次按下的X位置 private int lastY = 0; //上次按下的Y位置 private int offsetX = 0; //X轴偏移量 private int offsetY = 0; //Y轴偏移量 static int point = -1;// 用户按下的点 private int POINT_STATE = -1; //判断用户是缩小还是放大 0放大 1缩小 @OverrIDe public boolean ontouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: IF_SCANNING_SHOW = true;//显示扫描线 if (isInTheCornerCircle(event.getX(),event.getY()) != -1) { //开始缩放 *** 作 MOVE_OR_ZOOM_STATE = false; //设置false为缩放状态 point = isInTheCornerCircle(event.getX(),event.getY()); } lastX = x; lastY = y; invalIDate(); break; case MotionEvent.ACTION_MOVE: offsetX = x - lastX; offsetY = y - lastY; //判断当前是扩大还是缩小 *** 作 judgementXandY(); //限定移动范围 //移动状态:只有在移动状态下才能移动 if (MOVE_OR_ZOOM_STATE) { getoffsetXandoffsetY(); //四个点的坐标信息也要随之改变 for (int i = 0; i < four_corner_coordinate_positions.length; i++) { four_corner_coordinate_positions[i][0] += offsetX; four_corner_coordinate_positions[i][1] += offsetY; //更新回调接口 onImageDetailsSizeChanggedl.onborderSizeChangged( (int) four_corner_coordinate_positions[0][0],(int) four_corner_coordinate_positions[0][1],(int) borderLength ); invalIDate(); } // this.scrollBy(-offsetX,-offsetY); //这里弃用,后面改用了四点坐标移动代替背景移动 } //在缩放状态下 else { //按住某一个点,该点的坐标改变,其他2个点坐标跟着改变,对点坐标不变 max = Math.abs(offsetX) >= Math.abs(offsetY) ? Math.abs(offsetX) : Math.abs(offsetY); //只有在扩大 *** 作才进行边界范围判断 if (POINT_STATE == 0) { getoffsetXandoffsetY(); //边界范围判断 } //缩小 *** 作时进行边界不能太小判断 else if (POINT_STATE == 1) { //如果边长+max太小,直接返回 if (borderLength - max <= RECT_CORNER_HEIGHT*2-temp2) { max = 0; } } //改变坐标 changgeFourCoodinateposition(point,offsetX,offsetY); //更新边长 notifyNowborderLength(); //更新回调接口 onImageDetailsSizeChanggedl.onborderSizeChangged( (int) four_corner_coordinate_positions[0][0],(int) borderLength ); invalIDate(); } lastX = x; lastY = y; break; case MotionEvent.ACTION_UP: IF_SCANNING_SHOW = false; //不显示扫描线 MOVE_OR_ZOOM_STATE = true; //回归为默认的移动状态 invalIDate(); break; } return true; } /** * 更新矩形框边长的方法 */ private voID notifyNowborderLength() { float a = four_corner_coordinate_positions[0][0]; float b = four_corner_coordinate_positions[0][1]; float c = four_corner_coordinate_positions[1][0]; float d = four_corner_coordinate_positions[1][1]; float temp1 = (float) Math.pow(a - c,2); float temp2 = (float) Math.pow(b - d,2); borderLength = (float) Math.sqrt(temp1 + temp2); } /** * POINT_STATE 为0放大, 1缩小 */ private voID judgementXandY() { switch (point) { case 0: if ((offsetX <= 0 && offsetY <= 0) || (offsetX <= 0 && offsetY >= 0)) { POINT_STATE = 0;//扩大 } else { POINT_STATE = 1;//缩小 } break; case 1: if ((offsetX >= 0 && offsetY <= 0) || (offsetX >= 0 && offsetY >= 0)) { POINT_STATE = 0; } else { POINT_STATE = 1; } break; case 2: if ((offsetX <= 0 && offsetY >= 0) || (offsetX <= 0 && offsetY <= 0)) { POINT_STATE = 0; } else { POINT_STATE = 1; } break; case 3: if ((offsetX >= 0 && offsetY >= 0) || (offsetX >= 0 && offsetY <= 0)) { POINT_STATE = 0; } else { POINT_STATE = 1; } break; } } /** * 防止X和Y溢出边界的算法 */ private voID getoffsetXandoffsetY() { //如果是移动状态 if (MOVE_OR_ZOOM_STATE) { if ((four_corner_coordinate_positions[0][0] + offsetX <= 0) || (four_corner_coordinate_positions[1][0] + offsetX >= borderWith) ) { offsetX = 0; } if ((four_corner_coordinate_positions[0][1] + offsetY <= 0) || (four_corner_coordinate_positions[2][1] + offsetY >= borderHeight) ) { offsetY = 0; } } //如果是缩放状态 else { switch (point) { case 0: if ((four_corner_coordinate_positions[0][0] - max <= 0) || (four_corner_coordinate_positions[0][1] - max <= 0) ) { max = 0; } break; case 1: if ((four_corner_coordinate_positions[1][0] + max >= borderWith) || (four_corner_coordinate_positions[1][1] - max <= 0) ) { max = 0; } break; case 2: if ((four_corner_coordinate_positions[2][0] - max <= 0) || (four_corner_coordinate_positions[2][1] + max >= borderHeight) ) { max = 0; } break; case 3: if ((four_corner_coordinate_positions[3][0] + max >= borderWith) || (four_corner_coordinate_positions[3][1] + max >= borderHeight) ) { max = 0; } break; } } } static int max; /** * 扩大缩放方法 * 根据用户传来的点改变其他点的坐标 * 按住某一个点,该点的坐标改变,其他2个点坐标跟着改变,对点坐标不变 * 点阵示意: * 0 1 * 2 3 * * @param point 用户按的点 * @param offsetX X轴偏移量 * @param offsetY Y轴偏移量 */ private voID changgeFourCoodinateposition(int point,int offsetX,int offsetY) { switch (point) { case 0: if (offsetX > 0 && offsetY < 0) { //变化0点的位置 suoxiao four_corner_coordinate_positions[0][0] += max; four_corner_coordinate_positions[0][1] += max; //变化1点的Y轴 four_corner_coordinate_positions[1][1] += max; //变化2点的X轴 four_corner_coordinate_positions[2][0] += max; } else if (offsetX < 0 && offsetY > 0) { //变化0点的位置 kuoda four_corner_coordinate_positions[0][0] -= max; four_corner_coordinate_positions[0][1] -= max; //变化1点的Y轴 four_corner_coordinate_positions[1][1] -= max; //变化2点的X轴 four_corner_coordinate_positions[2][0] -= max; } else if (offsetX < 0 && offsetY < 0) { //变化0点的位置 kuoda four_corner_coordinate_positions[0][0] -= max; four_corner_coordinate_positions[0][1] -= max; //变化1点的Y轴 four_corner_coordinate_positions[1][1] -= max; //变化2点的X轴 four_corner_coordinate_positions[2][0] -= max; } else if (offsetX > 0 && offsetY > 0) { //变化0点的位置 suoxiao four_corner_coordinate_positions[0][0] += max; four_corner_coordinate_positions[0][1] += max; //变化1点的Y轴 four_corner_coordinate_positions[1][1] += max; //变化2点的X轴 four_corner_coordinate_positions[2][0] += max; } break; case 1: if (offsetX > 0 && offsetY < 0) { //变化1点的位置 four_corner_coordinate_positions[1][0] += max; four_corner_coordinate_positions[1][1] -= max; //变化0点的Y轴 four_corner_coordinate_positions[0][1] -= max; //变化3点的X轴 four_corner_coordinate_positions[3][0] += max; } else if (offsetX < 0 && offsetY > 0) { //变化1点的位置 four_corner_coordinate_positions[1][0] -= max; four_corner_coordinate_positions[1][1] += max; //变化0点的Y轴 four_corner_coordinate_positions[0][1] += max; //变化3点的X轴 four_corner_coordinate_positions[3][0] -= max; } else if (offsetX < 0 && offsetY < 0) { //变化1点的位置 four_corner_coordinate_positions[1][0] -= max; four_corner_coordinate_positions[1][1] += max; //变化0点的Y轴 four_corner_coordinate_positions[0][1] += max; //变化3点的X轴 four_corner_coordinate_positions[3][0] -= max; } else if (offsetX > 0 && offsetY > 0) { //变化1点的位置 four_corner_coordinate_positions[1][0] += max; four_corner_coordinate_positions[1][1] -= max; //变化0点的Y轴 four_corner_coordinate_positions[0][1] -= max; //变化3点的X轴 four_corner_coordinate_positions[3][0] += max; } break; case 2: if (offsetX > 0 && offsetY < 0) { //变化2点的位置 suoxiao four_corner_coordinate_positions[2][0] += max; four_corner_coordinate_positions[2][1] -= max; //变化0点的X轴 four_corner_coordinate_positions[0][0] += max; //变化3点的Y轴 four_corner_coordinate_positions[3][1] -= max; } else if (offsetX < 0 && offsetY > 0) { //变化2点的位置 kuoda four_corner_coordinate_positions[2][0] -= max; four_corner_coordinate_positions[2][1] += max; //变化0点的X轴 four_corner_coordinate_positions[0][0] -= max; //变化3点的Y轴 four_corner_coordinate_positions[3][1] += max; } else if (offsetX < 0 && offsetY < 0) { //变化2点的位置 kuoda four_corner_coordinate_positions[2][0] -= max; four_corner_coordinate_positions[2][1] += max; //变化0点的X轴 four_corner_coordinate_positions[0][0] -= max; //变化3点的Y轴 four_corner_coordinate_positions[3][1] += max; } else if (offsetX > 0 && offsetY > 0) { //变化2点的位置 suoxiao four_corner_coordinate_positions[2][0] += max; four_corner_coordinate_positions[2][1] -= max; //变化0点的X轴 four_corner_coordinate_positions[0][0] += max; //变化3点的Y轴 four_corner_coordinate_positions[3][1] -= max; } break; case 3: if (offsetX > 0 && offsetY < 0) { //变化3点的位置 kuoda four_corner_coordinate_positions[3][0] += max; four_corner_coordinate_positions[3][1] += max; //变化1点的X轴 four_corner_coordinate_positions[1][0] += max; //变化2点的Y轴 four_corner_coordinate_positions[2][1] += max; } else if (offsetX < 0 && offsetY > 0) { //变化3点的位置 suoxiao four_corner_coordinate_positions[3][0] -= max; four_corner_coordinate_positions[3][1] -= max; //变化1点的X轴 four_corner_coordinate_positions[1][0] -= max; //变化2点的Y轴 four_corner_coordinate_positions[2][1] -= max; } else if (offsetX < 0 && offsetY < 0) { //变化3点的位置 suoxiao four_corner_coordinate_positions[3][0] -= max; four_corner_coordinate_positions[3][1] -= max; //变化1点的X轴 four_corner_coordinate_positions[1][0] -= max; //变化2点的Y轴 four_corner_coordinate_positions[2][1] -= max; } else if (offsetX > 0 && offsetY > 0) { //变化3点的位置 kuoda four_corner_coordinate_positions[3][0] += max; four_corner_coordinate_positions[3][1] += max; //变化1点的X轴 four_corner_coordinate_positions[1][0] += max; //变化2点的Y轴 four_corner_coordinate_positions[2][1] += max; } break; } } /** * 判断按下的点在圆圈内 * * @param x 按下的X坐标 * @param y 按下的Y坐标 * @return 返回按到的是哪个点,没有则返回-1 * 点阵示意: * 0 1 * 2 3 */ private int isInTheCornerCircle(float x,float y) { for (int i = 0; i < four_corner_coordinate_positions.length; i++) { float a = four_corner_coordinate_positions[i][0]; float b = four_corner_coordinate_positions[i][1]; float temp1 = (float) Math.pow((x - a),2); float temp2 = (float) Math.pow((y - b),2); if (((float) RECT_CORNER_HEIGHT) >= Math.sqrt(temp1 + temp2)) { return i; } } return -1; } public interface onImageDetailsSizeChangged { voID onborderSizeChangged(int x,int y,int length); } public onImageDetailsSizeChangged onImageDetailsSizeChanggedl; public voID setonImageDetailsSizeChangged(onImageDetailsSizeChangged onImageDetailsSizeChangged) { this.onImageDetailsSizeChanggedl = onImageDetailsSizeChangged; } } 

以上所述是小编给大家介绍的AndroID自定义view实现照片裁剪框与照片裁剪功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android自定义View实现照片裁剪框与照片裁剪功能全部内容,希望文章能够帮你解决Android自定义View实现照片裁剪框与照片裁剪功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1148768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存