js-apis-animator.md 12.0 KB
Newer Older
E
esterzhou 已提交
1 2 3
# @ohos.animator

The **animator** module provides APIs for applying animation effects, including defining animations, starting animations, and playing animations in reverse order.
E
ester.zhou 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
E
ester.zhou 已提交
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.
Z
zengyawen 已提交
8 9


10
## Modules to Import
Z
zengyawen 已提交
11

E
esterzhou 已提交
12
```js
E
ester.zhou 已提交
13 14
import animator from '@ohos.animator';
```
E
esterzhou 已提交
15
## create<sup>9+</sup>
E
ester.zhou 已提交
16

E
esterzhou 已提交
17
create(options: AnimatorOptions): AnimatorResult
E
ester.zhou 已提交
18 19 20 21 22 23

Creates an **Animator** object.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
E
esterzhou 已提交
24 25 26 27

| Name    | Type                                 | Mandatory  | Description     |
| ------- | ----------------------------------- | ---- | ------- |
| options | [AnimatorOptions](#animatoroptions) | Yes   | Animator options.|
E
ester.zhou 已提交
28 29

**Return value**
E
esterzhou 已提交
30 31 32

| Type                               | Description           |
| --------------------------------- | ------------- |
E
ester.zhou 已提交
33 34 35 36
| [AnimatorResult](#animatorresult) | Animator result.|

**Example**

E
esterzhou 已提交
37 38 39 40 41 42 43 44 45 46 47 48
  ```js
  let options = {
    duration: 1500,
    easing: 'friction',
    delay: 0,
    fill: 'forwards',
    direction: "normal",
    iterations: 3,
    begin: 200.0,
    end: 400.0,
  };
  animator.create(options);
E
ester.zhou 已提交
49 50 51 52 53 54
  ```

## AnimatorResult

Defines the animator result.

E
esterzhou 已提交
55
### reset<sup>9+</sup>
E
ester.zhou 已提交
56

E
esterzhou 已提交
57
reset(options: AnimatorOptions): void
E
ester.zhou 已提交
58 59

Updates this animator.
Z
zengyawen 已提交
60

E
ester.zhou 已提交
61
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
62

E
ester.zhou 已提交
63
**Parameters**
E
esterzhou 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76

| Name    | Type                                 | Mandatory  | Description     |
| ------- | ----------------------------------- | ---- | ------- |
| options | [AnimatorOptions](#animatoroptions) | Yes   | Animator options.|

**Error codes**

For details about the error codes, see [Animator Error Codes](../errorcodes/errorcode-animator.md).

| ID  | Error Message|
| --------- | ------- |
| 100001    | If no page is found for pageId or fail to get object property list. |

Z
zengyawen 已提交
77

E
ester.zhou 已提交
78
**Example**
E
esterzhou 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

```js
let options = {
  duration: 1500,
  easing: 'friction',
  delay: 0,
  fill: 'forwards',
  direction: "normal",
  iterations: 3,
  begin: 200.0,
  end: 400.0,
};
try {
  animator.reset(options);
} catch(error) {
  console.error(`Animator reset failed, error code: ${error.code}, message: ${error.message}.`);
}
Z
zengyawen 已提交
96 97
```

E
ester.zhou 已提交
98
### play
Z
zengyawen 已提交
99

E
ester.zhou 已提交
100
play(): void
Z
zengyawen 已提交
101

E
ester.zhou 已提交
102
Plays this animation.
Z
zengyawen 已提交
103

E
ester.zhou 已提交
104
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
105

E
ester.zhou 已提交
106
**Example**
E
esterzhou 已提交
107 108

```js
E
ester.zhou 已提交
109 110
animator.play();
```
Z
zengyawen 已提交
111

E
ester.zhou 已提交
112
### finish
Z
zengyawen 已提交
113

E
ester.zhou 已提交
114
finish(): void
Z
zengyawen 已提交
115

E
ester.zhou 已提交
116
Ends this animation.
Z
zengyawen 已提交
117

E
ester.zhou 已提交
118
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
119

E
ester.zhou 已提交
120
**Example**
E
esterzhou 已提交
121 122

```js
E
ester.zhou 已提交
123 124
animator.finish();
```
Z
zengyawen 已提交
125

E
ester.zhou 已提交
126
### pause
Z
zengyawen 已提交
127

E
ester.zhou 已提交
128
pause(): void
Z
zengyawen 已提交
129

E
ester.zhou 已提交
130
Pauses this animation.
Z
zengyawen 已提交
131

E
ester.zhou 已提交
132
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
133

E
ester.zhou 已提交
134
**Example**
E
esterzhou 已提交
135 136

```js
E
ester.zhou 已提交
137 138
animator.pause();
```
Z
zengyawen 已提交
139

E
ester.zhou 已提交
140
### cancel
Z
zengyawen 已提交
141

E
ester.zhou 已提交
142
cancel(): void
Z
zengyawen 已提交
143

E
ester.zhou 已提交
144
Cancels this animation.
Z
zengyawen 已提交
145

E
ester.zhou 已提交
146
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
147

E
ester.zhou 已提交
148
**Example**
E
esterzhou 已提交
149 150

```js
E
ester.zhou 已提交
151 152 153 154 155 156 157 158 159 160 161 162
animator.cancel();
```

### reverse

reverse(): void

Plays this animation in reverse order.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
E
esterzhou 已提交
163 164

```js
E
ester.zhou 已提交
165 166 167 168 169 170 171 172 173 174 175 176
animator.reverse();
```

### onframe

onframe: (progress: number) => void

Called when a frame is received.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
E
esterzhou 已提交
177 178 179 180

| Name     | Type    | Mandatory  | Description      |
| -------- | ------ | ---- | -------- |
| progress | number | Yes   | Current progress of the animation.|
E
ester.zhou 已提交
181 182

**Example**
E
esterzhou 已提交
183 184 185 186 187 188

```js
let animatorResult = animator.create(options)
animatorResult.onframe = function(value) {
  console.info("onframe callback")
}
E
ester.zhou 已提交
189 190 191 192 193 194 195 196 197 198 199
```

### onfinish

onfinish: () => void

Called when this animation is finished.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
E
esterzhou 已提交
200 201 202 203 204 205

```js
let animatorResult = animator.create(options)
animatorResult.onfinish = function() {
  console.info("onfinish callback")
}
E
ester.zhou 已提交
206 207 208 209 210 211 212 213 214 215 216
```

### oncancel

oncancel: () => void

Called when this animation is canceled.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
E
esterzhou 已提交
217 218 219 220 221 222

```js
let animatorResult = animator.create(options)
animatorResult.oncancel = function() {
  console.info("oncancel callback")
}
E
ester.zhou 已提交
223 224 225 226 227 228
```

### onrepeat

onrepeat: () => void

E
ester.zhou 已提交
229 230
Called when this animation repeats.

E
ester.zhou 已提交
231 232 233
**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
E
esterzhou 已提交
234 235 236 237 238 239

```js
let animatorResult = animator.create(options)
animatorResult.onrepeat = function() {
  console.info("onrepeat callback")
}
E
ester.zhou 已提交
240
```
E
esterzhou 已提交
241 242


E
ester.zhou 已提交
243 244 245 246 247 248 249

## AnimatorOptions

Defines animator options.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
esterzhou 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
| Name        | Type                                    | Mandatory  | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| duration   | number                                   | Yes   | Duration for playing the animation, in milliseconds. The default value is **0**.                      |
| easing     | string                                   | Yes   | Animation interpolation curve. The default value is **ease**.                       |
| delay      | number                                   | Yes   | Animation delay duration, in milliseconds. The default value is **0**, indicating that there is no delay.                |
| fill       | "none" \| "forwards" \| "backwards" \| "both" | Yes   | State of the animated target after the animation is executed. The default value is **none**, which means that the target will retain its end state (defined by the last keyframe) after the animation is executed.|
| direction  | "normal" \| "reverse" \| "alternate" \| "alternate-reverse" | Yes   | Animation playback mode. The default value is **normal**.                     |
| iterations | number                                   | Yes   | Number of times that the animation is played. The default value is **1**. The value **0** means not to play the animation, and **-1** means to play the animation for an unlimited number of times.       |
| begin      | number                                   | Yes   | Start point of the animation interpolation. The default value is 0.                            |
| end        | number                                   | Yes   | End point of animation interpolation. The default value is 1.                            |


## Example
### JavaScript-compatible Web-like Development Paradigm

```html
<!-- hml -->
<div class="container">
  <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show">
  </div>
</div>
```

```js
export default {
  data: {
    divWidth: 200,
    divHeight: 200,
    animator: null
  },
  onInit() {
    let options = {
      duration: 1500,
      easing: 'friction',
      delay: 0,
      fill: 'forwards',
      direction: "normal",
      iterations: 2,
      begin: 200.0,
      end: 400.0
    };
    this.animator = animator.create(options);
  },
  Show() {
    let options1 = {
      duration: 1500,
      easing: 'friction',
      delay: 0,
      fill: 'forwards',
      direction: "normal",
      iterations: 2,
      begin: 0,
      end: 400.0,
    };
    try {
      this.animator.reset(options1);
    } catch(error) {
      console.error(`Animator reset failed, error code: ${error.code}, message: ${error.message}.`);
    }
    let _this = this;
    this.animator.onframe = function(value) {
      _this.divWidth = value;
      _this.divHeight = value;
    };
    this.animator.play();
  }
}
```

  ![en-us_image_00007](figures/en-us_image_00007.gif)

### ArkTS-based Declarative Development Paradigm

```ts
import animator from '@ohos.animator';

@Entry
@Component
struct AnimatorTest {
  private TAG: string = '[AnimatorTest]'
  private backAnimator: any = undefined
  private flag: boolean = false
  @State wid: number = 100
  @State hei: number = 100

  create() {
    let _this = this
    this.backAnimator = animator.create({
      duration: 2000,
      easing: 'ease',
      delay: 0,
      fill: 'none',
      direction: 'normal',
      iterations: 1,
      begin: 100,
      end: 200
    })
    this.backAnimator.onfinish = function () {
      _this.flag = true
      console.info(_this.TAG, 'backAnimator onfinish')
    }
    this.backAnimator.onrepeat = function () {
      console.info(_this.TAG, 'backAnimator repeat')
    }
    this.backAnimator.oncancel = function () {
      console.info(_this.TAG, 'backAnimator cancel')
    }
    this.backAnimator.onframe = function (value) {
      _this.wid = value
      _this.hei = value
    }
  }

  build() {
    Column() {
      Column() {
        Column()
          .width(this.wid)
          .height(this.hei)
          .backgroundColor(Color.Red)
      }
      .width('100%')
      .height(300)

      Column() {
        Row() {
          Button('create')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.create()
            })
        }
        .padding(10)

        Row() {
          Button('play')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = false
              this.backAnimator.play()
            })
        }
        .padding(10)

        Row() {
          Button('pause')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.backAnimator.pause()
            })
        }
        .padding(10)

        Row() {
          Button('finish')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = true
              this.backAnimator.finish()
            })
        }
        .padding(10)

        Row() {
          Button('reverse')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = false
              this.backAnimator.reverse()
            })
        }
        .padding(10)

        Row() {
          Button('cancel')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.backAnimator.cancel()
            })
        }
        .padding(10)

        Row() {
          Button('reset')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              if (this.flag) {
                this.flag = false
                this.backAnimator.reset({
                  duration: 5000,
                  easing: 'ease-in',
                  delay: 0,
                  fill: 'none',
                  direction: 'normal',
                  iterations: 4,
                  begin: 100,
                  end: 300
                })
              } else {
                console.info(this.TAG, 'Animation not ended')
              }
            })
        }
        .padding(10)
      }
    }
  }
}
```

## update<sup>(deprecated)</sup>

update(options: AnimatorOptions): void

Updates this animator.

This API is deprecated since API version 9. You are advised to use [reset<sup>9+</sup>](#reset9) instead.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                                 | Mandatory  | Description     |
| ------- | ----------------------------------- | ---- | ------- |
| options | [AnimatorOptions](#animatoroptions) | Yes   | Animator options.|

**Example**

```js
animator.update(options);
```

## createAnimator<sup>(deprecated)</sup>

createAnimator(options: AnimatorOptions): AnimatorResult

Creates an **Animator** object.

This API is deprecated since API version 9. You are advised to use [create<sup>9+</sup>](#create9) instead.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

| Name    | Type                                 | Mandatory  | Description     |
| ------- | ----------------------------------- | ---- | ------- |
| options | [AnimatorOptions](#animatoroptions) | Yes   | Animator options.|

**Return value**

| Type                               | Description           |
| --------------------------------- | ------------- |
| [AnimatorResult](#animatorresult) | Animator result.|

**Example**

```js
let options = {
  duration: 1500,
  easing: 'friction',
  delay: 0,
  fill: 'forwards',
  direction: "normal",
  iterations: 3,
  begin: 200.0,
  end: 400.0,
};
this.animator = animator.createAnimator(options);
```