android——8点控制裁剪框

        原创文章——转载请注明

        之前的图片裁剪功能是4点控制的,来源为他人的博客。目前有点时间,将其扩展为8控制点裁剪。

         先看一下效果:

        


         关键在于控制点的绘制,触屏时控制点的判断(一定半径的圆形范围),扩大缩小的判断,缩小的最小量判断(四角的点用对角线,中间点用长宽),移动边界越界的判断,放大缩小时控制点的重绘。


         代码很简单,注释也充足,就不多解释了,上代码

        

public class ChoiceMatrixView extends View {

	private static final String TAG = "ChoiceMatrixView";
	
	private int scale = (int) this.getResources().getDisplayMetrics().density;  //屏幕像素密度
    private float borderHeight;   //总高
    private float borderWith; //总宽
    private float imageHeight; //图片高度
    private float imageWidth; //图片宽度
    private float imageTranslateX; //由于图片是居中的,需要考虑x和y方向的偏移量
    private float imageTranslateY; //由于图片是居中的,需要考虑x和y方向的偏移量
    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; //四个角的长度

    //四个点坐标
    public 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 ChoiceMatrixView(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);
        
        //总高和总宽需要综合考虑view的宽和高和图片的宽和高
        //但是由于异步机制,无法在onLayout之前获得偏移量,因此图片溢出改为消息提醒
        /*
        if(imageHeight==0){
        	borderHeight = this.getHeight();
        }else{
        	if(imageHeight<this.getHeight())
        		borderHeight=imageHeight;
        	else
        		borderHeight = this.getHeight()+getTranslationY();
        }
        if(imageWidth==0){
        	borderWith = this.getWidth();
        }else{
        	if(imageWidth<this.getWidth())
        		borderWith=imageWidth;
        	else
        		borderWith = this.getWidth()+getTranslationX();
        }
        */
        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}, //右上
                {(borderWith - borderLength) / 2 + borderLength / 2, (borderHeight - borderLength) / 2}, //上中
                {(borderWith + borderLength) / 2, (borderHeight - borderLength) / 2 + borderLength / 2}, //右中
                {(borderWith - borderLength) / 2, (borderHeight - borderLength) / 2 + borderLength / 2}, //左中
                {(borderWith - borderLength) / 2 + borderLength / 2, (borderHeight + borderLength) / 2}  //下中
        };
    }

    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.darker_gray));    //颜色
        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);
        
        //上中
        canvas.drawLine(four_corner_coordinate_positions[4][0]- temp2 - RECT_CORNER_HEIGHT,
                four_corner_coordinate_positions[0][1] - temp1,
                four_corner_coordinate_positions[4][0] + (temp1 + RECT_CORNER_HEIGHT),
                four_corner_coordinate_positions[0][1] - temp1,
                paintRect);
        
        //右中
        canvas.drawLine(four_corner_coordinate_positions[3][0],
                four_corner_coordinate_positions[6][1] - temp2 - RECT_CORNER_HEIGHT,
                four_corner_coordinate_positions[3][0],
                four_corner_coordinate_positions[6][1] + temp1 + RECT_CORNER_HEIGHT,
                paintRect);
        
        //左中
        canvas.drawLine(four_corner_coordinate_positions[0][0],
                four_corner_coordinate_positions[6][1] - temp2 - RECT_CORNER_HEIGHT,
                four_corner_coordinate_positions[0][0],
                four_corner_coordinate_positions[6][1] + temp1 + RECT_CORNER_HEIGHT,
                paintRect);
        
        //下中
        canvas.drawLine(four_corner_coordinate_positions[4][0] - temp2 - RECT_CORNER_HEIGHT,
                four_corner_coordinate_positions[3][1] + temp1,
                four_corner_coordinate_positions[4][0] + temp1 + RECT_CORNER_HEIGHT,
                four_corner_c
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Image Cropper 是一个开源的图片裁剪架,它可以让你在 Android 应用中实现图片裁剪功能。使用 Android Image Cropper,你可以让用户在任何地方选择一张图片并对其进行裁剪。该架支持手势缩放、旋转、裁剪大小调整等功能。 Android Image Cropper 的主要功能如下: - 矩形和圆形裁剪 - 支持手势缩放、旋转、裁剪大小调整 - 支持设置裁剪宽高比、最大最小裁剪大小、裁剪结果大小等 - 支持设置裁剪结果压缩质量、格式等 - 支持自定义裁剪界面 下面是使用 Android Image Cropper 的基本步骤: 1. 在 build.gradle 文件中添加以下依赖项: ``` implementation 'com.github.yalantis:ucrop:2.2.6' ``` 2. 在你的Activity中,启动裁剪界面: ``` // 从相册中选择一张图片进行裁剪 UCrop.of(sourceUri, destinationUri) .withAspectRatio(16, 9) .withMaxResultSize(maxWidth, maxHeight) .start(this); // 或者从相机中拍照裁剪 UCrop.of(sourceUri, destinationUri) .withAspectRatio(16, 9) .withMaxResultSize(maxWidth, maxHeight) .start(this); ``` 3. 在 onActivityResult() 方法中获取裁剪结果: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == UCrop.REQUEST_CROP && resultCode == RESULT_OK) { final Uri resultUri = UCrop.getOutput(data); // 处理裁剪结果 } else if (resultCode == UCrop.RESULT_ERROR) { final Throwable cropError = UCrop.getError(data); // 处理裁剪错误 } } ``` 希望这些信息能够帮助你快速了解 Android Image Cropper。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值