0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

OpenHarmony PhotoView組件的介紹

OpenAtom OpenHarmony ? 來源:OpenAtom OpenHarmony ? 作者:OpenAtom OpenHarmony ? 2022-09-09 10:04 ? 次閱讀

PhotoView是OpenAtom OpenHarmony(簡(jiǎn)稱“OpenHarmony”)系統(tǒng)的一款圖片縮放及瀏覽的三方組件,用于聲明式應(yīng)用開發(fā),支持圖片縮放、平移、旋轉(zhuǎn)等功能。

使用場(chǎng)景

PhotoView為廣大OpenHarmony應(yīng)用開發(fā)者在處理圖片時(shí),提供了很大的便利。當(dāng)開發(fā)者需要對(duì)圖片進(jìn)行瀏覽、查看等處理時(shí),只需要導(dǎo)入PhotoView三方組件,然后調(diào)用相關(guān)的接口就能實(shí)現(xiàn)效果,例如基于大禹200開發(fā)板開發(fā)的圖庫(kù)應(yīng)用,就使用到了PhotoView三方庫(kù)去處理圖片。

效果展示

d104fd5a-2f77-11ed-ba43-dac502259ad0.png

開發(fā)環(huán)境

安裝IDE:支持DevEco Studio 3.0 Beta3(Build Version 3.0.0.901)及以上版本。

安裝SDK:支持OpenHarmony API version 9及以上版本

如何使用

1.下載PhotoView組件,在page頁(yè)面導(dǎo)入

npm install @ohos/photoview --save;import {PhotoView} from '@ohos/photoview';

2.生成Photo View

2.1創(chuàng)建model對(duì)象

@State data: PhotoView.Model = newPhotoView.Model();
2.2設(shè)置圖片源

aboutToAppear() {this.data.setImageResource($rawfile('wallpaper.jpg')).setScale(1, false).setImageFit(ImageFit.Contain).setOnPhotoTapListener({onPhotoTap(x:number,y:number){}})}

3.使用示例:

平移、縮放、旋轉(zhuǎn)核心思想都是通過手勢(shì)觸發(fā),然后獲取圖片變換前后的偏移量,最后更新圖片的矩陣Matrix實(shí)現(xiàn)效果。

這里以平移為例說明:

PinchGesture() // 平移手勢(shì)接口 .onActionStart((event) => {   console.log('photo PinchGesture start:' +this.model.animate) }) .onActionUpdate((event) => { console.info("photo pin:" +JSON.stringify(event)) if (this.model.isZoom) { var currentScale: number =this.model.scale + event.scale - 1; console.log('photo PinchGesture update:'+ currentScale) if (currentScale >this.model.scaleMax) { this.model.scale = this.model.scaleMax } else if (currentScale  { if (this.model.scale this.model.scaleMax) { this.model.scale = this.model.scaleMax } this.model.isZooming = (this.model.scale> 1) if (this.model.matrixChangedListener !=null) {this.model.matrixChangedListener.onMatrixChanged(this.model.rect) } if (this.model.scaleChangeListener != null){this.model.scaleChangeListener.onScaleChange(this.model.scale, 0, 0) } })
調(diào)用UpdateMatrix( )方法

public updateMatrix():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已更新,此時(shí)給圖片更新矩陣即可。

Image(this.model.src)      .alt(this.model.previewImage) .objectFit(this.model.imageFit) .transform(this.model.matrix) // 傳遞更新后的矩陣值 .interpolation(ImageInterpolation.Low)

demo源碼及文件結(jié)構(gòu)

下載地址 https://gitee.com/openharmony-sig/PhotoView-ETS

文件目錄結(jié)構(gòu)如下

|---- PhotoView-ETS  |---- entry|     |---- pages  # 示例代碼文件夾        |---- photoView |     |---- components  # 庫(kù)文件夾|     |     |---- PhotoView.ets  #自定義組件                 |     |     |---- RectF.ets  # 區(qū)域坐標(biāo)點(diǎn)數(shù)據(jù)封裝|     |---- README.md  # 安裝使用方法

類結(jié)構(gòu)

d151e41c-2f77-11ed-ba43-dac502259ad0.png

相關(guān)方法

d17960fa-2f77-11ed-ba43-dac502259ad0.jpg

結(jié)語(yǔ)

通過本篇文章介紹,您對(duì)OpenHarmony PhotoView組件應(yīng)該有了初步的了解。我們所有的源碼和指導(dǎo)文檔都已經(jīng)開源,如果您對(duì)本篇文章內(nèi)容以及所實(shí)現(xiàn)的Demo感興趣,可以根據(jù)本篇文章介紹自行下載OpenHarmony PhotoView源碼進(jìn)行研究和使用。同時(shí)也歡迎更多開發(fā)者與我們共享開發(fā)成果,分享技術(shù)解讀與經(jīng)驗(yàn)心得。

審核編輯:彭靜
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 組件
    +關(guān)注

    關(guān)注

    1

    文章

    498

    瀏覽量

    17770
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3607

    瀏覽量

    15956

原文標(biāo)題:PhotoView——支持圖片縮放、平移、旋轉(zhuǎn)的一個(gè)優(yōu)雅的三方組件

