Android自定义裁剪图片的View

2024-08-22 18:18

本文主要是介绍Android自定义裁剪图片的View,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前些天,分析了开源框架zxing的源码。里边有一个自定义的view,扫描界面的蒙层。这给我做自定义裁剪图片view的一些启发。因为,其实原理很相似,都是中间显示的图片没有被遮盖,四周有蒙层的效果。接下来,就按照这个思路实现这个自定义的view。

上图

预览图

效果分析

1 刚进来,设置蒙层刚好包裹图片显示的大小

2 当触摸的是边界的时候,增大蒙层的范围,即缩小透明区域的大小。

图片1

3 当触摸的是透明区域的内部时,移动手指,拖动整个透明的区域

实现细节

1 根据图片显示的大小,描绘蒙层

// 画阴影
canvas.drawRect(0, 0, frame.left, height, paint);
canvas.drawRect(frame.left, 0, frame.right, frame.top, paint);
canvas.drawRect(frame.right, 0, width, height, paint);
canvas.drawRect(frame.left, frame.bottom, frame.right, height,paint);
// 画圆点标注
canvas.drawCircle(frame.left, (frame.top + frame.bottom) / 2, 10,tipPaint);
canvas.drawCircle(frame.right, (frame.top + frame.bottom) / 2, 10,tipPaint);
canvas.drawCircle((frame.left + frame.right) / 2, frame.top, 10,tipPaint);
canvas.drawCircle((frame.left + frame.right) / 2, frame.bottom, 10,tipPaint);

2 判断是否触摸发生在边界上

2.1 判断

case MotionEvent.ACTION_DOWN:downX = (int) event.getX();downY = (int) event.getY();if (checkContainsPoint(downX, downY)) {touchInFrame = true;if ((downX > frame.left - 10 && downX < frame.left + 10)&& downY > frame.top && downY < frame.bottom) {touchFrameLeft = true;}if ((downX > frame.right - 10 && downX < frame.right + 10)&& downY > frame.top && downY < frame.bottom) {touchFrameRight = true;}if ((downX > frame.left && downX < frame.right)&& downY > frame.top - 10 && downY < frame.top + 10) {touchFrameTop = true;}if ((downX > frame.left && downX < frame.right)&& downY > frame.bottom - 10&& downY < frame.bottom + 10) {touchFrameBottom = true;}} else {touchInFrame = false;}break;

通过四个标志位,标识触摸发生在哪条边界上

2.2 在边界上移动的操作

int currentX = (int) event.getX();
int currentY = (int) event.getY();
// 是否在边界上判断
if (touchFrameLeft) {frame.left += currentX - downX;if (frame.left <= bitmapRect.left) {frame.left = bitmapRect.left;}frameWidth = frame.right - frame.left;downX = currentX;invalidate();return true;
}
if (touchFrameRight) {frame.right += currentX - downX;if (frame.right >= bitmapRect.right) {frame.right = bitmapRect.right;}frameWidth = frame.right - frame.left;downX = currentX;invalidate();return true;
}
if (touchFrameTop) {frame.top += currentY - downY;if (frame.top <= bitmapRect.top) {frame.top = bitmapRect.top;}frameHeight = frame.bottom - frame.top;downY = currentY;invalidate();return true;
}
if (touchFrameBottom) {frame.bottom += currentY - downY;if (frame.bottom >= bitmapRect.bottom) {frame.bottom = bitmapRect.bottom;}frameHeight = frame.bottom - frame.top;downY = currentY;invalidate();return true;
}

3 判断是否触摸发生在透明区域内

3.1 判断

private boolean checkContainsPoint(int x, int y) {Rect boundsRect = new Rect();boundsRect.left = frame.left - 10;boundsRect.top = frame.top - 10;boundsRect.right = frame.right + 10;boundsRect.bottom = frame.bottom + 10;if (boundsRect.contains(x, y)) {return true;}return false;}

通过点击位置与透明区域的四条边界(rect),标识触摸发生在透明区域内

3.2 在透明区域内移动的操作

// 触摸发生不属于边界
frame.left += currentX - downX;
frame.right += currentX - downX;
frame.top += currentY - downY;
frame.bottom += currentY - downY;
if (frame.left <= bitmapRect.left) {frame.left = bitmapRect.left;frame.right = bitmapRect.left + frameWidth;
}
if (frame.right >= bitmapRect.right) {frame.left = bitmapRect.right - frameWidth;frame.right = bitmapRect.right;
}
if (frame.top <= bitmapRect.top) {frame.top = bitmapRect.top;frame.bottom = bitmapRect.top + frameHeight;
}
if (frame.bottom >= bitmapRect.bottom) {frame.top = bitmapRect.bottom - frameHeight;frame.bottom = bitmapRect.bottom;
}
downX = currentX;
downY = currentY;
invalidate();

上边的操作都是通过onTouchEvent来实现的,移动的过程必须要注意不能让透明区域超过图片显示的边界

4 当点击了裁剪的按钮,通过透明区域与图片的显示比例,裁剪图片,并将裁剪的图片保存在本地,然后将图片的路径返回给上一个activity

4.1 通过Bitmap.createBitmap裁剪图片

4.2 由于部分图片过大,在裁剪界面预览的图片需要通过等比压缩再显示

这篇关于Android自定义裁剪图片的View的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097030

相关文章

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,