動畫的原理是在一個時間段內,多次改變UI外觀,由于人眼會產生視覺暫留,所以最終看到的就是一個“連續”的動畫。UI的一次改變稱為一個動畫幀,對應一次屏幕刷新,而決定動畫流暢度的一個重要指標就是幀率FPS(Frame Per Second),即每秒的動畫幀數,幀率越高則動畫就會越流暢。
ArkUI中,產生動畫的方式是改變屬性值且指定動畫參數。動畫參數包含了如動畫時長、變化規律(即曲線)等參數。當屬性值發生變化后,按照動畫參數,從原來的狀態過渡到新的狀態,即形成一個動畫。
ArkUI提供的動畫能力按照頁面的分類方式,可分為頁面內的動畫和頁面間的動畫。如下圖所示,頁面內的動畫指在一個頁面內即可發生的動畫,頁面間的動畫指兩個頁面跳轉時才會發生的動畫。
圖1 按照頁面分類的動畫
如果按照基礎能力分,可分為屬性動畫、顯式動畫、轉場動畫三部分。如下圖所示。
圖2 按照基礎能力分類的動畫
使用顯式動畫產生布局更新動畫
顯式動畫的接口為:
animateTo(value: AnimateParam, event: () => void): void
第一個參數指定動畫參數,第二個參數為動畫的閉包函數。
以下是使用顯式動畫產生布局更新動畫的示例。示例中,當Column組件的alignItems屬性改變后,其子組件的布局位置結果發生變化。只要該屬性是在animateTo的閉包函數中修改的,那么由其引起的所有變化都會按照animateTo的動畫參數執行動畫過渡到終點值。
@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; // 在閉包函數中修改this.itemAlign參數,使Column容器內部孩子的布局方式變化,使用動畫過渡到新位置 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 }, () => { // 動畫閉包中根據標志位改變控制第一個Button寬高的狀態變量,使第一個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 }, () => { // 動畫閉包中根據標志位改變控制第一個Button寬高的狀態變量,使第一個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%")
使用屬性動畫產生布局更新動畫
顯式動畫把要執行動畫的屬性的修改放在閉包函數中觸發動畫,而屬性動畫則無需使用閉包,把animation屬性加在要做屬性動畫的組件的屬性后即可。
屬性動畫的接口為:
animation(value: AnimateParam)
其入參為動畫參數。想要組件隨某個屬性值的變化而產生動畫,此屬性需要加在animation屬性之前。有的屬性變化不希望通過animation產生屬性動畫,可以放在animation之后。上面顯式動畫的示例很容易改為用屬性動畫實現。例如:
@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屬性無效。運行結果是width、height屬性會按照animation的動畫參數執行動畫,而backgroundColor會直接跳變,不會產生動畫
審核編輯 黃宇
-
鴻蒙
+關注
關注
57文章
2395瀏覽量
43091
發布評論請先 登錄
相關推薦
【書籍評測活動NO.56】極速探索HarmonyOS NEXT:純血鴻蒙應用開發實踐
鴻蒙Flutter實戰:07混合開發
鴻蒙OpenHarmony南向/北向快速開發教程-迅為RK3568開發板
哪吒汽車APP啟動鴻蒙原生應用開發
鴻蒙開發就業前景到底怎么樣?
OpenHarmony實戰開發-如何實現動畫幀
鴻蒙OS崛起,鴻蒙應用開發工程師成市場新寵
OpenHarmony實戰開發-如何實現組件動畫。
![OpenHarmony實戰<b class='flag-5'>開發</b>-如何實現組件<b class='flag-5'>動畫</b>。](https://file1.elecfans.com/web2/M00/DF/14/wKgaomYt_x2Ab_qaAAEJlEQIlYw492.jpg)
評論