#鸿蒙通关秘籍#鸿蒙系统中如何使用系统提供的API进行动画优化?

HarmonyOS
2024-12-02 14:28:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
DB梦绘江

在鸿蒙系统中,为了提高动画的流畅性并减少因丢帧造成的卡顿,采用系统提供的API来优化动画是一个非常好的选择。以下是使用两种不同API的具体步骤:

  1. 属性动效API实现:

    使用这个API,只需定义动画的基础参数,如持续时间、动画曲线、延迟等,可以轻松对UI组件实现动画效果。

    @Entry
    @Component
    struct AttrAnimationExample1 {
      @State widthSize: number = 200
      @State heightSize: number = 100
      @State flag: boolean = true
    
      build() {
        Column() {
          Button('click me')
            .onClick((event?: ClickEvent | undefined) => {
              if (this.flag) {
                this.widthSize = 100
                this.heightSize = 50
              } else {
                this.widthSize = 200
                this.heightSize = 100
              }
              this.flag = !this.flag
            })
            .width(this.widthSize).height(this.heightSize).backgroundColor(0x317aff)
            .animation({
              duration: 2000,
              curve: Curve.Linear,
              delay: 500,
              iterations: 1,
              playMode: PlayMode.Normal,
            })
        }.width('100%').margin({ top: 5 })
      }
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
  2. 显式动效API实现:

    显式动效API可以通过定义动画动作的方式,直接控制动画过程中的属性变化。

    @Entry
    @Component
    struct AnimateToExample2 {
      @State widthSize: number = 200;
      @State heightSize: number = 100;
      @State flag: boolean = true;
    
      build() {
        Column() {
          Button('click me')
            .onClick((event?: ClickEvent | undefined) => {
              if (this.flag) {
                animateTo({
                  duration: 2000,
                  curve: Curve.Linear,
                  delay: 500,
                  iterations: 1,
                  playMode: PlayMode.Normal,
                }, () => {
                  this.widthSize = 100;
                  this.heightSize = 50;
                })
              } else {
                animateTo({
                  duration: 2000,
                  curve: Curve.Linear,
                  delay: 500,
                  iterations: 1,
                  playMode: PlayMode.Normal,
                }, () => {
                  this.widthSize = 200;
                  this.heightSize = 100;
                })
              }
              this.flag = !this.flag;
            })
            .width(this.widthSize).height(this.heightSize).backgroundColor(0x317aff)
        }.width('100%').margin({ top: 5 })
      }
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.

通过使用上述API,减少繁杂的自定义计算过程,从而减少UI主线程的负载,提高动画的流畅度。

分享
微博
QQ
微信
回复
2024-12-02 16:50:07
相关问题
如何鸿蒙系统进行应用性能优化?
1954浏览 • 2回复 待解决