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

embedded/2024/9/24 7:42:58/

【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");})))}
}

http://www.ppmy.cn/embedded/109875.html

相关文章

云服务器部署DB-GPT项目

本文收录于《DB-GPT项目》专栏&#xff0c;专栏总目录&#xff1a; 点击这里。 文章目录 项目介绍 一、登录云服务器 1. 进入控制台 2.点击容器实例&#xff08;点数字&#xff09; 二、创建容器实例 1. 等待容器实例创建好&#xff0c;创建好的容器实例如下&#xff1a;…

[含视频和源码]CRUD的最佳实践,联动前后端,包含微信小程序,API,HTML等(三)

关说不练假把式&#xff0c;在上一&#xff0c;二篇中介绍了我心目中的CRUD的样子 基于之前的理念&#xff0c;我开发了一个命名为PasteTemplate的项目&#xff0c;这个项目呢后续会转化成项目模板&#xff0c;转化成项目模板后&#xff0c;后续需要开发新的项目就可以基于这…

阿里云镜像报错 [Errno 14] HTTP Error 302 - Found 问题解决记录

1、问题背景和解决思路 在本地安装 CentOS7 后&#xff0c;网络已调通可正常上网&#xff0c;但切换阿里云镜像后&#xff0c;使用 yum 安装软件时出现 “[Errno 14] HTTPS Error 302 - Found Trying other mirror.” 报错&#xff0c;原因是 yum 源配置问题。给出了详细的解决…

SQL优化(二)统计信息

收集统计信息 数据库的统计信息非常重要&#xff0c;如果没有正确地收集表的统计信息&#xff0c;或者没有及时地更新表的统计信息&#xff0c;SQL就有可能走错执行计划&#xff0c;也就会出现性能问题。 统计信息主要分为表的统计信息、列的统计信息、索引的统计信息、系统的…

MT6895(天玑8100)处理器规格参数_MTK联发科平台方案

MT6895平台 采用台积电5nm工艺&#xff0c;与天玑 8000 相比性能提升 20% &#xff0c;搭载4 个 2.85GHz A78 核心 4 个 2.0GHz A55 核心&#xff0c;CPU能效比上一代提高 25% 。GPU 采用了第三代的Valhall Arm Mali-G610 MC6架构&#xff0c;拥有6核心&#xff0c;搭配天玑81…

ECOLOGY携带BearerToken后根据手机号码获取北森系统人员id

需求:获取北森系统的人员id 核心代码: package com.kaka.beisen;import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets;p…

尚品汇-商品上下架完善(更新ES)、延迟消息(四十四)

目录&#xff1a; &#xff08;1&#xff09;改造商品搜索上下架 &#xff08;2&#xff09;延迟消息 &#xff08;1&#xff09;改造商品搜索上下架 定义商品上下架常量 在rabbit-util模块中导入常量类MqConst。 /*** 商品上下架.*/ public static final String EXCHANGE…

python进阶篇-day09-数据结构与算法(非线性结构与排序算法)

非线性结构(树状结构) 特点: 每个节点都可以有n个子节点(后继节点) 和 n个父节点(前驱节点) 代表: 树, 图...... 概述 属于数据结构之 非线性结构的一种, 父节点可以有多个子节点(后续节点) 特点 有且只有1个根节点 每个节点都可以有1个父节点及任意个子节点, 前提: 根节点除…