当前位置: 首页 > Web前端 > HTML

PhotoView——支持图片缩放、平移、旋转的一个优雅的三方组件

时间:2023-03-28 13:12:03 HTML

PhotoView——优雅的三方组件,支持图片缩放、平移、旋转图片缩放、平移、旋转等功能。使用场景PhotoView为广大OpenHarmony应用开发者在处理图片时提供了极大的便利。开发者需要浏览查看图片时,只需要导入PhotoView三方组件,然后调用相关接口即可实现效果。例如,基于大宇200开发板开发的图库应用,使用的是PhotoView三方库。处理图片。效果展示开发环境安装IDE:支持DevEcoStudio3.0Beta3(BuildVersion3.0.0.901)及以上版本。安装SDK:支持OpenHarmonyAPIversion9及以上如何使用1.下载PhotoView组件,导入npminstall@ohos/photoview--save;从“@ohos/photoview”导入{PhotoView};2.生成PhotoView2.1创建模型对象@State数据:PhotoView.Model=newPhotoView.Model();2.2设置图片源aboutToAppear(){this.data.setImageResource($rawfile('wallpaper.jpg')).setScale(1,false).setImageFit(ImageFit.Contain).setOnPhotoTapListener({onPhotoTap(x:number,y:number){}})}3.使用示例:平移、缩放、旋转的核心思想都是通过手势触发,然后得到变换前后的图片Offset,最后更新图片的矩阵Matrix为达到效果。这里以平移为例子说明:PinchGesture()//平移手势接口.onActionStart((event)=>{console.log('photoPinchGesturestart:'+this.model.animate)}).onActionUpdate((event)=>{console.info("photopin:"+JSON.stringify(event))if(this.model.isZoom){varcurrentScale:number=this.model.scale+event.scale-1;console.log('photoPinchGesture更新:'+currentScale)if(currentScale>this.model.scaleMax){this.model.scale=this.model.scaleMax}elseif(currentScale{if(this.model.scalethis.model.scaleMax){this.model.scale=this.model.scaleMax}this.model.isZooming=(this.model.scale>1)如果(this.model.matrixChangedListener!=null){this.model.matrixChangedListener.onMatrixChanged(this.model.rect)}如果(this.model.scaleChangeListener!=null){this.model.scaleChangeListener.onScaleChange(this.model.scale,0,0)}})调用UpdateMatrix()方法publicupdateMatrix():void{this.rect.left=this.componentWidth/2-(this.componentWidth*this.scale)/2+this.offsetX;this.rect.right=this.componentWidth/2+(this.componentWidth*this.scale)/2+this.offsetX;this.rect.top=this.componentHeight/2-(this.sHeight/2)*this.scale+this.offsetY;this.rect.bottom=this.componentHeight/2+(this.sHeight/2)*this.scale+this.offsetY;this.matrix=Matrix4.identity().rotate({x:0,y:0,z:1,angle:this.rotateAngle}).translate({x:this.offsetX,y:this.offsetY})。scale({x:this.scale,y:this.scale,centerX:this.centerX,centerY:this.centerY})}Matrix已经更新,此时给图片更新矩阵即可Image(this.model.src).alt(this.model.previewImage).objectFit(this.model.imageFit).transform(this.model.matrix)//传递更新后的矩阵value.interpolation(ImageInterpolation.Low)Demo源码及文件结构下载地址https://gitee.com/openharmony...文件目录结构如下|----PhotoView-ETS|----入口||----pages#示例代码文件夹|----照片查看||----组件#库文件夹|||----PhotoView.ets#自定义组件|||----RectF.ets#区域坐标点数据包||----README.md#安装和使用方法类结构相关方法结束语通过本文的介绍,你应该对OpenHarmonyPhotoView组件有了一个初步的了解。我们所有的源代码和指导文档都是开源的。如果您对本文的内容和实现的demo感兴趣,可以根据本文的介绍下载OpenHarmonyPhotoView的源码进行研究和使用。同时也欢迎更多的开发者与我们分享开发成果、技术解读和经验。(OpenHarmonyPhotoView源码下载链接https://gitee.com/openharmony...)