在動(dòng)畫開發(fā)場(chǎng)景中,經(jīng)常用到彈性效果,尤其在拖拽某個(gè)對(duì)象時(shí)經(jīng)常伴隨彈性動(dòng)效。
OpenHarmony 提供了三種彈簧動(dòng)畫曲線用來實(shí)現(xiàn)彈性效果,本例將為大家介紹這三種曲線的用法。
最終效果如下:
運(yùn)行環(huán)境
本例基于以下環(huán)境開發(fā),開發(fā)者也可以基于其他適配的版本進(jìn)行開發(fā):
IDE:DevEco Studio 3.1 Beta2
SDK:Ohos_sdk_public 3.2.11.9(API Version 9 Release)
實(shí)現(xiàn)思路
本例主要用到以下三種彈簧動(dòng)畫曲線:
curves.springCurve:通過設(shè)置彈簧的初始速度、質(zhì)量、剛度和阻尼來控制彈簧動(dòng)畫的效果。對(duì)應(yīng)本例中 springCurve 按鈕觸發(fā)的動(dòng)畫。
curves.springMotion:通過設(shè)置彈簧震動(dòng)時(shí)間和阻尼來控制彈簧動(dòng)畫的效果。對(duì)應(yīng)本例中 springMotion 按鈕觸發(fā)的動(dòng)畫。
curves.responsiveSpringMotion:構(gòu)造彈性跟手動(dòng)畫曲線對(duì)象,是springMotion的一種特例,僅默認(rèn)參數(shù)不同,可與 springMotion 混合使用。用來實(shí)現(xiàn)拖拽動(dòng)畫。
開發(fā)步驟
①搭建 UI 框架
樣例中有兩個(gè)按鈕,一個(gè)圖片。內(nèi)容整體縱向分布,兩個(gè)按鈕橫向分布。縱向布局可以采用 Column 組件,橫向布局可以采用 Row 組件。
代碼如下:
@Entry @Component structImageComponent{ build(){ Column(){ Row(){ Button('springCurve') .margin({right:10}) .fontSize(20) .backgroundColor('#18183C') Button('springMotion') .fontSize(20) .backgroundColor('#18183C') } .margin({top:30}) Image($r("app.media.contact2")) .width(100) .height(100) }.width("100%").height("100%").backgroundColor('#A4AE77') } }②為 springCurve 按鈕添加 curves.springCurve 的曲線動(dòng)畫。
... //定義狀態(tài)變量translateY,用來控制笑臉圖像的位移 @StatetranslateY:number=0 ... Button('springCurve') .margin({right:10}) .fontSize(20) .backgroundColor('#18183C') //綁定點(diǎn)擊事件 .onClick(()=>{ //在點(diǎn)擊事件中添加顯示動(dòng)畫 animateTo({ duration:2000, //設(shè)定curves.springCurve為動(dòng)畫曲線 curve:curves.springCurve(100,10,80,10) }, ()=>{ //改變translateY的值,使笑臉圖像發(fā)生位移 this.translateY=-20 }) this.translateY=0 }) ... Image($r("app.media.contact2")) .width(100) .height(100) //為笑臉圖像添加位移屬性,以translateY為參數(shù) .translate({y:this.translateY}) ...效果如下:
![2ba2ebdc-0826-11ee-962d-dac502259ad0.gif](https://file1.elecfans.com//web2/M00/9B/34/wKgZomTnueiATYBXAAJaF4jGO68820.gif)
③為 springMotion 按鈕添加 curves.springMotion 曲線動(dòng)畫。
這里通過 position 屬性控制 springMotion 按鈕的移動(dòng),當(dāng)然開發(fā)者也可以繼續(xù)選擇使用 translate 屬性。
... //定義狀態(tài)變量translateY,用來控制笑臉圖像的位置變化 @StateimgPos:{ x:number, y:number }={x:125,y:400} ... Button('springMotion') .fontSize(20) .backgroundColor('#18183C') //綁定點(diǎn)擊事件 .onClick(()=>{ //在點(diǎn)擊事件中添加顯示動(dòng)畫 animateTo({ duration:15, //設(shè)定curves.springMotion為動(dòng)畫曲線 curve:curves.springMotion(0.5,0.5), onFinish:()=>{ animateTo({duration:500, curve:curves.springMotion(0.5,0.5),},()=>{ //動(dòng)畫結(jié)束時(shí)笑臉圖像位置還原 this.imgPos={x:125,y:400} }) } },()=>{ //改變笑臉圖像位置,y軸位置由400,變?yōu)?50 this.imgPos={x:125,y:150} }) }) ... Image($r("app.media.contact2")) .width(100) .height(100) .translate({y:this.translateY}) //為笑臉圖像添加位置屬性,以imgPos為參數(shù) .position(this.imgPos) ...效果如下:
![2be677f8-0826-11ee-962d-dac502259ad0.gif](https://file1.elecfans.com//web2/M00/9B/34/wKgZomTnueiAXPCFAAMfXa_slBU511.gif)
④使用 curves.responsiveSpringMotion 為笑臉圖像添加拖拽動(dòng)畫。
... Image($r("app.media.contact2")) .width(100) .height(100) .translate({y:this.translateY}) .position(this.imgPos) //綁定觸摸事件 .onTouch((event:TouchEvent)=>{ //當(dāng)觸摸放開時(shí),笑臉圖像位置還原 if(event.type==TouchType.Up){ animateTo({ duration:50, delay:0, curve:curves.springMotion(), onFinish:()=>{ } },()=>{ this.imgPos={x:125,y:400} }) }else{ //觸摸過程中觸發(fā)跟手動(dòng)畫 animateTo({ duration:50, delay:0, //設(shè)定跟手動(dòng)畫曲線 curve:curves.responsiveSpringMotion(), onFinish:()=>{ } },()=>{ //根據(jù)觸點(diǎn)位置改變笑臉圖像位置,從而實(shí)現(xiàn)跟手動(dòng)畫 this.imgPos={ x:event.touches[0].screenX-100/2, y:event.touches[0].screenY-100/2 } }) } }) ...效果如下:
![2c0585f8-0826-11ee-962d-dac502259ad0.gif](https://file1.elecfans.com//web2/M00/9B/34/wKgZomTnueiAaESzAAcvnhGX5bE984.gif)
完整代碼
本例完整代碼如下:
importcurvesfrom'@ohos.curves'; @Entry @Component structImageComponent{ //定義狀態(tài)變量translateY,用來控制笑臉圖像的位移 @StatetranslateY:number=0 //定義狀態(tài)變量translateY,用來控制笑臉圖像的位置變化 @StateimgPos:{ x:number, y:number }={x:125,y:400} build(){ Column(){ Row(){ Button('springCurve') .margin({right:10}) .fontSize(20) .backgroundColor('#18183C') //綁定點(diǎn)擊事件 .onClick(()=>{ //在點(diǎn)擊事件中添加顯示動(dòng)畫 animateTo({ duration:2000, //設(shè)定curves.springCurve為動(dòng)畫曲線 curve:curves.springCurve(100,10,80,10) }, ()=>{ //改變translateY的值,使笑臉圖像發(fā)生位移 this.translateY=-20 }) this.translateY=0 }) Button('springMotion') .fontSize(20) .backgroundColor('#18183C') //綁定點(diǎn)擊事件 .onClick(()=>{ //在點(diǎn)擊事件中添加顯示動(dòng)畫 animateTo({ duration:15, //設(shè)定curves.springMotion為動(dòng)畫曲線 curve:curves.springMotion(0.5,0.5), onFinish:()=>{ animateTo({duration:500, curve:curves.springMotion(0.5,0.5),},()=>{ //動(dòng)畫結(jié)束時(shí)笑臉圖像位置還原 this.imgPos={x:125,y:400} }) } },()=>{ //改變笑臉圖像位置,y軸位置由400,變?yōu)?50 this.imgPos={x:125,y:150} }) }) } .margin({top:30}) Image($r("app.media.contact2")) .width(100) .height(100) //為笑臉圖像添加位移屬性,以translateY為參數(shù) .translate({y:this.translateY}) //為笑臉圖像添加位置屬性,以imgPos為參數(shù) .position(this.imgPos) //綁定觸摸事件 .onTouch((event:TouchEvent)=>{ //當(dāng)觸摸放開時(shí),笑臉圖像位置還原 if(event.type==TouchType.Up){ animateTo({ duration:50, delay:0, curve:curves.springMotion(), onFinish:()=>{ } },()=>{ this.imgPos={x:125,y:400} }) }else{ //觸摸過程中觸發(fā)跟手動(dòng)畫,同樣通過animateTo實(shí)現(xiàn)動(dòng)畫效果 animateTo({ duration:50, delay:0, //設(shè)定跟手動(dòng)畫曲線 curve:curves.responsiveSpringMotion(), onFinish:()=>{ } },()=>{ //根據(jù)觸點(diǎn)位置改變笑臉圖像位置,從而實(shí)現(xiàn)跟手動(dòng)畫 this.imgPos={ x:event.touches[0].screenX-100/2, y:event.touches[0].screenY-100/2 } }) } }) }.width("100%").height("100%").backgroundColor('#A4AE77') } }
審核編輯:劉清
-
觸摸屏
+關(guān)注
關(guān)注
42文章
2317瀏覽量
116776 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3747瀏覽量
16594
原文標(biāo)題:OpenHarmony上實(shí)現(xiàn)彈性動(dòng)效
文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
家庭、單位用電安全,需要“在彈性范圍內(nèi)、彈性連接”的接線端子
動(dòng)效設(shè)計(jì)的這些設(shè)計(jì)流程你知道嗎
OpenHarmony 3.1 Beta版本關(guān)鍵特性解析——OpenHarmony圖形框架
為什么要在OpenHarmony設(shè)備上安裝Dropbear呢
app圖標(biāo)動(dòng)效在openharmony的源碼上哪里實(shí)現(xiàn)的?
OpenHarmony 官網(wǎng)文檔有哪些上新?上篇:應(yīng)用開發(fā)文檔上新
網(wǎng)絡(luò)組件axios可以在OpenHarmony上使用了
開源圖形驅(qū)動(dòng)在OpenHarmony上的使用和落地
云計(jì)算彈性評(píng)測(cè)模型的研究與實(shí)現(xiàn)
基于距離徙動(dòng)校正的彈速補(bǔ)償FPGA實(shí)現(xiàn)方法
![基于距離徙<b class='flag-5'>動(dòng)</b>校正的彈速補(bǔ)償FPGA<b class='flag-5'>實(shí)現(xiàn)</b><b class='flag-5'>方法</b>](https://file.elecfans.com/web1/M00/8D/15/pIYBAFyezMCAPgB9AABLXGB10OY949.png)
華為圖像服務(wù)場(chǎng)景動(dòng)效Java示例代碼
OpenHarmony生態(tài)論壇:UROVO在OpenHarmony上的規(guī)劃和實(shí)踐
![<b class='flag-5'>OpenHarmony</b>生態(tài)論壇:UROVO<b class='flag-5'>在</b><b class='flag-5'>OpenHarmony</b><b class='flag-5'>上</b>的規(guī)劃和實(shí)踐](https://file.elecfans.com/web2/M00/3F/33/pYYBAGJmai-AVvFKAAaIpTOZAxw635.png)
如何在OpenHarmony上實(shí)現(xiàn)?翻頁動(dòng)效呢?
![如何在<b class='flag-5'>OpenHarmony</b><b class='flag-5'>上</b><b class='flag-5'>實(shí)現(xiàn)</b>?翻頁<b class='flag-5'>動(dòng)</b><b class='flag-5'>效</b>呢?](https://file1.elecfans.com/web2/M00/89/77/wKgaomSFdOKAWUi3AAANB0pPSlA815.gif)
OpenHarmony實(shí)戰(zhàn)開發(fā)-如何實(shí)現(xiàn)組件動(dòng)畫。
![<b class='flag-5'>OpenHarmony</b>實(shí)戰(zhàn)開發(fā)-如何<b class='flag-5'>實(shí)現(xiàn)</b>組件動(dòng)畫。](https://file1.elecfans.com/web2/M00/DF/14/wKgaomYt_x2Ab_qaAAEJlEQIlYw492.jpg)
評(píng)論