1# @ohos.animator (动画) 2 3本模块提供组件动画效果,包括定义动画、启动动画和以相反的顺序播放动画等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 该模块不支持在[UIAbility](./js-apis-app-ability-uiAbility.md)中使用,需要在创建组件实例后使用。 10 11 12## 导入模块 13 14```js 15import animator from '@ohos.animator'; 16``` 17## create<sup>9+</sup> 18 19create(options: AnimatorOptions): AnimatorResult 20 21定义Animator类。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25**参数:** 26 27| 参数名 | 类型 | 必填 | 说明 | 28| ------- | ----------------------------------- | ---- | ------- | 29| options | [AnimatorOptions](#animatoroptions) | 是 | 定义动画选项。 | 30 31**返回值:** 32 33| 类型 | 说明 | 34| --------------------------------- | ------------- | 35| [AnimatorResult](#animatorresult) | Animator结果接口。 | 36 37**示例:** 38 39 ```js 40 let options = { 41 duration: 1500, 42 easing: "friction", 43 delay: 0, 44 fill: "forwards", 45 direction: "normal", 46 iterations: 3, 47 begin: 200.0, 48 end: 400.0 49 }; 50 animator.create(options); 51 ``` 52 53## AnimatorResult 54 55定义Animator结果接口。 56 57### reset<sup>9+</sup> 58 59reset(options: AnimatorOptions): void 60 61更新当前动画器。 62 63**系统能力:** SystemCapability.ArkUI.ArkUI.Full 64 65**参数:** 66 67| 参数名 | 类型 | 必填 | 说明 | 68| ------- | ----------------------------------- | ---- | ------- | 69| options | [AnimatorOptions](#animatoroptions) | 是 | 定义动画选项。 | 70 71**错误码:** 72 73以下错误码的详细介绍请参见[ohos.animator(动画)](../errorcodes/errorcode-animator.md)错误码。 74 75| 错误码ID | 错误信息 | 76| --------- | ------- | 77| 100001 | if no page is found for pageId or fail to get object property list. | 78 79 80**示例:** 81 82```js 83let options = { 84 duration: 1500, 85 easing: "friction", 86 delay: 0, 87 fill: "forwards", 88 direction: "normal", 89 iterations: 3, 90 begin: 200.0, 91 end: 400.0 92}; 93try { 94 animator.reset(options); 95} catch(error) { 96 console.error(`Animator reset failed, error code: ${error.code}, message: ${error.message}.`); 97} 98``` 99 100### play 101 102play(): void 103 104启动动画。动画会保留上一次的播放状态,比如播放状态设置reverse后,再次播放会保留reverse的播放状态。 105 106**系统能力:** SystemCapability.ArkUI.ArkUI.Full 107 108**示例:** 109 110```js 111animator.play(); 112``` 113 114### finish 115 116finish(): void 117 118结束动画。 119 120**系统能力:** SystemCapability.ArkUI.ArkUI.Full 121 122**示例:** 123 124```js 125animator.finish(); 126``` 127 128### pause 129 130pause(): void 131 132暂停动画。 133 134**系统能力:** SystemCapability.ArkUI.ArkUI.Full 135 136**示例:** 137 138```js 139animator.pause(); 140``` 141 142### cancel 143 144cancel(): void 145 146删除动画。 147 148**系统能力:** SystemCapability.ArkUI.ArkUI.Full 149 150**示例:** 151 152```js 153animator.cancel(); 154``` 155 156### reverse 157 158reverse(): void 159 160以相反的顺序播放动画。 161 162**系统能力:** SystemCapability.ArkUI.ArkUI.Full 163 164**示例:** 165 166```js 167animator.reverse(); 168``` 169 170### onframe 171 172onframe: (progress: number) => void 173 174接收到帧时回调。 175 176**系统能力:** SystemCapability.ArkUI.ArkUI.Full 177 178**参数:** 179 180| 参数名 | 类型 | 必填 | 说明 | 181| -------- | ------ | ---- | -------- | 182| progress | number | 是 | 动画的当前进度。 | 183 184**示例:** 185 186```js 187let animatorResult = animator.create(options) 188animatorResult.onframe = function(value) { 189 console.info("onframe callback") 190} 191``` 192 193### onfinish 194 195onfinish: () => void 196 197动画完成时回调。 198 199**系统能力:** SystemCapability.ArkUI.ArkUI.Full 200 201**示例:** 202 203```js 204let animatorResult = animator.create(options) 205animatorResult.onfinish = function() { 206 console.info("onfinish callback") 207} 208``` 209 210### oncancel 211 212oncancel: () => void 213 214动画被取消时回调。 215 216**系统能力:** SystemCapability.ArkUI.ArkUI.Full 217 218**示例:** 219 220```js 221let animatorResult = animator.create(options) 222animatorResult.oncancel = function() { 223 console.info("oncancel callback") 224} 225``` 226 227### onrepeat 228 229onrepeat: () => void 230 231动画重复时回调。 232 233**系统能力:** SystemCapability.ArkUI.ArkUI.Full 234 235**示例:** 236 237```js 238let animatorResult = animator.create(options) 239animatorResult.onrepeat = function() { 240 console.info("onrepeat callback") 241} 242``` 243 244 245 246## AnimatorOptions 247 248定义动画选项。 249 250**系统能力:** SystemCapability.ArkUI.ArkUI.Full 251 252| 名称 | 类型 | 必填 | 说明 | 253| ---------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 254| duration | number | 是 | 动画播放的时长,单位毫秒。 | 255| easing | string | 是 | 动画插值曲线,仅支持以下可选值:<br/>"linear":动画线性变化。<br/>"ease":动画开始和结束时的速度较慢,cubic-bezier(0.25、0.1、0.25、1.0)。<br/>"ease-in":动画播放速度先慢后快,cubic-bezier(0.42, 0.0, 1.0, 1.0)。<br/>"ease-out":动画播放速度先快后慢,cubic-bezier(0.0, 0.0, 0.58, 1.0)。<br/>"ease-in-out":动画播放速度先加速后减速,cubic-bezier(0.42, 0.0, 0.58, 1.0)。<br/>"fast-out-slow-in":标准曲线,cubic-bezier(0.4,0.0,0.2,1.0)。<br/>"linear-out-slow-in":减速曲线,cubic-bezier(0.0,0.0,0.2,1.0)。<br/>"friction":阻尼曲线,cubic-bezier(0.2, 0.0, 0.2, 1.0)。<br/>"extreme-deceleration":急缓曲线,cubic-bezier(0.0, 0.0, 0.0, 1.0)。<br/>"rhythm":节奏曲线,cubic-bezier(0.7, 0.0, 0.2, 1.0)。<br/>"sharp":锐利曲线,cubic-bezier(0.33, 0.0, 0.67, 1.0)。<br/>"smooth":平滑曲线,cubic-bezier(0.4, 0.0, 0.4, 1.0)。<br/>cubic-bezier(x1, y1, x2, y2):在三次贝塞尔函数中定义动画变化过程,入参的值必须处于0-1之间。<br/>steps(number, step-position):阶梯曲线。number必须设置,支持的类型为int。step-position参数可选,支持设置start或end,默认值为end。 | 256| delay | number | 是 | 动画延时播放时长,单位毫秒,设置为0时,表示不延时。 | 257| fill | "none" \| "forwards" \| "backwards" \| "both" | 是 | 动画执行后是否恢复到初始状态,动画执行后,动画结束时的状态(在最后一个关键帧中定义)将保留。<br/>"none":在动画执行之前和之后都不会应用任何样式到目标上。<br/>"forwards":在动画结束后,目标将保留动画结束时的状态(在最后一个关键帧中定义)。<br/>"backwards":动画将在animation-delay期间应用第一个关键帧中定义的值。当animation-direction为"normal"或"alternate"时应用from关键帧中的值,当animation-direction为"reverse"或"alternate-reverse"时应用to关键帧中的值。<br/>"both":动画将遵循forwards和backwards的规则,从而在两个方向上扩展动画属性。 | 258| direction | "normal" \| "reverse" \| "alternate" \| "alternate-reverse" | 是 | 动画播放模式。<br/>"normal": 动画正向循环播放。<br/>"reverse": 动画反向循环播放。<br/>"alternate":动画交替循环播放,奇数次正向播放,偶数次反向播放。<br/>"alternate-reverse":动画反向交替循环播放,奇数次反向播放,偶数次正向播放。 | 259| iterations | number | 是 | 动画播放次数。设置为0时不播放,设置为-1时无限次播放。<br/>**说明:** 设置为除-1外其他负数视为无效取值,无效取值动画默认播放1次。 | 260| begin | number | 是 | 动画插值起点。 | 261| end | number | 是 | 动画插值终点。 | 262 263 264## 完整示例 265### 基于JS扩展的类Web开发范式 266 267```html 268<!-- hml --> 269<div class="container"> 270 <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show"> 271 </div> 272</div> 273``` 274 275```js 276export default { 277 data: { 278 divWidth: 200, 279 divHeight: 200, 280 animator: null 281 }, 282 onInit() { 283 let options = { 284 duration: 1500, 285 easing: "friction", 286 delay: 0, 287 fill: "forwards", 288 direction: "normal", 289 iterations: 2, 290 begin: 200.0, 291 end: 400.0 292 }; 293 this.animator = animator.create(options); 294 }, 295 Show() { 296 let options1 = { 297 duration: 1500, 298 easing: "friction", 299 delay: 0, 300 fill: "forwards", 301 direction: "normal", 302 iterations: 2, 303 begin: 0, 304 end: 400.0 305 }; 306 try { 307 this.animator.reset(options1); 308 } catch(error) { 309 console.error(`Animator reset failed, error code: ${error.code}, message: ${error.message}.`); 310 } 311 let _this = this; 312 this.animator.onframe = function(value) { 313 _this.divWidth = value; 314 _this.divHeight = value; 315 }; 316 this.animator.play(); 317 } 318} 319``` 320 321  322 323### 基于TS扩展的声明式开发范式 324 325```ts 326import animator from '@ohos.animator'; 327 328@Entry 329@Component 330struct AnimatorTest { 331 private TAG: string = '[AnimatorTest]' 332 private backAnimator: any = undefined 333 private flag: boolean = false 334 @State wid: number = 100 335 @State hei: number = 100 336 337 create() { 338 let _this = this 339 this.backAnimator = animator.create({ 340 duration: 2000, 341 easing: "ease", 342 delay: 0, 343 fill: "none", 344 direction: "normal", 345 iterations: 1, 346 begin: 100, 347 end: 200 348 }) 349 this.backAnimator.onfinish = function () { 350 _this.flag = true 351 console.info(_this.TAG, 'backAnimator onfinish') 352 } 353 this.backAnimator.onrepeat = function () { 354 console.info(_this.TAG, 'backAnimator repeat') 355 } 356 this.backAnimator.oncancel = function () { 357 console.info(_this.TAG, 'backAnimator cancel') 358 } 359 this.backAnimator.onframe = function (value) { 360 _this.wid = value 361 _this.hei = value 362 } 363 } 364 365 build() { 366 Column() { 367 Column() { 368 Column() 369 .width(this.wid) 370 .height(this.hei) 371 .backgroundColor(Color.Red) 372 } 373 .width('100%') 374 .height(300) 375 376 Column() { 377 Row() { 378 Button('create') 379 .fontSize(30) 380 .fontColor(Color.Black) 381 .onClick(() => { 382 this.create() 383 }) 384 } 385 .padding(10) 386 387 Row() { 388 Button('play') 389 .fontSize(30) 390 .fontColor(Color.Black) 391 .onClick(() => { 392 this.flag = false 393 this.backAnimator.play() 394 }) 395 } 396 .padding(10) 397 398 Row() { 399 Button('pause') 400 .fontSize(30) 401 .fontColor(Color.Black) 402 .onClick(() => { 403 this.backAnimator.pause() 404 }) 405 } 406 .padding(10) 407 408 Row() { 409 Button('finish') 410 .fontSize(30) 411 .fontColor(Color.Black) 412 .onClick(() => { 413 this.flag = true 414 this.backAnimator.finish() 415 }) 416 } 417 .padding(10) 418 419 Row() { 420 Button('reverse') 421 .fontSize(30) 422 .fontColor(Color.Black) 423 .onClick(() => { 424 this.flag = false 425 this.backAnimator.reverse() 426 }) 427 } 428 .padding(10) 429 430 Row() { 431 Button('cancel') 432 .fontSize(30) 433 .fontColor(Color.Black) 434 .onClick(() => { 435 this.backAnimator.cancel() 436 }) 437 } 438 .padding(10) 439 440 Row() { 441 Button('reset') 442 .fontSize(30) 443 .fontColor(Color.Black) 444 .onClick(() => { 445 if (this.flag) { 446 this.flag = false 447 this.backAnimator.reset({ 448 duration: 5000, 449 easing: "ease-in", 450 delay: 0, 451 fill: "none", 452 direction: "normal", 453 iterations: 4, 454 begin: 100, 455 end: 300 456 }) 457 } else { 458 console.info(this.TAG, 'Animation not ended') 459 } 460 }) 461 } 462 .padding(10) 463 } 464 } 465 } 466} 467``` 468 469## update<sup>(deprecated)</sup> 470 471update(options: AnimatorOptions): void 472 473更新当前动画器。 474 475从API version9开始不再维护,建议使用[reset<sup>9+</sup>](#reset9) 476 477**系统能力:** SystemCapability.ArkUI.ArkUI.Full 478 479**参数:** 480 481| 参数名 | 类型 | 必填 | 说明 | 482| ------- | ----------------------------------- | ---- | ------- | 483| options | [AnimatorOptions](#animatoroptions) | 是 | 定义动画选项。 | 484 485**示例:** 486 487```js 488animator.update(options); 489``` 490 491## createAnimator<sup>(deprecated)</sup> 492 493createAnimator(options: AnimatorOptions): AnimatorResult 494 495定义Animator类。 496 497从API version9开始不再维护,建议使用[create<sup>9+</sup>](#create9) 498 499**系统能力:** SystemCapability.ArkUI.ArkUI.Full 500 501**参数:** 502 503| 参数名 | 类型 | 必填 | 说明 | 504| ------- | ----------------------------------- | ---- | ------- | 505| options | [AnimatorOptions](#animatoroptions) | 是 | 定义动画选项。 | 506 507**返回值:** 508 509| 类型 | 说明 | 510| --------------------------------- | ------------- | 511| [AnimatorResult](#animatorresult) | Animator结果接口。 | 512 513**示例:** 514 515```js 516let options = { 517 duration: 1500, 518 easing: "friction", 519 delay: 0, 520 fill: "forwards", 521 direction: "normal", 522 iterations: 3, 523 begin: 200.0, 524 end: 400.0 525}; 526this.animator = animator.createAnimator(options); 527```