顯式動(dòng)畫(huà)

https://gitee.com/openharmony/docs/blob/5654c2b940ab3e2f4f0baf435e630c4ef3536428/zh-cn/application-dev/reference/arkui-ts/ts-explicit-animation.md
來(lái)看一個(gè)簡(jiǎn)單的示例:
@Entry
@Component
structAnimationPage{
//位移屬性
@State_translate:TranslateOptions={
x:0,
y:0,
z:0
}
build(){
Flex({
alignItems:ItemAlign.Center,
justifyContent:FlexAlign.Center,
direction:FlexDirection.Column
}){
Button('執(zhí)行動(dòng)畫(huà)').margin({bottom:50}).onClick(()=>{
//添加一個(gè)簡(jiǎn)單顯式動(dòng)畫(huà)
animateTo({
duration:1000,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
},()=>{
//閉包內(nèi)更改狀態(tài)
this._translate={
x:0,
y:100,
z:0
}
})
})
Column(){
Text('Animate.css')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.fontColor('#351c75')
.translate(this._translate)//位移變換
}
}
.width('100%')
.height('100%')
}
}

如果我們希望向下位移完成后,再向右位移,就需要在第一個(gè)動(dòng)畫(huà)完成后再進(jìn)行第二個(gè)動(dòng)畫(huà),即在第一個(gè)動(dòng)畫(huà)的 onFinish 函數(shù)中執(zhí)行第二個(gè)動(dòng)畫(huà)。