文章出處:【微信號(hào):gh_e4f28cfa3159,微信公眾號(hào):OpenAtom OpenHarmony】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    鴻蒙開發(fā)OpenHarmony組件復(fù)用案例

    緩存里。 在父自定義組件再次創(chuàng)建可復(fù)用組件時(shí),會(huì)通過更新可復(fù)用組件的方式,從緩存快速創(chuàng)建可復(fù)用組件。這就是OpenHarmony
    發(fā)表于 01-15 17:37

    【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(5)——Photoview組件

    `PhotoView使用說明1.PhotoView功能介紹1.1 組件介紹PhotoView
    發(fā)表于 03-30 09:29

    組件資料】HarmonyOS三方件開發(fā)指南

    手機(jī)模擬器上運(yùn)行效果:4、HarmonyOS三方件開發(fā)指南——Photoview組件1.PhotoView功能介紹1.1 組件
    發(fā)表于 03-21 11:18

    HarmonyOS應(yīng)用開發(fā)-photoView組件體驗(yàn)分享

    組件名稱:photoView主語(yǔ)言:JAVA作用:PhotoView 旨在幫助生成一個(gè)易于使用的縮放 openharmony 圖像組件的實(shí)現(xiàn)
    發(fā)表于 05-10 10:44

    【學(xué)習(xí)打卡】OpenHarmony的TextClock組件介紹

    OpenHarmony系統(tǒng)中可以通過TextClock組件實(shí)現(xiàn)時(shí)鐘應(yīng)用,可以把當(dāng)前系統(tǒng)時(shí)間顯示在設(shè)備上,并且支持不同時(shí)區(qū)的時(shí)間顯示。Text Clock 是OpenHarmon中的 UI 小部
    發(fā)表于 07-30 22:33

    PhotoView——支持圖片縮放、平移、旋轉(zhuǎn)的一個(gè)優(yōu)雅的三方組件

    PhotoView——支持圖片縮放、平移、旋轉(zhuǎn)的一個(gè)優(yōu)雅的三方組件簡(jiǎn)介PhotoView是OpenAtom OpenHarmony(簡(jiǎn)稱“OpenH
    發(fā)表于 09-16 10:30

    OpenHarmony組件復(fù)用示例

    承載數(shù)據(jù)的差異。這樣的組件緩存起來,需要使用到該組件時(shí)直接復(fù)用, ● 減少了創(chuàng)建和渲染的時(shí)間,可以提高幀率和用戶性能體驗(yàn)。本文會(huì)介紹開發(fā)OpenHarmony應(yīng)用時(shí)如何使用
    發(fā)表于 08-29 14:40

    OpenHarmony自定義組件:ClearableInput和Keyboard

    組件介紹: 本示例包含了兩個(gè)OpenHarmony自定義組件,一個(gè)是ClearableInput,另一個(gè)是Keyboard。 ClearableInput 定義了一個(gè)帶清空?qǐng)D標(biāo)的文本輸
    發(fā)表于 03-18 15:21 ?1次下載
    <b class='flag-5'>OpenHarmony</b>自定義<b class='flag-5'>組件</b>:ClearableInput和Keyboard

    OpenHarmony自定義組件FlowImageLayout

    組件介紹 本示例是OpenHarmony自定義組件FlowImageLayout。 用于將一個(gè)圖片列表以瀑布流的形式顯示出來。 調(diào)用方法
    發(fā)表于 03-21 10:17 ?3次下載
    <b class='flag-5'>OpenHarmony</b>自定義<b class='flag-5'>組件</b>FlowImageLayout

    OpenHarmony自定義組件ProgressWithText

    組件介紹 本示例是OpenHarmony自定義組件ProgressWithText。 在原來進(jìn)度條的上方加了一個(gè)文本框,動(dòng)態(tài)顯示當(dāng)前進(jìn)度并調(diào)整位置。 調(diào)用方法
    發(fā)表于 03-23 14:03 ?1次下載
    <b class='flag-5'>OpenHarmony</b>自定義<b class='flag-5'>組件</b>ProgressWithText

    OpenHarmony自定義組件CircleProgress

    組件介紹 本示例是OpenHarmony自定義組件CircleProgress。 用于定義一個(gè)帶文字的圓形進(jìn)度條。 調(diào)用方法
    發(fā)表于 03-23 14:06 ?4次下載
    <b class='flag-5'>OpenHarmony</b>自定義<b class='flag-5'>組件</b>CircleProgress

    易于使用的openharmony圖像組件PhotoView的實(shí)現(xiàn)

    PhotoView 旨在幫助生成一個(gè)易于使用的縮放 openharmony 圖像組件的實(shí)現(xiàn)。 特征: 開箱即用的縮放,使用多點(diǎn)觸控和雙擊。 滾動(dòng),平滑滾動(dòng)。 在滾動(dòng)父級(jí)中使用時(shí)效果很好。 允許在顯示
    發(fā)表于 04-11 10:30 ?2次下載

    2022 OpenHarmony組件大賽,共建開源組件

    原標(biāo)題:共建開源組件生態(tài) 2022 OpenHarmony組件大賽等你來 2022年4月15日,2022 OpenHarmony組件大賽(下
    的頭像 發(fā)表于 04-26 17:31 ?1496次閱讀
    2022 <b class='flag-5'>OpenHarmony</b><b class='flag-5'>組件</b>大賽,共建開源<b class='flag-5'>組件</b>

    關(guān)于OpenHarmony Jchardet組件介紹

    Jchardet是OpenAtom OpenHarmony(以下簡(jiǎn)稱“OpenHarmony”)系統(tǒng)的一款檢測(cè)文本編碼的組件。當(dāng)上傳一個(gè)文件時(shí),組件可以檢測(cè)并輸出該文件中文本使用的編碼
    的頭像 發(fā)表于 10-12 10:08 ?939次閱讀

    OpenHarmony Jchardet組件簡(jiǎn)介及使用方法

    Jchardet是OpenAtom OpenHarmony(以下簡(jiǎn)稱“OpenHarmony”)系統(tǒng)的一款檢測(cè)文本編碼的組件。當(dāng)上傳一個(gè)文件時(shí),組件可以檢測(cè)并輸出該文件中文本使用的編碼
    的頭像 發(fā)表于 10-14 10:17 ?779次閱讀