管理中心
我的

您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明。

指南与API参考API参考应用框架ArkUI(方舟UI框架)ArkTS组件动画属性动画 (animation)

属性动画 (animation)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+
本文导读

组件的某些通用属性变化时,若不设置动画,属性变化会直接跳变到目标值。通过属性动画可实现渐变过渡效果,使界面变化更加自然流畅。支持的属性包括width、height、backgroundColor、opacity、scale、rotate、translate等。对于改变布局类属性(如宽高)的动画,内容通常会直接跳变到最终状态,例如文字或Canvas中的内容。如果希望内容跟随宽高变化,可以使用renderFit属性进行配置。

说明

从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

animation

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

animation(value:AnimateParam): T

设置组件的属性动画,当组件的通用属性发生变化时,按照AnimateParam参数配置对属性变化过程进行渐变过渡。

说明
  • 在单一页面上同时存在数十个及以上应用动效的组件时,可以使用renderGroup方法来解决卡顿问题,从而提升动画性能。最佳实践请参考优化动画性能中的使用renderGroup。

  • 该接口不支持在attributeModifier中调用,在attributeModifier中调用animation不会产生动画效果。如需在attributeModifier中实现属性变化动画,请使用显式动画(animateTo)替代。

  • 仅对部分通用属性生效(包括width、height、backgroundColor、opacity、scale、rotate、translate等)。对于改变布局类属性(如宽高)的动画,组件内容(如文字或Canvas中的内容)通常会直接跳转到最终状态。如果希望内容跟随宽高变化平滑过渡,可以配合使用renderFit属性进行配置,建议将renderFit设置为RenderFit.CENTER或RenderFit.TOP_LEFT等值,使内容在动画过程中随组件尺寸同步变化。

卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

展开
参数名 类型 必填 说明
value AnimateParam 是 设置动画效果相关参数,各属性的取值范围及含义详见AnimateParam对象说明。

返回值:

展开
类型 说明
T 返回当前组件,用于链式调用。

属性动画只对写在animation前面的属性生效,产生渐变过渡效果;写在animation之后的属性变化不会有渐变动画效果,而是直接跳变到目标值。组件构造器的属性(如Column构造器的space参数、List构造器的initialIndex参数等在组件构造时传入的参数)不支持动画效果,仅对通过属性方法(如.width()、.height()、.backgroundColor()等)设置的属性生效。

收起
自动换行
深色代码主题
复制
  1. @Entry
  2. @Component
  3. struct AnimationExample {
  4. @State widthSize: number = 250;
  5. @State heightSize: number = 100;
  6. @State rotateAngle: number = 0;
  7. @State flag: boolean = true;
  8. @State space: number = 10;
  9. build() {
  10. Column() {
  11. Column({ space: this.space }) // 改变Column构造器中的space动画不生效
  12. .onClick(() => {
  13. if (this.flag) {
  14. this.widthSize = 150;
  15. this.heightSize = 60;
  16. this.space = 20; // 改变this.space动画不生效
  17. } else {
  18. this.widthSize = 250;
  19. this.heightSize = 100;
  20. this.space = 10; // 改变this.space动画不生效
  21. }
  22. this.flag = !this.flag;
  23. })
  24. .backgroundColor(Color.Black)
  25. .margin(30)
  26. .width(this.widthSize) // 只有写在animation前面才生效
  27. .height(this.heightSize) // 只有写在animation前面才生效
  28. // 为按钮尺寸变化配置缓出曲线,重复播放3次
  29. .animation({
  30. duration: 2000,
  31. curve: Curve.EaseOut,
  32. iterations: 3,
  33. playMode: PlayMode.Normal
  34. })
  35. // .width(this.widthSize) // 动画不生效
  36. // .height(this.heightSize) // 动画不生效
  37. }
  38. }
  39. }

示例

该示例通过animation实现了组件的属性动画。

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct AttrAnimationExample {
  5. @State widthSize: number = 250
  6. @State heightSize: number = 100
  7. @State rotateAngle: number = 0
  8. @State flag: boolean = true
  9. build() {
  10. Column() {
  11. Button('change size')
  12. .onClick(() => {
  13. if (this.flag) {
  14. this.widthSize = 150
  15. this.heightSize = 60
  16. } else {
  17. this.widthSize = 250
  18. this.heightSize = 100
  19. }
  20. this.flag = !this.flag
  21. })
  22. .margin(30)
  23. .width(this.widthSize)
  24. .height(this.heightSize)
  25. .animation({
  26. duration: 2000,
  27. curve: Curve.EaseOut,
  28. iterations: 3,
  29. playMode: PlayMode.Normal
  30. })
  31. Button('change rotate angle')
  32. .onClick(() => {
  33. this.rotateAngle = 90
  34. })
  35. .margin(50)
  36. .rotate({ angle: this.rotateAngle })
  37. // 为旋转角度变化配置阻尼曲线,延迟500ms启动,无限循环交替播放
  38. .animation({
  39. duration: 1200,
  40. curve: Curve.Friction,
  41. delay: 500,
  42. iterations: -1, // 设置-1表示动画无限循环
  43. playMode: PlayMode.Alternate,
  44. expectedFrameRateRange: {
  45. min: 20,
  46. max: 120,
  47. expected: 90,
  48. }
  49. })
  50. }.width('100%').margin({ top: 20 })
  51. }
  52. }