這樣組合起來(lái)可以構(gòu)成一個(gè)更復(fù)雜的連續(xù)動(dòng)畫(huà):
//單步動(dòng)畫(huà)執(zhí)行函數(shù)
animationStep(value:AnimateParam,event:()=>void){
return()=>{
returnnewPromise((resolve)=>{
letonFinish=value.onFinish
value.onFinish=()=>{
if(onFinish)onFinish()
resolve(true)
}
animateTo(value,event)
})
}
}
創(chuàng)建 4 步動(dòng)畫(huà):
aboutToAppear(){
//每步動(dòng)畫(huà)執(zhí)行時(shí)長(zhǎng)
lettime=200
this.step1=this.animationStep({
duration:time,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
onFinish:()=>{
//動(dòng)畫(huà)執(zhí)行完成
console.info('playend')
}
},()=>{
//閉包內(nèi)更改狀態(tài)
this._translate={
x:0,
y:100,
z:0
}
})
this.step2=this.animationStep({
duration:time,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
onFinish:()=>{
//動(dòng)畫(huà)執(zhí)行完成
console.info('playend')
}
},()=>{
//閉包內(nèi)更改狀態(tài)
this._translate={
x:100,
y:100,
z:0
}
})
this.step3=this.animationStep({
duration:time,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
onFinish:()=>{
//動(dòng)畫(huà)執(zhí)行完成
console.info('playend')
}
},()=>{
//閉包內(nèi)更改狀態(tài)
this._translate={
x:100,
y:0,
z:0
}
})
this.step4=this.animationStep({
duration:time,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
onFinish:()=>{
//動(dòng)畫(huà)執(zhí)行完成
console.info('playend')
}
},()=>{
//閉包內(nèi)更改狀態(tài)
this._translate={
x:0,
y:0,
z:0
}
})
}
順序執(zhí)行 4 步動(dòng)畫(huà):
Button('執(zhí)行動(dòng)畫(huà)').margin({bottom:50}).onClick(async()=>{
awaitthis.step1()
awaitthis.step2()
awaitthis.step3()
awaitthis.step4()
})
實(shí)現(xiàn) AnimateCSS 動(dòng)畫(huà)
AnimateCSS:
https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.css
https://animate.style/
pulse 動(dòng)畫(huà):
看下 pulse 動(dòng)畫(huà)樣式代碼:
.animate__pulse{
-webkit-animation-name:pulse;
animation-name:pulse;
-webkit-animation-timing-function:ease-in-out;
animation-timing-function:ease-in-out;
}
@keyframespulse{
from{
-webkit-transform:scale3d(1,1,1);
transform:scale3d(1,1,1);
}
50%{
-webkit-transform:scale3d(1.05,1.05,1.05);
transform:scale3d(1.05,1.05,1.05);
}
to{
-webkit-transform:scale3d(1,1,1);
transform:scale3d(1,1,1);
}
}
ETS 實(shí)現(xiàn):
@State_scale:ScaleOptions={
x:1,
y:1,
z:1
}
...
Column(){
Text('Animate.css')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.fontColor('#351c75')
.translate(this._translate)//位移變換
.scale(this._scale)//比例變化
}
動(dòng)畫(huà)方法:
-
傳遞一個(gè)動(dòng)畫(huà)總時(shí)長(zhǎng) time
-
第一步動(dòng)畫(huà)執(zhí)行段為 0%-50%,所以動(dòng)畫(huà)執(zhí)行時(shí)長(zhǎng)為總時(shí)長(zhǎng)time * 50%
- 第二步動(dòng)畫(huà)執(zhí)行段為 50%-100%,所以動(dòng)畫(huà)執(zhí)行時(shí)長(zhǎng)為總時(shí)長(zhǎng)time * 50%
asyncpulse(time){
//0%-50%
letstep1=this.animationStep({
duration:time*0.5,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
},()=>{
this._scale={
x:1.05,
y:1.05,
z:1.05
}
})
//50%-100%
letstep2=this.animationStep({
duration:time*0.5,//動(dòng)畫(huà)時(shí)長(zhǎng)
tempo:0.5,//播放速率
curve:Curve.EaseInOut,//動(dòng)畫(huà)曲線
delay:0,//動(dòng)畫(huà)延遲
iterations:1,//播放次數(shù)
playMode:PlayMode.Normal,//動(dòng)畫(huà)模式
},()=>{
this._scale={
x:1,
y:1,
z:1
}
})
awaitstep1()
awaitstep2()
}
執(zhí)行動(dòng)畫(huà):
Button('執(zhí)行PULSE動(dòng)畫(huà)').margin({bottom:50}).onClick(async()=>{
this.pulse(500)
})
審核編輯 :李倩
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4400瀏覽量
66385 -
Harmony
+關(guān)注
關(guān)注
0文章
108瀏覽量
3414
原文標(biāo)題:在鴻蒙上實(shí)現(xiàn)AnimateCSS動(dòng)畫(huà)
文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
SGTools--動(dòng)畫(huà)控件--屏幕實(shí)現(xiàn)動(dòng)畫(huà)顯示 就是這么簡(jiǎn)單
【M-K1HSE開(kāi)發(fā)板免費(fèi)體驗(yàn)】相關(guān)源碼之閱讀和分析1-使用XComponent + Vsync 實(shí)現(xiàn)自定義動(dòng)畫(huà)
分享---儲(chǔ)能UI界面能量流動(dòng)動(dòng)畫(huà)實(shí)現(xiàn)方法
【EASY EAI Orin Nano開(kāi)發(fā)板試用體驗(yàn)】使用和LVGL的anim(簡(jiǎn)易動(dòng)畫(huà))和animimg(圖像動(dòng)畫(huà))控件組合實(shí)現(xiàn)復(fù)雜的動(dòng)畫(huà)功能
【EASY EAI Orin Nano開(kāi)發(fā)板試用體驗(yàn)】使用Linux posix文件讀取接口和LVGL的animimg(圖像動(dòng)畫(huà))控件實(shí)現(xiàn)動(dòng)畫(huà)播放以及
harmony-utils之CrashUtil,異常相關(guān)工具類
harmony-utils之DeviceUtil,設(shè)備相關(guān)工具類
harmony-utils之DisplayUtil,屏幕相關(guān)工具類
harmony-utils之EmitterUtil,Emitter工具類
harmony-utils之FileUtil,文件相關(guān)工具類
harmony-utils之ImageUtil,圖片相關(guān)工具類
用DeepSeek-R1實(shí)現(xiàn)自動(dòng)生成Manim動(dòng)畫(huà)

評(píng)論