動畫的原理是在一個時間段內(nèi),多次改變UI外觀,由于人眼會產(chǎn)生視覺暫留,所以最終看到的就是一個“連續(xù)”的動畫。UI的一次改變稱為一個動畫幀,對應一次屏幕刷新,而決定動畫流暢度的一個重要指標就是幀率FPS(Frame Per Second),即每秒的動畫幀數(shù),幀率越高則動畫就會越流暢。
ArkUI中,產(chǎn)生動畫的方式是改變屬性值且指定動畫參數(shù)。動畫參數(shù)包含了如動畫時長、變化規(guī)律(即曲線)等參數(shù)。當屬性值發(fā)生變化后,按照動畫參數(shù),從原來的狀態(tài)過渡到新的狀態(tài),即形成一個動畫。
ArkUI提供的動畫能力按照頁面的分類方式,可分為頁面內(nèi)的動畫和頁面間的動畫。如下圖所示,頁面內(nèi)的動畫指在一個頁面內(nèi)即可發(fā)生的動畫,頁面間的動畫指兩個頁面跳轉(zhuǎn)時才會發(fā)生的動畫。

圖1 按照頁面分類的動畫

如果按照基礎能力分,可分為屬性動畫、顯式動畫、轉(zhuǎn)場動畫三部分。如下圖所示。

圖2 按照基礎能力分類的動畫

