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

相关文章

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp