【HarmonyOS】头像圆形裁剪功能之手势放大缩小,平移,双击缩放控制(三)

本文主要是介绍【HarmonyOS】头像圆形裁剪功能之手势放大缩小,平移,双击缩放控制(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【HarmonyOS】头像裁剪之手势放大缩小,平移,双击缩放控制(三)

一、DEMO效果图:
在这里插入图片描述
在这里插入图片描述

二、开发思路:
使用矩阵变换控制图片的放大缩小和平移形态。

通过监听点击手势TapGesture,缩放手势PinchGesture,拖动手势PanGesture进行手势操作的功能实现。

通过对矩阵变换参数mMatrix的赋值,将矩阵变换参数赋值给image控件。实现手势操作和图片操作的同步。

在这里插入图片描述
该参数拥有四维坐标,只需要通过手势操作调整四个参数即可实现。通过.transform(this.mMatrix)赋值给image控件。

通过image的.onComplete(this.onLoadImgComplete)函数回调,获取图片控件的宽高和内容宽高等参数。以此为基准,手势操作调整的都是这些值。

三、DEMO示例代码:

import router from '@ohos.router';
import { CropMgr, ImageInfo } from '../manager/CropMgr';
import { image } from '@kit.ImageKit';
import Matrix4 from '@ohos.matrix4';
import FS from '@ohos.file.fs';export class LoadResult {width: number = 0;height: number = 0;componentWidth: number = 0;componentHeight: number = 0;loadingStatus: number = 0;contentWidth: number = 0;contentHeight: number = 0;contentOffsetX: number = 0;contentOffsetY: number = 0;
}

export struct CropPage {private TAG: string = "CropPage";private mRenderingContextSettings: RenderingContextSettings = new RenderingContextSettings(true);private mCanvasRenderingContext2D: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.mRenderingContextSettings);// 加载图片 mImg: PixelMap | undefined = undefined;// 图片矩阵变换参数 mMatrix: object = Matrix4.identity().translate({ x: 0, y: 0 }).scale({ x: 1, y: 1}); mImageInfo: ImageInfo = new ImageInfo();private tempScale = 1;private startOffsetX: number = 0;private startOffsetY: number = 0;aboutToAppear(): void {console.log(this.TAG, "aboutToAppear start");let temp = CropMgr.Ins().mSourceImg;console.log(this.TAG, "aboutToAppear temp: " + JSON.stringify(temp));this.mImg = temp;console.log(this.TAG, "aboutToAppear end");}private getImgInfo(){return this.mImageInfo;}onClickCancel = ()=>{router.back();}onClickConfirm = async ()=>{if(!this.mImg){console.error(this.TAG, " onClickConfirm mImg error null !");return;}// 存当前裁剪的图// ...router.back();}/*** 复制图片* @param pixel* @returns*/async copyPixelMap(pixel: PixelMap): Promise<PixelMap> {const info: image.ImageInfo = await pixel.getImageInfo();const buffer: ArrayBuffer = new ArrayBuffer(pixel.getPixelBytesNumber());await pixel.readPixelsToBuffer(buffer);const opts: image.InitializationOptions = {editable: true,pixelFormat: image.PixelMapFormat.RGBA_8888,size: { height: info.size.height, width: info.size.width }};return image.createPixelMap(buffer, opts);}/*** 图片加载回调*/private onLoadImgComplete = (msg: LoadResult) => {this.getImgInfo().loadResult = msg;this.checkImageScale();}/*** 绘制画布中的取景框*/private onCanvasReady = ()=>{if(!this.mCanvasRenderingContext2D){console.error(this.TAG, "onCanvasReady error mCanvasRenderingContext2D null !");return;}let cr = this.mCanvasRenderingContext2D;// 画布颜色cr.fillStyle = '#AA000000';let height = cr.height;let width = cr.width;cr.fillRect(0, 0, width, height);// 圆形的中心点let centerX = width / 2;let centerY = height / 2;// 圆形半径let radius = Math.min(width, height) / 2 - px2vp(100);cr.globalCompositeOperation = 'destination-out'cr.fillStyle = 'white'cr.beginPath();cr.arc(centerX, centerY, radius, 0, 2 * Math.PI);cr.fill();cr.globalCompositeOperation = 'source-over';cr.strokeStyle = '#FFFFFF';cr.beginPath();cr.arc(centerX, centerY, radius, 0, 2 * Math.PI);cr.closePath();cr.lineWidth = 1;cr.stroke();}build() {RelativeContainer() {// 黑色底图Row().width("100%").height("100%").backgroundColor(Color.Black)// 用户图Image(this.mImg).objectFit(ImageFit.Contain).width('100%').height('100%').transform(this.mMatrix).alignRules({center: { anchor: '__container__', align: VerticalAlign.Center },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).onComplete(this.onLoadImgComplete)// 取景框Canvas(this.mCanvasRenderingContext2D).width('100%').height('100%').alignRules({center: { anchor: '__container__', align: VerticalAlign.Center },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).backgroundColor(Color.Transparent).onReady(this.onCanvasReady).clip(true).backgroundColor("#00000080")Row(){Button("取消").size({width: px2vp(450),height: px2vp(200)}).onClick(this.onClickCancel)Blank()Button("确定").size({width: px2vp(450),height: px2vp(200)}).onClick(this.onClickConfirm)}.width("100%").height(px2vp(200)).margin({ bottom: px2vp(500) }).alignRules({center: { anchor: '__container__', align: VerticalAlign.Bottom },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).justifyContent(FlexAlign.Center)}.width("100%").height("100%").priorityGesture(// 点击手势TapGesture({// 点击次数count: 2,// 一个手指fingers: 1}).onAction((event: GestureEvent)=>{console.log(this.TAG, "TapGesture onAction start");if(!event){return;}if(this.getImgInfo().scale != 1){this.getImgInfo().scale = 1;this.getImgInfo().offsetX = 0;this.getImgInfo().offsetY = 0;this.mMatrix = Matrix4.identity().translate({x: this.getImgInfo().offsetX,y: this.getImgInfo().offsetY}).scale({x: this.getImgInfo().scale,y: this.getImgInfo().scale})}else{this.getImgInfo().scale = 2;this.mMatrix = Matrix4.identity().translate({x: this.getImgInfo().offsetX,y: this.getImgInfo().offsetY}).scale({x: this.getImgInfo().scale,y: this.getImgInfo().scale})}console.log(this.TAG, "TapGesture onAction end");})).gesture(GestureGroup(GestureMode.Parallel,// 缩放手势PinchGesture({// 两指缩放fingers: 2}).onActionStart(()=>{console.log(this.TAG, "PinchGesture onActionStart");this.tempScale = this.getImgInfo().scale;}).onActionUpdate((event)=>{console.log(this.TAG, "PinchGesture onActionUpdate" + JSON.stringify(event));if(event){this.getImgInfo().scale = this.tempScale * event.scale;this.mMatrix = Matrix4.identity().translate({x: this.getImgInfo().offsetX,y: this.getImgInfo().offsetY}).scale({x: this.getImgInfo().scale,y: this.getImgInfo().scale})}}).onActionEnd(()=>{console.log(this.TAG, "PinchGesture onActionEnd");}),// 拖动手势PanGesture().onActionStart(()=>{console.log(this.TAG, "PanGesture onActionStart");this.startOffsetX = this.getImgInfo().offsetX;this.startOffsetY = this.getImgInfo().offsetY;}).onActionUpdate((event)=>{console.log(this.TAG, "PanGesture onActionUpdate" + JSON.stringify(event));if(event){let distanceX: number = this.startOffsetX + vp2px(event.offsetX) / this.getImgInfo().scale;let distanceY: number = this.startOffsetY + vp2px(event.offsetY) / this.getImgInfo().scale;this.getImgInfo().offsetX = distanceX;this.getImgInfo().offsetY = distanceY;this.mMatrix = Matrix4.identity().translate({x: this.getImgInfo().offsetX,y: this.getImgInfo().offsetY}).scale({x: this.getImgInfo().scale,y: this.getImgInfo().scale})}}).onActionEnd(()=>{console.log(this.TAG, "PanGesture onActionEnd");})))}
}