使用顯式動畫產(chǎn)生布局更新動畫
顯式動畫的接口為:
animateTo(value: AnimateParam, event: () => void): void
第一個參數(shù)指定動畫參數(shù),第二個參數(shù)為動畫的閉包函數(shù)。
以下是使用顯式動畫產(chǎn)生布局更新動畫的示例。示例中,當Column組件的alignItems屬性改變后,其子組件的布局位置結(jié)果發(fā)生變化。只要該屬性是在animateTo的閉包函數(shù)中修改的,那么由其引起的所有變化都會按照animateTo的動畫參數(shù)執(zhí)行動畫過渡到終點值。
@Entry
@Component
struct LayoutChange {
// 用于控制Column的alignItems屬性
@State itemAlign: HorizontalAlign = HorizontalAlign.Start;
allAlign: HorizontalAlign[] = [HorizontalAlign.Start, HorizontalAlign.Center, HorizontalAlign.End];
alignIndex: number = 0;
build() {
Column() {
Column({ space: 10 }) {
Button("1").width(100).height(50)
Button("2").width(100).height(50)
Button("3").width(100).height(50)
}
.margin(20)
.alignItems(this.itemAlign)
.borderWidth(2)
.width("90%")
.height(200)
Button("click").onClick(() => {
// 動畫時長為1000ms,曲線為EaseInOut
animateTo({ duration: 1000, curve: Curve.EaseInOut }, () => {
this.alignIndex = (this.alignIndex + 1) % this.allAlign.length;
// 在閉包函數(shù)中修改this.itemAlign參數(shù),使Column容器內(nèi)部孩子的布局方式變化,使用動畫過渡到新位置
this.itemAlign = this.allAlign[this.alignIndex];
});
})
}
.width("100%")
.height("100%")
}
}
除直接改變布局方式外,也可直接修改組件的寬、高、位置。
@Entry
@Component
struct LayoutChange2 {
@State myWidth: number = 100;
@State myHeight: number = 50;
// 標志位,true和false分別對應一組myWidth、myHeight值
@State flag: boolean = false;
build() {
Column({ space: 10 }) {
Button("text")
.type(ButtonType.Normal)
.width(this.myWidth)
.height(this.myHeight)
.margin(20)
Button("area: click me")
.fontSize(12)
.margin(20)
.onClick(() => {
animateTo({ duration: 1000, curve: Curve.Ease }, () => {
// 動畫閉包中根據(jù)標志位改變控制第一個Button寬高的狀態(tài)變量,使第一個Button做寬高動畫
if (this.flag) {
this.myWidth = 100;
this.myHeight = 50;
} else {
this.myWidth = 200;
this.myHeight = 100;
}
this.flag = !this.flag;
});
})
}
.width("100%")
.height("100%")
}
}
另一種方式是給第二個Button添加布局約束,如position的位置約束,使其位置不被第一個Button的寬高影響。核心代碼如下:
Column({ space: 10 }) {
Button("text")
.type(ButtonType.Normal)
.width(this.myWidth)
.height(this.myHeight)
.margin(20)
Button("area: click me")
.fontSize(12)
// 配置position屬性固定,使自己的布局位置不被第一個Button的寬高影響
.position({ x: "30%", y: 200 })
.onClick(() => {
animateTo({ duration: 1000, curve: Curve.Ease }, () => {
// 動畫閉包中根據(jù)標志位改變控制第一個Button寬高的狀態(tài)變量,使第一個Button做寬高動畫
if (this.flag) {
this.myWidth = 100;
this.myHeight = 50;
} else {
this.myWidth = 200;
this.myHeight = 100;
}
this.flag = !this.flag;
});
})
}
.width("100%")
.height("100%")
使用屬性動畫產(chǎn)生布局更新動畫
顯式動畫把要執(zhí)行動畫的屬性的修改放在閉包函數(shù)中觸發(fā)動畫,而屬性動畫則無需使用閉包,把animation屬性加在要做屬性動畫的組件的屬性后即可。
屬性動畫的接口為:
animation(value: AnimateParam)
其入?yún)閯赢媴?shù)。想要組件隨某個屬性值的變化而產(chǎn)生動畫,此屬性需要加在animation屬性之前。有的屬性變化不希望通過animation產(chǎn)生屬性動畫,可以放在animation之后。上面顯式動畫的示例很容易改為用屬性動畫實現(xiàn)。例如:
@Entry
@Component
struct LayoutChange2 {
@State myWidth: number = 100;
@State myHeight: number = 50;
@State flag: boolean = false;
@State myColor: Color = Color.Blue;
build() {
Column({ space: 10 }) {
Button("text")
.type(ButtonType.Normal)
.width(this.myWidth)
.height(this.myHeight)
// animation只對其上面的type、width、height屬性生效,時長為1000ms,曲線為Ease
.animation({ duration: 1000, curve: Curve.Ease })
// animation對下面的backgroundColor、margin屬性不生效
.backgroundColor(this.myColor)
.margin(20)
Button("area: click me")
.fontSize(12)
.onClick(() => {
// 改變屬性值,配置了屬性動畫的屬性會進行動畫過渡
if (this.flag) {
this.myWidth = 100;
this.myHeight = 50;
this.myColor = Color.Blue;
} else {
this.myWidth = 200;
this.myHeight = 100;
this.myColor = Color.Pink;
}
this.flag = !this.flag;
})
}
}
}
上述示例中,第一個button上的animation屬性,只對寫在animation之前的type、width、height屬性生效,而對寫在animation之后的backgroundColor、margin屬性無效。運行結(jié)果是width、height屬性會按照animation的動畫參數(shù)執(zhí)行動畫,而backgroundColor會直接跳變,不會產(chǎn)生動畫
審核編輯 黃宇
-
鴻蒙
+關注
關注
60文章
2963瀏覽量
45894
發(fā)布評論請先 登錄
想體驗鴻蒙生態(tài),該怎么獲取鴻蒙開發(fā)板?有哪些途徑?
【M-K1HSE開發(fā)板免費體驗】相關源碼之閱讀和分析1-使用XComponent + Vsync 實現(xiàn)自定義動畫
【HarmonyOS 5】金融應用開發(fā)鴻蒙組件實踐
鴻蒙5開發(fā)寶藏案例分享---一多開發(fā)實例(音樂)
鴻蒙5開發(fā)寶藏案例分享---優(yōu)化應用時延問題
鴻蒙5開發(fā)寶藏案例分享---Web頁面內(nèi)點擊響應時延分析
鴻蒙5開發(fā)寶藏案例分享---分析幀率問題
鴻蒙5開發(fā)寶藏案例分享---點擊完成時延分析
鴻蒙5開發(fā)寶藏案例分享---性能體驗設計
鴻蒙5開發(fā)寶藏案例分享---體驗流暢的首頁信息流
2025開源鴻蒙開發(fā)者大會圓滿落幕
DevEco Studio AI輔助開發(fā)工具兩大升級功能 鴻蒙應用開發(fā)效率再提升
DialogHub上線OpenHarmony開源社區(qū),高效開發(fā)鴻蒙應用彈窗
鴻蒙北向開發(fā)OpenHarmony5.0 DevEco Studio開發(fā)工具安裝與配置
鴻蒙開發(fā)之發(fā)動畫篇
評論