1# @ohos.animator (Animator) 2 3The **Animator** module provides APIs for applying animation effects, including defining animations, starting animations, and playing animations in reverse order. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> This module can be used in ArkTS since API version 9. 10> 11> This module cannot be used in the file declaration of the [UIAbility](../apis-ability-kit/js-apis-app-ability-uiAbility.md). In other words, the APIs of this module can be used only after a component instance is created; they cannot be called in the lifecycle of the UIAbility. 12> 13> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where [the UI context is unclear](../../ui/arkts-global-interface.md). For details, see [UIContext](js-apis-arkui-UIContext.md#uicontext). 14 15## Modules to Import 16 17```ts 18import { Animator as animator, AnimatorOptions, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 19``` 20 21## Animator 22 23Creates an **Animator** object. 24 25**Atomic service API**: This API can be used in atomic services since API version 11. 26 27**System capability**: SystemCapability.ArkUI.ArkUI.Full 28 29### create<sup>(deprecated)</sup> 30 31create(options: AnimatorOptions): AnimatorResult 32 33Creates an **AnimatorResult** object for animations. 34 35> **NOTE** 36> 37> This API is supported since API version 9 and deprecated since API version 18. You are advised to use [createAnimator](js-apis-arkui-UIContext.md#createanimator) in [UIContext](js-apis-arkui-UIContext.md#uicontext) instead. 38> 39> Since API version 10, you can use the [createAnimator](js-apis-arkui-UIContext.md#createanimator) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to obtain the UI context. 40 41**Atomic service API**: This API can be used in atomic services since API version 11. 42 43**System capability**: SystemCapability.ArkUI.ArkUI.Full 44 45**Parameters** 46 47| Name | Type | Mandatory | Description | 48| ------- | ----------------------------------- | ---- | ------- | 49| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 50 51**Return value** 52 53| Type | Description | 54| --------------------------------- | ------------- | 55| [AnimatorResult](#animatorresult) | Animator result.| 56 57**Error codes** 58 59For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 60 61| ID| Error Message| 62| ------- | -------- | 63| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 64 65**Example** 66 67> **NOTE** 68> 69> For precise animation control, use the [createAnimator](js-apis-arkui-UIContext.md#createanimator) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to specify the UI context. 70 71<!--deprecated_code_no_check--> 72```ts 73import { Animator as animator, AnimatorOptions } from '@kit.ArkUI'; 74 75let options: AnimatorOptions = { 76 duration: 1500, 77 easing: "friction", 78 delay: 0, 79 fill: "forwards", 80 direction: "normal", 81 iterations: 3, 82 begin: 200.0, 83 end: 400.0 84}; 85animator.create(options); // You are advised to use UIContext.createAnimator(). 86``` 87 88### create<sup>18+</sup> 89 90create(options: AnimatorOptions \| [SimpleAnimatorOptions](#simpleanimatoroptions18)): AnimatorResult 91 92Creates an **AnimatorResult** object for animations. Compared with [create](#createdeprecated), this API accepts parameters of the [SimpleAnimatorOptions](#simpleanimatoroptions18) type. 93 94**Atomic service API**: This API can be used in atomic services since API version 18. 95 96**System capability**: SystemCapability.ArkUI.ArkUI.Full 97 98**Parameters** 99 100| Name | Type | Mandatory | Description | 101| ------- | ----------------------------------- | ---- | ------- | 102| options | [AnimatorOptions](#animatoroptions) \| [SimpleAnimatorOptions](#simpleanimatoroptions18) | Yes | Parameters of the animation.| 103 104**Return value** 105 106| Type | Description | 107| --------------------------------- | ------------- | 108| [AnimatorResult](#animatorresult) | Animator result.| 109 110**Error codes** 111 112For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 113 114| ID| Error Message| 115| ------- | -------- | 116| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 117 118**Example** 119 120> **NOTE** 121> 122> For precise animation control, use the [createAnimator](js-apis-arkui-UIContext.md#createanimator) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to specify the UI context. 123 124<!--deprecated_code_no_check--> 125```ts 126import { Animator as animator, SimpleAnimatorOptions } from '@kit.ArkUI'; 127let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).duration(2000); 128animator.create(options);// You are advised to use UIContext.createAnimator(). 129``` 130 131### createAnimator<sup>(deprecated)</sup> 132 133createAnimator(options: AnimatorOptions): AnimatorResult 134 135Creates an animation. 136 137This API is deprecated since API version 9. You are advised to use [createAnimator](js-apis-arkui-UIContext.md#createanimator) in [UIContext](js-apis-arkui-UIContext.md#uicontext) instead. 138 139**System capability**: SystemCapability.ArkUI.ArkUI.Full 140 141**Parameters** 142 143| Name | Type | Mandatory | Description | 144| ------- | ----------------------------------- | ---- | ------- | 145| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 146 147**Return value** 148 149| Type | Description | 150| --------------------------------- | ------------- | 151| [AnimatorResult](#animatorresult) | Animator result.| 152 153**Example** 154 155```ts 156import { Animator as animator } from '@kit.ArkUI'; 157 158let options: AnimatorOptions = { 159 // There is no need to explicitly specify the type AnimatorOptions in the xxx.js file. 160 duration: 1500, 161 easing: "friction", 162 delay: 0, 163 fill: "forwards", 164 direction: "normal", 165 iterations: 3, 166 begin: 200.0, 167 end: 400.0, 168}; 169this.animator = animator.createAnimator(options); 170``` 171 172## AnimatorResult 173 174Defines the animator result. 175 176### reset<sup>9+</sup> 177 178reset(options: AnimatorOptions): void 179 180Updates this animation. 181 182**Atomic service API**: This API can be used in atomic services since API version 11. 183 184**System capability**: SystemCapability.ArkUI.ArkUI.Full 185 186**Parameters** 187 188| Name | Type | Mandatory | Description | 189| ------- | ----------------------------------- | ---- | ------- | 190| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 191 192**Error codes** 193 194For details about the error codes, see [Animator Error Codes](errorcode-animator.md). 195 196| ID | Error Message| 197| --------- | ------- | 198| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 199| 100001 | The specified page is not found or the object property list is not obtained.| 200 201 202**Example** 203 204```ts 205import { Animator as animator, AnimatorOptions, AnimatorResult } from '@kit.ArkUI'; 206import { BusinessError } from '@kit.BasicServicesKit'; 207 208let options: AnimatorOptions = { 209 duration: 1500, 210 easing: "friction", 211 delay: 0, 212 fill: "forwards", 213 direction: "normal", 214 iterations: 3, 215 begin: 200.0, 216 end: 400.0 217}; 218let optionsNew: AnimatorOptions = { 219 duration: 1500, 220 easing: "friction", 221 delay: 0, 222 fill: "forwards", 223 direction: "normal", 224 iterations: 5, 225 begin: 200.0, 226 end: 400.0 227}; 228try { 229 let animatorResult: AnimatorResult | undefined = animator.create(options) 230 animatorResult.reset(optionsNew); 231} catch (error) { 232 let message = (error as BusinessError).message 233 let code = (error as BusinessError).code 234 console.error(`Animator reset failed, error code: ${code}, message: ${message}.`); 235} 236``` 237 238### reset<sup>18+</sup> 239 240reset(options: AnimatorOptions \| [SimpleAnimatorOptions](#simpleanimatoroptions18)): void 241 242Updates this animation. Compared with [reset](#reset9), this API accepts parameters of the [SimpleAnimatorOptions](#simpleanimatoroptions18) type. 243 244**Atomic service API**: This API can be used in atomic services since API version 18. 245 246**System capability**: SystemCapability.ArkUI.ArkUI.Full 247 248**Parameters** 249 250| Name | Type | Mandatory | Description | 251| ------- | ----------------------------------- | ---- | ------- | 252| options | [AnimatorOptions](#animatoroptions) \| [SimpleAnimatorOptions](#simpleanimatoroptions18) | Yes | Animator options.| 253 254**Error codes** 255 256For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Animator Error Codes](errorcode-animator.md). 257 258| ID | Error Message| 259| --------- | ------- | 260| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 261| 100001 | The specified page is not found or the object property list is not obtained.| 262 263**Example** 264 265<!--deprecated_code_no_check--> 266```ts 267import { Animator as animator, AnimatorResult, AnimatorOptions, SimpleAnimatorOptions } from '@kit.ArkUI'; 268let options: AnimatorOptions = { 269 duration: 1500, 270 easing: "ease", 271 delay: 0, 272 fill: "forwards", 273 direction: "normal", 274 iterations: 1, 275 begin: 100, 276 end: 200 277}; 278let optionsNew: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200) 279 .duration(2000) 280 .iterations(3) 281 .delay(1000) 282let animatorResult:AnimatorResult = animator.create(options); 283animatorResult.reset(optionsNew); 284``` 285 286### play 287 288play(): void 289 290Plays this animation. The animation retains the previous playback state. For example, if the animation is set to **reverse** and paused, it will remain in **reverse** when resumed. 291 292**Atomic service API**: This API can be used in atomic services since API version 11. 293 294**System capability**: SystemCapability.ArkUI.ArkUI.Full 295 296**Example** 297 298```ts 299animator.play(); 300``` 301 302### finish 303 304finish(): void 305 306Ends this animation. 307 308**Atomic service API**: This API can be used in atomic services since API version 11. 309 310**System capability**: SystemCapability.ArkUI.ArkUI.Full 311 312**Example** 313 314```ts 315animator.finish(); 316``` 317 318### pause 319 320pause(): void 321 322Pauses this animation. 323 324**Atomic service API**: This API can be used in atomic services since API version 11. 325 326**System capability**: SystemCapability.ArkUI.ArkUI.Full 327 328**Example** 329 330```ts 331animator.pause(); 332``` 333 334### cancel 335 336cancel(): void 337 338Cancels this animation. 339 340**Atomic service API**: This API can be used in atomic services since API version 11. 341 342**System capability**: SystemCapability.ArkUI.ArkUI.Full 343 344**Example** 345 346```ts 347animator.cancel(); 348``` 349 350### reverse 351 352reverse(): void 353 354Plays this animation in reverse order. This API does not take effect when the interpolating spring curve is used. 355 356**Atomic service API**: This API can be used in atomic services since API version 11. 357 358**System capability**: SystemCapability.ArkUI.ArkUI.Full 359 360**Example** 361 362```ts 363animator.reverse(); 364``` 365 366### onFrame<sup>12+</sup> 367 368onFrame: (progress: number) => void 369 370Called when a frame is received. 371 372**Atomic service API**: This API can be used in atomic services since API version 12. 373 374**System capability**: SystemCapability.ArkUI.ArkUI.Full 375 376**Parameters** 377 378| Name | Type | Mandatory | Description | 379| -------- | ------ | ---- | -------- | 380| progress | number | Yes | Current value of the animation.<br>Value range: [begin, end] defined in [AnimatorOptions](#animatoroptions)<br>Default value range: [0, 1]| 381 382**Example** 383 384<!--deprecated_code_no_check--> 385```ts 386import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 387 388let animatorResult: AnimatorResult | undefined = animator.create(options) 389animatorResult.onFrame = (value: number) => { 390 console.info("onFrame callback") 391} 392``` 393 394### onFinish<sup>12+</sup> 395 396onFinish: () => void 397 398Called when this animation is finished. 399 400**Atomic service API**: This API can be used in atomic services since API version 12. 401 402**System capability**: SystemCapability.ArkUI.ArkUI.Full 403 404**Example** 405 406<!--deprecated_code_no_check--> 407```ts 408import {Animator as animator, AnimatorResult } from '@kit.ArkUI'; 409let animatorResult:AnimatorResult|undefined = animator.create(options) 410animatorResult.onFinish = ()=> { 411 console.info("onFinish callback") 412} 413``` 414 415### onCancel<sup>12+</sup> 416 417onCancel: () => void 418 419Called when this animation is canceled. 420 421**Atomic service API**: This API can be used in atomic services since API version 12. 422 423**System capability**: SystemCapability.ArkUI.ArkUI.Full 424 425**Example** 426 427<!--deprecated_code_no_check--> 428```ts 429import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 430 431let animatorResult: AnimatorResult | undefined = animator.create(options) 432animatorResult.onCancel = () => { 433 console.info("onCancel callback") 434} 435``` 436 437### onRepeat<sup>12+</sup> 438 439onRepeat: () => void 440 441Called when this animation repeats. 442 443**Atomic service API**: This API can be used in atomic services since API version 12. 444 445**System capability**: SystemCapability.ArkUI.ArkUI.Full 446 447**Example** 448 449<!--deprecated_code_no_check--> 450```ts 451import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 452 453let animatorResult: AnimatorResult | undefined = animator.create(options) 454animatorResult.onRepeat = () => { 455 console.info("onRepeat callback") 456} 457``` 458 459### onframe<sup>(deprecated)</sup> 460 461> **NOTE** 462> 463> This API is deprecated since API version 12. You are advised to use [onFrame](#onframe12) instead. 464 465onframe: (progress: number) => void 466 467Called when a frame is received. 468 469**Atomic service API**: This API can be used in atomic services since API version 11. 470 471**System capability**: SystemCapability.ArkUI.ArkUI.Full 472 473**Parameters** 474 475| Name | Type | Mandatory | Description | 476| -------- | ------ | ---- | -------- | 477| progress | number | Yes | Current progress of the animation.| 478 479**Example** 480 481```ts 482import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 483 484let animatorResult: AnimatorResult | undefined = animator.create(options) 485animatorResult.onframe = (value) => { 486 console.info("onframe callback") 487} 488``` 489 490### onfinish<sup>(deprecated)</sup> 491 492> **NOTE** 493> 494> This API is deprecated since API version 12. You are advised to use [onFinish](#onfinish12) instead. 495 496onfinish: () => void 497 498Called when this animation is finished. 499 500**Atomic service API**: This API can be used in atomic services since API version 11. 501 502**System capability**: SystemCapability.ArkUI.ArkUI.Full 503 504**Example** 505 506```ts 507import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 508 509let animatorResult: AnimatorResult | undefined = animator.create(options) 510animatorResult.onfinish = () => { 511 console.info("onfinish callback") 512} 513``` 514 515### oncancel<sup>(deprecated)</sup> 516 517> **NOTE** 518> 519> This API is deprecated since API version 12. You are advised to use [onCancel](#oncancel12) instead. 520 521 522oncancel: () => void 523 524Called when this animation is canceled. 525 526**Atomic service API**: This API can be used in atomic services since API version 11. 527 528**System capability**: SystemCapability.ArkUI.ArkUI.Full 529 530**Example** 531 532<!--deprecated_code_no_check--> 533```ts 534import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 535 536let animatorResult: AnimatorResult | undefined = animator.create(options) 537animatorResult.oncancel = () => { 538 console.info("oncancel callback") 539} 540``` 541 542### onrepeat<sup>(deprecated)</sup> 543 544> **NOTE** 545> 546> This API is deprecated since API version 12. You are advised to use [onRepeat](#onrepeat12) instead. 547 548onrepeat: () => void 549 550Called when this animation repeats. 551 552**Atomic service API**: This API can be used in atomic services since API version 11. 553 554**System capability**: SystemCapability.ArkUI.ArkUI.Full 555 556**Example** 557 558```ts 559import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 560 561let animatorResult: AnimatorResult | undefined = animator.create(options) 562animatorResult.onrepeat = () => { 563 console.info("onrepeat callback") 564} 565``` 566 567### setExpectedFrameRateRange<sup>12+</sup> 568 569setExpectedFrameRateRange(rateRange: ExpectedFrameRateRange): void 570 571Sets the expected frame rate range. 572 573**Atomic service API**: This API can be used in atomic services since API version 12. 574 575**System capability**: SystemCapability.ArkUI.ArkUI.Full 576 577**Parameters** 578 579| Name | Type | Mandatory| Description | 580| --------------- | ------------------------------------------ | ---- | -----------------------------| 581| rateRange | [ExpectedFrameRateRange](../apis-arkui/arkui-ts/ts-explicit-animation.md#expectedframeraterange11)| Yes | Expected frame rate range.| 582 583**Example** 584 585<!--deprecated_code_no_check--> 586```ts 587import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 588 589let animatorResult: AnimatorResult | undefined = animator.create({ 590 duration: 2000, 591 easing: "ease", 592 delay: 0, 593 fill: "forwards", 594 direction: "normal", 595 iterations: 1, 596 begin: 100, 597 end: 200 598}) 599let expectedFrameRate: ExpectedFrameRateRange = { 600 min: 0, 601 max: 120, 602 expected: 30 603} 604animatorResult.setExpectedFrameRateRange(expectedFrameRate); 605``` 606 607### update<sup>(deprecated)</sup> 608 609update(options: AnimatorOptions): void 610 611Updates this animator. 612 613This API is deprecated since API version 9. You are advised to use [reset<sup>9+</sup>](#reset9) instead. 614 615**System capability**: SystemCapability.ArkUI.ArkUI.Full 616 617**Parameters** 618 619| Name | Type | Mandatory | Description | 620| ------- | ----------------------------------- | ---- | ------- | 621| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 622 623**Example** 624 625```ts 626animator.update(options); 627``` 628 629## AnimatorOptions 630 631Animator options. 632 633**Atomic service API**: This API can be used in atomic services since API version 11. 634 635**System capability**: SystemCapability.ArkUI.ArkUI.Full 636 637| Name | Type | Mandatory| Description | 638| ---------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 639| duration | number | Yes | Duration for playing the animation, in milliseconds.<br> Value range: [0, +∞). | 640| easing | string | Yes | Animation interpolation curve. Only the following values are supported:<br>**"linear"**: The animation speed keeps unchanged.<br>**"ease"**: The animation starts slowly, accelerates, and then slows down towards the end. The cubic-bezier curve (0.25, 0.1, 0.25, 1.0) is used.<br>**"ease-in"**: The animation starts at a low speed and then picks up speed until the end. The cubic-bezier curve (0.42, 0.0, 1.0, 1.0) is used.<br>**"ease-out"**: The animation ends at a low speed. The cubic-bezier curve (0.0, 0.0, 0.58, 1.0) is used.<br>**"ease-in-out"**: The animation starts and ends at a low speed. The cubic-bezier curve (0.42, 0.0, 0.58, 1.0) is used.<br>**"fast-out-slow-in"**: The animation uses the standard cubic-bezier curve (0.4, 0.0, 0.2, 1.0).<br>**"linear-out-slow-in"**: The animation uses the deceleration cubic-bezier curve (0.0, 0.0, 0.2, 1.0).<br>**"fast-out-linear-in"**: The animation uses acceleration cubic-bezier curve (0.4, 0.0, 1.0, 1.0)<br>**"friction"**: The animation uses the damping cubic-bezier curve (0.2, 0.0, 0.2, 1.0).<br>**"extreme-deceleration"**: The animation uses the extreme deceleration cubic-bezier curve (0.0, 0.0, 0.0, 1.0).<br>**"rhythm"**: The animation uses the rhythm cubic-bezier curve (0.7, 0.0, 0.2, 1.0).<br>**"sharp"**: The animation uses the sharp cubic-bezier curve (0.33, 0.0, 0.67, 1.0).<br>**"smooth"**: The animation uses the smooth cubic-bezier curve (0.4, 0.0, 0.4, 1.0).<br>**"cubic-bezier(x1,y1,x2,y2)"**: The animation uses the defined cubic bezier curve, where the value of **x1** and **x2** must range from 0 to 1. For example, **"cubic-bezier(0.42,0.0,0.58,1.0)"**.<br>**"steps(number,step-position)"**: The animation uses a step curve. The **number** parameter is mandatory and must be set to a positive integer. The **step-position** parameter is optional and can be set to **start** or **end** (default value). For example, **"steps(3,start)"**.<br>**"interpolating-spring(velocity,mass,stiffness,damping)"**: The animation uses an interpolating spring curve. This API is supported since API version 11 and can be used only in ArkTS. The **velocity**, **mass**, **stiffness**, and **damping** parameters are of the numeric type, and the values of **mass**, **stiffness**, and **damping** must be greater than 0. For details about the parameters, see [Curves.interpolatingSpring](./js-apis-curve.md#curvesinterpolatingspring10). When an interpolating spring curve is used, settings for the **duration**, **fill**, **direction**, and **iterations** do not take effect. Rather, the value of **duration** is subject to the spring settings, **fill** is fixed at **forwards**, **direction** at **normal**, and **iterations** at **1**. In addition, invoking [reverse](#reverse) of **animator** is not effective. In other words, when using an interpolating spring curve, the animation can play only once in forward mode.<br>If the provided string is invalid, **"ease"** is used.| 641| delay | number | Yes | Animation delay duration, in milliseconds. Value **0** means that there is no delay. If the value specified is a negative number, the animation starts playing ahead of its scheduled time. If the amount of time by which the playback is advanced is greater than the total duration of the animation, the animation skips to the end state immediately. | 642| fill | 'none' \| 'forwards' \| 'backwards' \| 'both' | Yes | State of the animated target after the animation is executed.<br>**'none'**: No style is applied to the target before or after the animation is executed.<br>**'forwards'**: The target keeps the state at the end of the animation (defined in the last key frame) after the animation is executed.<br>**'backwards'**: The animation uses the value defined in the first key frame during the **animation-delay**. When **animation-direction** is set to **'normal'** or **'alternate'**, the value in the **from** key frame is used. When **animation-direction** is set to **'reverse'** or **'alternate-reverse'**, the value in the **to** key frame is used.<br>**'both'**: The animation follows the **'forwards'** and **'backwards'** rules.| 643| direction | 'normal' \| 'reverse' \| 'alternate' \| 'alternate-reverse' | Yes | Animation playback mode.<br>**'normal'**: plays the animation in forward loop mode.<br>**'reverse'**: plays the animation in reverse loop mode.<br>**'alternate'**: plays the animation in alternating loop mode. When the animation is played for an odd number of times, the playback is in forward direction. When the animation is played for an even number of times, the playback is in reverse direction.<br>**'alternate-reverse'**: plays the animation in reverse alternating loop mode. When the animation is played for an odd number of times, the playback is in reverse direction. When the animation is played for an even number of times, the playback is in forward direction.| 644| iterations | number | Yes | Number of times that the animation is played. The value **0** means the animation is not played, **-1** means the animation is played for an unlimited number of times, and a positive integer means the animation is played that specific number of times.<br>**NOTE**<br>Any negative value other than **-1** is considered invalid. For invalid values, the animation is played once.| 645| begin | number | Yes | Start point of the animation interpolation.<br>**NOTE**<br>This setting affects the input parameter value of the [onFrame](#onframe12) callback. | 646| end | number | Yes | End point of animation interpolation.<br>**NOTE**<br>This setting affects the input parameter value of the [onFrame](#onframe12) callback. | 647 648## SimpleAnimatorOptions<sup>18+</sup> 649 650Defines a simple animation parameter object. Unlike **AnimatorOptions**, this object comes with some default values for certain animation parameters, so you don't have to set them manually. 651 652### constructor<sup>18+</sup> 653 654constructor(begin: number, end: number) 655 656A constructor used to create a **SimpleAnimatorOptions** instance. 657 658**Atomic service API**: This API can be used in atomic services since API version 18. 659 660**System capability**: SystemCapability.ArkUI.ArkUI.Full 661 662**Parameters** 663 664| Name | Type | Mandatory| Description | 665| ---------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 666| begin | number | Yes | Start point of the animation interpolation. | 667| end | number | Yes | End point of animation interpolation. 668 669**Example** 670 671```ts 672import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 673 674let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200); // Animation interpolation from 100 to 200, with other animation parameters set to default values. 675let animatorResult:AnimatorResult = animator.create(options); 676``` 677 678### duration<sup>18+</sup> 679 680duration(duration: number): SimpleAnimatorOptions 681 682Sets the animation duration. 683 684**Atomic service API**: This API can be used in atomic services since API version 18. 685 686**System capability**: SystemCapability.ArkUI.ArkUI.Full 687 688**Parameters** 689 690| Name | Type | Mandatory | Description | 691| ------- | ----------------------------------- | ---- | ------- | 692| duration | number | Yes | Animation duration, in milliseconds.<br>Default value: **1000**| 693 694**Return value** 695 696| Type | Description | 697| --------------------------------- | ------------- | 698| [SimpleAnimatorOptions](#simpleanimatoroptions18) | **SimpleAnimatorOptions** object for animation parameters.| 699 700**Example** 701 702```ts 703import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 704 705let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).duration(500); 706let animatorResult:AnimatorResult = animator.create(options); 707``` 708 709### easing<sup>18+</sup> 710 711easing(curve: string): SimpleAnimatorOptions 712 713Sets the interpolation curve for this animation. 714 715**Atomic service API**: This API can be used in atomic services since API version 18. 716 717**System capability**: SystemCapability.ArkUI.ArkUI.Full 718 719**Parameters** 720 721| Name | Type | Mandatory | Description | 722| ------- | ----------------------------------- | ---- | ------- | 723| curve | string | Yes | Interpolation curve. For details, see [AnimatorOptions](#animatoroptions).<br>Default value: **"ease"**| 724 725**Return value** 726 727| Type | Description | 728| --------------------------------- | ------------- | 729| [SimpleAnimatorOptions](#simpleanimatoroptions18) | **SimpleAnimatorOptions** object for animation parameters.| 730 731**Example** 732 733```ts 734import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 735 736let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).easing("ease-in"); 737let animatorResult:AnimatorResult = animator.create(options); 738``` 739 740### delay<sup>18+</sup> 741 742delay(delay: number): SimpleAnimatorOptions 743 744Sets the playback delay for this animation. 745 746**Atomic service API**: This API can be used in atomic services since API version 18. 747 748**System capability**: SystemCapability.ArkUI.ArkUI.Full 749 750**Parameters** 751 752| Name | Type | Mandatory | Description | 753| ------- | ----------------------------------- | ---- | ------- | 754| delay | number | Yes | Playback delay, in milliseconds. The value **0** indicates no delay. If the value specified is a negative number, the animation starts playing ahead of its scheduled time. If the amount of time by which the playback is advanced is greater than the total duration of the animation, the animation skips to the end state immediately.<br>Default value: **0**| 755 756**Return value** 757 758| Type | Description | 759| --------------------------------- | ------------- | 760| [SimpleAnimatorOptions](#simpleanimatoroptions18) | **SimpleAnimatorOptions** object for animation parameters.| 761 762**Example** 763 764```ts 765import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 766 767let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).delay(500); 768let animatorResult:AnimatorResult = animator.create(options); 769``` 770 771### fill<sup>18+</sup> 772 773fill(fillMode: [FillMode](./arkui-ts/ts-appendix-enums.md#fillmode)): SimpleAnimatorOptions 774 775Sets the fill mode for this animation. 776 777**Atomic service API**: This API can be used in atomic services since API version 18. 778 779**System capability**: SystemCapability.ArkUI.ArkUI.Full 780 781**Parameters** 782 783| Name | Type | Mandatory | Description | 784| ------- | ----------------------------------- | ---- | ------- | 785| fillMode | [FillMode](./arkui-ts/ts-appendix-enums.md#fillmode) | Yes | Fill mode, which affects how the animation behaves during the delay period and after it ends.<br>Default value: **FillMode.Forwards**| 786 787**Return value** 788 789| Type | Description | 790| --------------------------------- | ------------- | 791| [SimpleAnimatorOptions](#simpleanimatoroptions18) | **SimpleAnimatorOptions** object for animation parameters.| 792 793**Example** 794 795```ts 796import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 797 798let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).fill(FillMode.Forwards); 799let animatorResult:AnimatorResult = animator.create(options); 800``` 801 802### direction<sup>18+</sup> 803 804direction(direction: [PlayMode](./arkui-ts/ts-appendix-enums.md#playmode)): SimpleAnimatorOptions 805 806Sets the playback direction for this animation. 807 808**Atomic service API**: This API can be used in atomic services since API version 18. 809 810**System capability**: SystemCapability.ArkUI.ArkUI.Full 811 812**Parameters** 813 814| Name | Type | Mandatory | Description | 815| ------- | ----------------------------------- | ---- | ------- | 816| direction | [PlayMode](./arkui-ts/ts-appendix-enums.md#playmode) | Yes | Playback direction.<br>Default value: **PlayMode.Normal**| 817 818**Return value** 819 820| Type | Description | 821| --------------------------------- | ------------- | 822| [SimpleAnimatorOptions](#simpleanimatoroptions18) | **SimpleAnimatorOptions** object for animation parameters.| 823 824**Example** 825 826```ts 827import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 828 829let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).direction(PlayMode.Alternate); 830let animatorResult:AnimatorResult = animator.create(options); 831``` 832 833### iterations<sup>18+</sup> 834 835iterations(iterations: number): SimpleAnimatorOptions 836 837Sets the number of times that this animation is played. 838 839**Atomic service API**: This API can be used in atomic services since API version 18. 840 841**System capability**: SystemCapability.ArkUI.ArkUI.Full 842 843**Parameters** 844 845| Name | Type | Mandatory | Description | 846| ------- | ----------------------------------- | ---- | ------- | 847| iterations | number | Yes | Number of times that the animation is played. The value **0** means the animation is not played, and **-1** means the animation is played for an unlimited number of times.<br>Default value: **1**| 848 849**Return value** 850 851| Type | Description | 852| --------------------------------- | ------------- | 853| [SimpleAnimatorOptions](#simpleanimatoroptions18) | **SimpleAnimatorOptions** object for animation parameters.| 854 855**Example** 856 857```ts 858import { Animator as animator, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 859 860let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).iterations(3); 861let animatorResult:AnimatorResult = animator.create(options); 862``` 863 864## Example 865### JavaScript-compatible Web-like Development Paradigm 866 867```html 868<!-- hml --> 869<div class="container"> 870 <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show"> 871 </div> 872</div> 873``` 874 875<!--deprecated_code_no_check--> 876```ts 877import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 878import { BusinessError } from '@kit.BasicServicesKit'; 879 880let DataTmp: Record<string, Animator> = { 881 'divWidth': 200, 882 'divHeight': 200, 883 'animator': animator 884} 885 886class Tmp { 887 data: animator = DataTmp 888 onInit: Function = () => { 889 } 890 Show: Function = () => { 891 } 892} 893 894class DateT { 895 divWidth: number = 0 896 divHeight: number = 0 897 animator: AnimatorResult | null = null 898} 899 900(Fn: (v: Tmp) => void) => { 901 Fn({ 902 data: DataTmp, 903 onInit() { 904 let options: AnimatorOptions = { 905 duration: 1500, 906 easing: "friction", 907 delay: 0, 908 fill: "forwards", 909 direction: "normal", 910 iterations: 2, 911 begin: 200.0, 912 end: 400.0 913 }; 914 let DataTmp: DateT = { 915 divWidth: 200, 916 divHeight: 200, 917 animator: null 918 } 919 DataTmp.animator = animator.create(options); 920 }, 921 Show() { 922 let options1: AnimatorOptions = { 923 duration: 1500, 924 easing: "friction", 925 delay: 0, 926 fill: "forwards", 927 direction: "normal", 928 iterations: 2, 929 begin: 0, 930 end: 400.0, 931 }; 932 let DataTmp: DateT = { 933 divWidth: 200, 934 divHeight: 200, 935 animator: null 936 } 937 try { 938 DataTmp.animator = animator.create(options1); 939 DataTmp.animator.reset(options1); 940 } catch (error) { 941 let message = (error as BusinessError).message 942 let code = (error as BusinessError).code 943 console.error(`Animator reset failed, error code: ${code}, message: ${message}.`); 944 } 945 let _this = DataTmp; 946 if (DataTmp.animator) { 947 DataTmp.animator.onFrame = (value: number) => { 948 _this.divWidth = value; 949 _this.divHeight = value; 950 }; 951 DataTmp.animator.play(); 952 } 953 } 954 }) 955} 956``` 957 958  959 960### ArkTS-based Declarative Development Paradigm 961 962> **NOTE** 963> 964> For precise animation control, use the [createAnimator](js-apis-arkui-UIContext.md#createanimator) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to specify the UI context. 965 966<!--deprecated_code_no_check--> 967```ts 968import { Animator as animator, AnimatorResult } from '@kit.ArkUI'; 969 970@Entry 971@Component 972struct AnimatorTest { 973 private TAG: string = '[AnimatorTest]' 974 private backAnimator: AnimatorResult | undefined = undefined 975 private flag: boolean = false 976 @State wid: number = 100 977 @State hei: number = 100 978 979 create() { 980 this.backAnimator = animator.create({ 981 // You are advised to use this.getUIContext.createAnimator(). 982 duration: 2000, 983 easing: "ease", 984 delay: 0, 985 fill: "forwards", 986 direction: "normal", 987 iterations: 1, 988 begin: 100, // Start point of the animation interpolation. 989 end: 200 // End point of the animation interpolation. 990 }) 991 this.backAnimator.onFinish = () => { 992 this.flag = true 993 console.info(this.TAG, 'backAnimator onFinish') 994 } 995 this.backAnimator.onRepeat = () => { 996 console.info(this.TAG, 'backAnimator repeat') 997 } 998 this.backAnimator.onCancel = () => { 999 console.info(this.TAG, 'backAnimator cancel') 1000 } 1001 this.backAnimator.onFrame = (value: number) => { 1002 this.wid = value 1003 this.hei = value 1004 } 1005 } 1006 1007 aboutToDisappear() { 1008 // When the custom component disappears, call finish to end incomplete animations and prevent them from continuing. 1009 // Since backAnimator references this in onFrame, and this holds backAnimator, 1010 // set backAnimator stored in the custom component to undefined when the component disappears to avoid memory leak. 1011 this.backAnimator?.finish(); 1012 this.backAnimator = undefined; 1013 } 1014 1015 build() { 1016 Column() { 1017 Column() { 1018 Column() 1019 .width(this.wid) 1020 .height(this.hei) 1021 .backgroundColor(Color.Red) 1022 } 1023 .width('100%') 1024 .height(300) 1025 1026 Column() { 1027 Row() { 1028 Button('create') 1029 .fontSize(30) 1030 .fontColor(Color.Black) 1031 .onClick(() => { 1032 this.create() 1033 }) 1034 } 1035 .padding(10) 1036 1037 Row() { 1038 Button('play') 1039 .fontSize(30) 1040 .fontColor(Color.Black) 1041 .onClick(() => { 1042 this.flag = false 1043 if (this.backAnimator) { 1044 this.backAnimator.play() 1045 } 1046 }) 1047 } 1048 .padding(10) 1049 1050 Row() { 1051 Button('pause') 1052 .fontSize(30) 1053 .fontColor(Color.Black) 1054 .onClick(() => { 1055 if (this.backAnimator) { 1056 this.backAnimator.pause() 1057 } 1058 }) 1059 } 1060 .padding(10) 1061 1062 Row() { 1063 Button('finish') 1064 .fontSize(30) 1065 .fontColor(Color.Black) 1066 .onClick(() => { 1067 this.flag = true 1068 if (this.backAnimator) { 1069 this.backAnimator.finish() 1070 } 1071 }) 1072 } 1073 .padding(10) 1074 1075 Row() { 1076 Button('reverse') 1077 .fontSize(30) 1078 .fontColor(Color.Black) 1079 .onClick(() => { 1080 this.flag = false 1081 if (this.backAnimator) { 1082 this.backAnimator.reverse() 1083 } 1084 }) 1085 } 1086 .padding(10) 1087 1088 Row() { 1089 Button('cancel') 1090 .fontSize(30) 1091 .fontColor(Color.Black) 1092 .onClick(() => { 1093 if (this.backAnimator) { 1094 this.backAnimator.cancel() 1095 } 1096 }) 1097 } 1098 .padding(10) 1099 1100 Row() { 1101 Button('reset') 1102 .fontSize(30) 1103 .fontColor(Color.Black) 1104 .onClick(() => { 1105 if (this.flag) { 1106 this.flag = false 1107 if (this.backAnimator) { 1108 this.backAnimator.reset({ 1109 duration: 3000, 1110 easing: "ease-in", 1111 delay: 0, 1112 fill: "forwards", 1113 direction: "alternate", 1114 iterations: 3, 1115 begin: 100, 1116 end: 300 1117 }) 1118 } 1119 } else { 1120 console.info(this.TAG, 'Animation not ended') 1121 } 1122 }) 1123 } 1124 .padding(10) 1125 } 1126 } 1127 } 1128} 1129``` 1130 1131### Example: Implementing a Translation Animation with Simple Parameters 1132 1133```ts 1134import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI'; 1135 1136@Entry 1137@Component 1138struct AnimatorTest { 1139 private TAG: string = '[AnimatorTest]' 1140 private backAnimator: AnimatorResult | undefined = undefined 1141 private flag: boolean = false 1142 @State translate_: number = 0 1143 1144 create() { 1145 this.backAnimator = this.getUIContext()?.createAnimator( 1146 new SimpleAnimatorOptions(0, 100) 1147 ) 1148 this.backAnimator.onFinish = ()=> { 1149 this.flag = true 1150 console.info(this.TAG, 'backAnimator onFinish') 1151 } 1152 this.backAnimator.onFrame = (value:number)=> { 1153 this.translate_ = value 1154 } 1155 } 1156 1157 aboutToDisappear() { 1158 // When the custom component disappears, call finish to end incomplete animations and prevent them from continuing. 1159 // Since backAnimator references this in onFrame, and this holds backAnimator, 1160 // set backAnimator stored in the custom component to undefined when the component disappears to avoid memory leak. 1161 this.backAnimator?.finish(); 1162 this.backAnimator = undefined; 1163 } 1164 1165 build() { 1166 Column() { 1167 Column() { 1168 Column() 1169 .width(100) 1170 .height(100) 1171 .translate({x: this.translate_}) 1172 .backgroundColor(Color.Green) 1173 } 1174 .width('100%') 1175 .height(300) 1176 1177 Column() { 1178 Column() { 1179 Button('create') 1180 .fontSize(30) 1181 .fontColor(Color.Black) 1182 .onClick(() => { 1183 this.create() 1184 }) 1185 } 1186 .padding(10) 1187 1188 Column() { 1189 Button('play') 1190 .fontSize(30) 1191 .fontColor(Color.Black) 1192 .onClick(() => { 1193 this.flag = false 1194 if(this.backAnimator){ 1195 this.backAnimator.play() 1196 } 1197 }) 1198 } 1199 .padding(10) 1200 1201 Column() { 1202 Button('reset') 1203 .fontSize(30) 1204 .fontColor(Color.Black) 1205 .onClick(() => { 1206 if (this.flag) { 1207 this.flag = false 1208 if(this.backAnimator){ 1209 this.backAnimator.reset( 1210 new SimpleAnimatorOptions(0, -100) 1211 .duration(2000) 1212 .easing("ease-in") 1213 .fill(FillMode.Forwards) 1214 .direction(PlayMode.Alternate) 1215 .iterations(2) 1216 ) 1217 } 1218 } else { 1219 console.info(this.TAG, 'Animation not ended') 1220 } 1221 }) 1222 } 1223 .padding(10) 1224 } 1225 } 1226 } 1227} 1228``` 1229 1230 1231