1# 属性动画 (animation) 2 3组件的某些通用属性变化时,可以通过属性动画实现渐变过渡效果,提升用户体验。支持的属性包括[width](ts-universal-attributes-size.md#width)、[height](ts-universal-attributes-size.md#height)、[backgroundColor](ts-universal-attributes-background.md#backgroundcolor)、[opacity](ts-universal-attributes-opacity.md#opacity)、[scale](ts-universal-attributes-transformation.md#scale)、[rotate](ts-universal-attributes-transformation.md#rotate)、[translate](ts-universal-attributes-transformation.md#translate)等。布局类改变宽高的动画,内容都是直接到终点状态,例如文字、[Canvas](ts-components-canvas-canvas.md#canvas)的内容等,如果要内容跟随宽高变化,可以使用[renderFit](ts-universal-attributes-renderfit.md#renderfit)属性配置。 4 5> **说明:** 6> 7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8> 9> **卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 10 11## 接口 12 13animation(value:AnimateParam) 14 15**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 16 17**参数:** 18| 参数 | 类型 | 是否必填 | 描述 | 19| ----- | --------------------------------- | ---- | ------------------------------------- | 20| value | [AnimateParam](ts-explicit-animation.md#animateparam对象说明) | 是 | 设置动画效果相关参数。 | 21 22属性动画只对写在animation前面的属性生效,且对组件构造器的属性不生效。 23 ``` 24@State widthSize: number = 250; 25@State heightSize: number = 100; 26@State rotateAngle: number = 0; 27@State flag: boolean = true; 28@State space: number = 10; 29// ... 30Column({ space: this.space }) // 改变Column构造器中的space动画不生效 31 .onClick(() => { 32 if (this.flag) { 33 this.widthSize = 150; 34 this.heightSize = 60; 35 this.space = 20; // 改变this.space动画不生效 36 } else { 37 this.widthSize = 250; 38 this.heightSize = 100;; 39 this.space = 10; // 改变this.space动画不生效 40 } 41 this.flag = !this.flag; 42 }) 43 .margin(30) 44 .width(this.widthSize) // 只有写在animation前面才生效 45 .height(this.heightSize) // 只有写在animation前面才生效 46 .animation({ 47 duration: 2000, 48 curve: Curve.EaseOut, 49 iterations: 3, 50 playMode: PlayMode.Normal 51 }) 52 // .width(this.widthSize) // 动画不生效 53 // .height(this.heightSize) // 动画不生效 54``` 55 56## 示例 57 58该示例通过animation实现了组件的属性动画。 59 60```ts 61// xxx.ets 62@Entry 63@Component 64struct AttrAnimationExample { 65 @State widthSize: number = 250 66 @State heightSize: number = 100 67 @State rotateAngle: number = 0 68 @State flag: boolean = true 69 70 build() { 71 Column() { 72 Button('change size') 73 .onClick(() => { 74 if (this.flag) { 75 this.widthSize = 150 76 this.heightSize = 60 77 } else { 78 this.widthSize = 250 79 this.heightSize = 100 80 } 81 this.flag = !this.flag 82 }) 83 .margin(30) 84 .width(this.widthSize) 85 .height(this.heightSize) 86 .animation({ 87 duration: 2000, 88 curve: Curve.EaseOut, 89 iterations: 3, 90 playMode: PlayMode.Normal 91 }) 92 Button('change rotate angle') 93 .onClick(() => { 94 this.rotateAngle = 90 95 }) 96 .margin(50) 97 .rotate({ angle: this.rotateAngle }) 98 .animation({ 99 duration: 1200, 100 curve: Curve.Friction, 101 delay: 500, 102 iterations: -1, // 设置-1表示动画无限循环 103 playMode: PlayMode.Alternate, 104 expectedFrameRateRange: { 105 min: 20, 106 max: 120, 107 expected: 90, 108 } 109 }) 110 }.width('100%').margin({ top: 20 }) 111 } 112} 113``` 114 115