Immediate Delivery of Explicit Animation (animateToImmediately) (System API)

The animateToImmediately API implements immediate delivery for explicit animations. When multiple property animations are loaded at once, you can call this API to immediately execute the transition animation for state changes caused by the specified closure function.

System API: This is a system API and cannot be called by third-party applications.

Note:

This feature is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version.

animateToImmediately(value: AnimateParam, event: () => void): void

Parameter Type Mandatory Description
value AnimateParam Yes Animation settings.
event () => void Yes Closure function that displays the animation. The system automatically inserts a transition animation for state changes caused by the closure function.

Example

// xxx.ets
@Entry
@Component
struct AnimateToImmediatelyExample {
  @State widthSize: number = 250
  @State heightSize: number = 100
  @State opacitySize: number = 0
  private flag: boolean = true

  build() {
    Column() {
      Column()
      .width(this.widthSize)
      .height(this.heightSize)
      .backgroundColor(Color.Green)
      .opacity(this.opacitySize)
      Button('change size')
        .margin(30)
        .onClick(() => {
          if (this.flag) {
            animateToImmediately({
              delay: 0,
              duration: 1000
            }, () => {
              this.opacitySize = 1
            })
            animateTo({
              delay: 1000,
              duration: 1000
            }, () => {
              this.widthSize = 150
              this.heightSize = 60
            })
          } else {
            animateToImmediately({
              delay: 0,
              duration: 1000
            }, () => {
              this.widthSize = 250
              this.heightSize = 100
            })
            animateTo({
              delay: 1000,
              duration: 1000
            }, () => {
              this.opacitySize = 0
            })
          }
          this.flag = !this.flag
        })
    }.width('100%').margin({ top: 5 })
  }
}

animation1