当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义View实现照片裁剪框与照片裁剪功能

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

什么叫生辰八字,在线繁体,费尔教育网

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

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

实现思路:

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, 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); //在横纵轴上进行平移 
} 
} 
//进行绘制 
canvas.drawbitmap(sourcebitmap, matrix, 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, attributeset attrs) { 
super(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, int left, int top, int right, int bottom) { 
super.onlayout(changed, left, top, right, bottom); 
borderheight = this.getheight(); 
borderwith = this.getwidth(); 
//初始化四个点的坐标 
four_corner_coordinate_positions = new float[][]{ 
{(borderwith - borderlength) / 2, (borderheight - borderlength) / 2}, //左上 
{(borderwith + borderlength) / 2, (borderheight - borderlength) / 2}, //右上 
{(borderwith - borderlength) / 2, (borderheight + borderlength) / 2}, //左下 
{(borderwith + borderlength) / 2, (borderheight + 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, 
four_corner_coordinate_positions[0][1] - temp1, 
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, 
four_corner_coordinate_positions[2][1] + temp1, 
paintrect); 
canvas.drawline(four_corner_coordinate_positions[2][0] - temp1, 
four_corner_coordinate_positions[2][1] + 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, 
four_corner_coordinate_positions[1][1] - temp1, 
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, 
four_corner_coordinate_positions[3][1] + temp1, 
paintrect); 
canvas.drawline(four_corner_coordinate_positions[3][0] + temp1, 
four_corner_coordinate_positions[3][1] + 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) four_corner_coordinate_positions[0][1], 
(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实现照片裁剪框与照片裁剪功能,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网