这篇关于【HarmonyOS】头像圆形裁剪功能之手势放大缩小,平移,双击缩放控制(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

JavaFX应用更新检测功能(在线自动更新方案)

JavaFX开发的桌面应用属于C端,一般来说需要版本检测和自动更新功能,这里记录一下一种版本检测和自动更新的方法。 1. 整体方案 JavaFX.应用版本检测、自动更新主要涉及一下步骤: 读取本地应用版本拉取远程版本并比较两个版本如果需要升级,那么拉取更新历史弹出升级控制窗口用户选择升级时,拉取升级包解压,重启应用用户选择忽略时,本地版本标志为忽略版本用户选择取消时,隐藏升级控制窗口 2.

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

Spring+MyBatis+jeasyui 功能树列表

java代码@EnablePaging@RequestMapping(value = "/queryFunctionList.html")@ResponseBodypublic Map<String, Object> queryFunctionList() {String parentId = "";List<FunctionDisplay> tables = query(parent

PostgreSQL核心功能特性与使用领域及场景分析

PostgreSQL有什么优点? 开源和免费 PostgreSQL是一个开源的数据库管理系统,可以免费使用和修改。这降低了企业的成本,并为开发者提供了一个活跃的社区和丰富的资源。 高度兼容 PostgreSQL支持多种操作系统(如Linux、Windows、macOS等)和编程语言(如C、C++、Java、Python、Ruby等),并提供了多种接口(如JDBC、ODBC、ADO.NET等

寻迹模块TCRT5000的应用原理和功能实现(基于STM32)

目录 概述 1 认识TCRT5000 1.1 模块介绍 1.2 电气特性 2 系统应用 2.1 系统架构 2.2 STM32Cube创建工程 3 功能实现 3.1 代码实现 3.2 源代码文件 4 功能测试 4.1 检测黑线状态 4.2 未检测黑线状态 概述 本文主要介绍TCRT5000模块的使用原理,包括该模块的硬件实现方式,电路实现原理,还使用STM32类