Property Animation APIs

ArkUI provides two types of APIs, namely, animateTo and animation, to implement a property animation – a visual illusion of continuity created by driving component attributes to change over time based on animation parameters such as the animation curve.

Property Animation API Scope Principle Application Scenario
animateTo GUI changes caused by attribute changes in closures.
Transition for appearance and disappearance.
This API is a common function. It animates the differences between the UIs before and after the state variable in the closure is changed.
This API can be called multiple times and can be nested.
- A single set of animation parameters is used to animate multiple attributes.
- Animations need to be nested.
animation GUI changes caused by attribute changes bound to components through attribute APIs. This API identifies the change of the animatable properties of a component and automatically adds an animation.
As the API call sequence of the component is from bottom to top, this API applies only to the properties above it.
In a component, you can set animation for individual properties based on the API call sequence.
Different sets of animation parameters are used to animate different properties.

animateTo

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

Among the parameters of animateTo, value indicates the animation parameters (including duration and curve); event indicates the closure of the animation. Property animations generated by variable changes within a closure follow the same animation parameters.

import curves from '@ohos.curves'

@Entry
@Component
struct AnimateToDemo {
  @State animate: boolean = false;
  // Step 1: Declare related state variables.
  @State rotateValue: number = 0; // Rotation angle of component 1.
  @State translateX: number = 0; // Offset of component 2
  @State opacityValue: number = 1; // Opacity of component 2.

  // Step 2: Set the declared state variables to the related animatable property APIs.
  build() {
    Row() {
      // Component 1
      Column() {
      }
      .rotate({ angle: this.rotateValue })
      // Step 3: Enable property animation.
      .animation({ curve: curves.springMotion() })
      .backgroundColor('#317AF7')
      .justifyContent(FlexAlign.Center)
      .width(100)
      .height(100)
      .borderRadius(30)
      .onClick(() => {
        animateTo({ curve: curves.springMotion() }, () => {
          this.animate = !this.animate;
          // Step 4: Change the state variables in the closure to update the UI.
          // You can write any logic that can change the UI, such as array adding and visibility control. The system detects the differences between the new UI and the previous UI and adds animations for the differences.
          // The rotate attribute of component 1 is changed. Therefore, a rotate animation is added to component 1.
          this.rotateValue = this.animate ? 90 : 0;
          // The scale attribute of component 2 is changed. Therefore, a scale animation is added to component 2.
          this.opacityValue = this.animate ? 0.6 : 1;
          // The offset attribute of component 2 is changed. Therefore, an offset animation is added to component 2.
          this.translateX = this.animate ? 50 : 0;
        })
      })

      // Component 2
      Column() {

      }
      .justifyContent(FlexAlign.Center)
      .width(100)
      .height(100)
      .backgroundColor('#D94838')
      .borderRadius(30)
      .opacity(this.opacityValue)
      .translate({ x: this.translateX })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

en-us_image_0000001599958466

animation

Unlike the animateTo API, the animation API does not need to use a closure. Just add it to the end of the target animatable property, and the API will automatically add a property animation once it detects that the bound attribute is changed.

import curves from '@ohos.curves'

@Entry
@Component
struct AnimationDemo {
  @State animate: boolean = false;
  // Step 1: Declare related state variables.
  @State rotateValue: number = 0; // Rotation angle of component 1.
  @State translateX: number = 0; // Offset of component 2
  @State opacityValue: number = 1; // Opacity of component 2.

  // Step 2: Set the declared state variables to the related animatable property APIs.
  build() {
    Row() {
      // Component 1
      Column() {
      }
      .opacity(this.opacityValue)
      .rotate({ angle: this.rotateValue })
      // Step 3: Enable property animation.
      .animation({ curve: curves.springMotion() })
      .backgroundColor('#317AF7')
      .justifyContent(FlexAlign.Center)
      .width(100)
      .height(100)
      .borderRadius(30)
      .onClick(() => {
        this.animate = !this.animate;
        // Step 4: Change the state variables in the closure to update the UI.
        // You can write any logic that can change the UI, such as array adding and visibility control. The system detects the differences between the new UI and the previous UI and adds animations for the differences.
        // The rotate attribute of component 1 is changed. Therefore, a rotate animation is added to component 1.
        this.rotateValue = this.animate ? 90 : 0;
        // The offset attribute of component 2 is changed. Therefore, an offset animation is added to component 2.
        this.translateX = this.animate ? 50 : 0;
        // The opacity attribute of the parent component <Column> is changed, which results in an opacity change of its child components. Therefore, opacity animations are added to <Column> and its child components.
        this.opacityValue = this.animate ? 0.6 : 1;
      })

      // Component 2
      Column() {
      }
      .justifyContent(FlexAlign.Center)
      .width(100)
      .height(100)
      .backgroundColor('#D94838')
      .borderRadius(30)
      .opacity(this.opacityValue)
      .translate({ x: this.translateX })
      .animation({ curve: curves.springMotion() })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

en-us_image_0000001649279705

NOTE

  • When an animation is applied to the position or size change of a component, as layout measurement is involved, performance overheads are high. To reduce performance overheads, use the scale instead, whose value change does not involve layout measurement. This practice is applicable where the component location and size change continuously, for example, where the component size changes as a response to gestures.

  • Property animations should be applied to the components that are always present. For the components that may appear or disappear, use the transition animation.

  • Avoid using animation end callbacks, which are invoked when an animation ends. Property animations are applied to states that have occurred, and therefore you do not need to process the end logic. If animation end callbacks are needed, be sure to handle the data management of continuous operations correctly.