ts-components-canvas-lottie.md 24.6 KB
Newer Older
Z
zengyawen 已提交
1
# Lottie
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
**Lottie** allows you to implement animation-specific operations.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
> **NOTE**
E
esterzhou 已提交
6
> 
E
ester.zhou 已提交
7
> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

Z
zengyawen 已提交
10

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

Z
zengyawen 已提交
13
```
E
ester.zhou 已提交
14
import lottie from '@ohos/lottieETS'
Z
zengyawen 已提交
15 16
```

17
> **NOTE**
E
ester.zhou 已提交
18
>
E
ester.zhou 已提交
19
> In the **Terminal** window, run the `npm install @ohos/lottieETS` command to download Lottie. The download requires the related permission.
E
ester.zhou 已提交
20 21
>
> To install an OpenHarmony npm third-party package, run the `npm config set @ohos:registry=https://repo.harmonyos.com/npm/` command to set the repository address.
Z
zengyawen 已提交
22 23 24 25 26 27 28 29


## lottie.loadAnimation

loadAnimation(

path: string, container: object, render: string, loop: boolean, autoplay: boolean, name: string ): AnimationItem

E
ester.zhou 已提交
30
Loads an animation. Before calling this method, declare the **Animator('\__lottie\_ets')** object and check that the canvas layout is complete. This method can be used together with a lifecycle callback of the **Canvas** component, for example, **onAppear()** and **onPageShow()**.
Z
zengyawen 已提交
31

E
esterzhou 已提交
32 33
**Parameters**

E
ester.zhou 已提交
34 35 36 37 38 39 40 41 42
| Name            | Type                         | Mandatory  | Description                                      |
| -------------- | --------------------------- | ---- | ---------------------------------------- |
| path           | string                      | Yes   | Path of the animation resource file in the HAP file. The resource file must be in JSON format. Example: **path: "common/lottie/data.json"**|
| container      | object                      | Yes   | Canvas drawing context. A **CanvasRenderingContext2D** object must be declared in advance.|
| render         | string                      | Yes   | Rendering type. The value can only be **"canvas"**.                       |
| loop           | boolean \| number | No   | If the value is of the Boolean type, this parameter indicates whether to repeat the animation cyclically after the animation ends; the default value is **true**. If the value is of the number type and is greater than or equal to 1, this parameter indicates the number of times the animation plays.|
| autoplay       | boolean                     | No   | Whether to automatically play the animation. The default value is **true**.                       |
| name           | string                      | No   | Custom animation name. In later versions, the name can be used to reference and control the animation. The default value is null.       |
| initialSegment | [number, number]       | No   | Start frame and end frame of the animation, respectively.                |
Z
zengyawen 已提交
43 44 45 46 47 48 49 50


## lottie.destroy

destroy(name: string): void

Destroys the animation. This method must be called when a page exits. This method can be used together with a lifecycle callback of the **Canvas** component, for example, **onDisappear()** and **onPageHide()**.

E
esterzhou 已提交
51 52
**Parameters**

E
ester.zhou 已提交
53 54 55
| Name  | Type    | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---------------------------------------- |
| name | string | Yes   | Name of the animation to destroy, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are destroyed.|
Z
zengyawen 已提交
56

E
esterzhou 已提交
57
**Example** 
E
ester.zhou 已提交
58 59
  ```ts
  // xxx.ets
E
ester.zhou 已提交
60
  import lottie from '@ohos/lottieETS'
E
ester.zhou 已提交
61

Z
zengyawen 已提交
62 63 64 65 66 67 68
  @Entry
  @Component
  struct Index {
    private controller: CanvasRenderingContext2D = new CanvasRenderingContext2D()
    private animateName: string = "animate"
    private animatePath: string = "common/lottie/data.json"
    private animateItem: any = null
E
ester.zhou 已提交
69 70

    onPageHide(): void {
Z
zengyawen 已提交
71 72 73
      console.log('onPageHide')
      lottie.destroy()
    }
E
ester.zhou 已提交
74

Z
zengyawen 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.controller)
        .width('30%')
        .height('20%')
        .backgroundColor('#0D9FFB')
        .onAppear(() => {
          console.log('canvas onAppear');
          this.animateItem = lottie.loadAnimation({
            container: this.controller,
            renderer: 'canvas',
            loop: true,
            autoplay: true,
            name: this.animateName,
            path: this.animatePath,
Z
zengyawen 已提交
90
          })
Z
zengyawen 已提交
91
        })
E
ester.zhou 已提交
92

Z
zengyawen 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        Animator('__lottie_ets') // declare Animator('__lottie_ets') when use lottie
        Button('load animation')
          .onClick(() => {
          if (this.animateItem != null) {
            this.animateItem.destroy()
            this.animateItem = null
          }
          this.animateItem = lottie.loadAnimation({
            container: this.controller,
            renderer: 'canvas',
            loop: true,
            autoplay: true,
            name: this.animateName,
            path: this.animatePath,
            initialSegment: [10, 50],
          })
        })
E
ester.zhou 已提交
110

Z
zengyawen 已提交
111 112 113 114
        Button('destroy animation')
          .onClick(() => {
            lottie.destroy(this.animateName)
            this.animateItem = null
Z
zengyawen 已提交
115 116
          })
      }
Z
zengyawen 已提交
117 118
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
119
    }
Z
zengyawen 已提交
120 121
  }
  ```
Z
zengyawen 已提交
122

E
ester.zhou 已提交
123
  ![en-us_image_0000001194352468](figures/en-us_image_0000001194352468.gif)
Z
zengyawen 已提交
124 125


Z
zengyawen 已提交
126
## lottie.play
Z
zengyawen 已提交
127

Z
zengyawen 已提交
128
play(name: string): void
Z
zengyawen 已提交
129 130 131

Plays a specified animation.

E
esterzhou 已提交
132 133
**Parameters**

E
ester.zhou 已提交
134 135 136
| Name  | Type    | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---------------------------------------- |
| name | string | Yes   | Name of the animation to play, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are played.|
Z
zengyawen 已提交
137

E
esterzhou 已提交
138 139
**Example**

E
ester.zhou 已提交
140
  ```ts
Z
zengyawen 已提交
141 142 143 144 145 146 147 148 149 150
  lottie.play(this.animateName)
  ```


## lottie.pause

pause(name: string): void

Pauses a specified animation. The next time **lottie.play()** is called, the animation starts from the current frame.

E
esterzhou 已提交
151 152
**Parameters**

E
ester.zhou 已提交
153 154 155
| Name  | Type    | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---------------------------------------- |
| name | string | Yes   | Name of the animation to pause, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are paused.|
Z
zengyawen 已提交
156

E
esterzhou 已提交
157 158
**Example**

E
ester.zhou 已提交
159
  ```ts
Z
zengyawen 已提交
160 161 162 163 164 165 166 167 168 169
  lottie.pause(this.animateName)
  ```


## lottie.togglePause

togglePause(name: string): void

Pauses or plays a specified animation. This method is equivalent to the switching between **lottie.play()** and **lottie.pause()**.

E
esterzhou 已提交
170 171
**Parameters**

E
ester.zhou 已提交
172 173 174
| Name  | Type    | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---------------------------------------- |
| name | string | Yes   | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are paused.|
Z
zengyawen 已提交
175

E
esterzhou 已提交
176 177
**Example**

E
ester.zhou 已提交
178
  ```ts
Z
zengyawen 已提交
179 180 181 182 183 184 185 186 187 188
  lottie.togglePause(this.animateName)
  ```


## lottie.stop

stop(name: string): void

Stops the specified animation. The next time **lottie.play()** is called, the animation starts from the first frame.

E
esterzhou 已提交
189 190
**Parameters**

E
ester.zhou 已提交
191 192 193
| Name  | Type    | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---------------------------------------- |
| name | string | Yes   | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are paused.|
Z
zengyawen 已提交
194

E
esterzhou 已提交
195 196
**Example**

E
ester.zhou 已提交
197
  ```ts
Z
zengyawen 已提交
198 199 200 201 202 203 204
  lottie.stop(this.animateName)
  ```


## lottie.setSpeed

setSpeed(speed: number, name: string): void
Z
zengyawen 已提交
205 206 207

Sets the playback speed of the specified animation.

E
esterzhou 已提交
208 209
**Parameters**

E
ester.zhou 已提交
210 211 212 213
| Name   | Type    | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---------------------------------------- |
| speed | number | Yes   | Playback speed. The value is a floating-point number. If the value is greater than 0, the animation plays in forward direction. If the value is less than 0, the animation plays in reversed direction. If the value is 0, the animation is paused. If the value is 1.0 or -1.0, the animation plays at the normal speed.|
| name  | string | Yes   | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are stopped.|
Z
zengyawen 已提交
214

E
esterzhou 已提交
215 216
**Example**

E
ester.zhou 已提交
217
  ```ts
Z
zengyawen 已提交
218 219 220 221 222 223 224 225 226 227
  lottie.setSpeed(5, this.animateName)
  ```


## lottie.setDirection

setDirection(direction: AnimationDirection, name: string): void

Sets the direction in which the specified animation plays.

E
esterzhou 已提交
228 229
**Parameters**

E
ester.zhou 已提交
230 231 232 233
| Name       | Type                | Mandatory  | Description                                      |
| --------- | ------------------ | ---- | ---------------------------------------- |
| direction | AnimationDirection | Yes   | Direction in which the animation plays. **1**: forwards; **-1**: backwards. When set to play backwards, the animation plays from the current playback progress to the first frame. When this setting is combined with **loop** being set to **true**, the animation plays backwards continuously. When the value of **speed** is less than 0, the animation also plays backwards.<br>AnimationDirection: 1 \| -1 |
| name      | string             | Yes   | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are set.|
Z
zengyawen 已提交
234

E
esterzhou 已提交
235 236
**Example**

E
ester.zhou 已提交
237
  ```ts
Z
zengyawen 已提交
238 239 240 241 242 243 244 245
  lottie.setDirection(-1, this.animateName)
  ```


## AnimationItem

Defines an **AnimationItem** object, which is returned by the **loadAnimation** interface and has attributes and interfaces. The attributes are described as follows:

E
ester.zhou 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| Name             | Type                                    | Description                                    |
| ----------------- | ---------------------------------------- | ---------------------------------------- |
| name              | string                                   | Animation name.                                   |
| isLoaded          | boolean                                  | Whether the animation is loaded.                                |
| currentFrame      | number                                   | Frame that is being played. The default precision is a floating-point number greater than or equal to 0.0. After **setSubframe(false)** is called, the value is a positive integer without decimal points.|
| currentRawFrame   | number                                   | Number of frames that are being played. The precision is a floating point number greater than or equal to 0.0.           |
| firstFrame        | number                                   | First frame of the animation segment that is being played.                           |
| totalFrames       | number                                   | Total number of frames in the animation segment that is being played.                             |
| frameRate         | number                                   | Frame rate (frame/s).                      |
| frameMult         | number                                   | Frame rate (frame/ms).                     |
| playSpeed         | number                                   | Playback speed. The value is a floating-point number. If the value is greater than 0, the animation plays forward. If the value is less than 0, the animation plays backward. If the value is 0, the animation is paused.|If the value is **1.0** or **-1.0**, the animation plays at the normal speed.|
| playDirection     | number                                   | Playback direction. The options are **1** (forward) and **-1** (backward).            |
| playCount         | number                                   | Number of times the animation plays.                              |
| isPaused          | boolean                                  | Whether the current animation is paused. The value **true** means that the animation is paused.            |
| autoplay          | boolean                                  | Whether to automatically play the animation upon completion of the loading. The value **false** means that the **play()** interface needs to be called to start playing.|
| loop              | boolean \| number              | If the value is of the Boolean type, this parameter indicates whether to repeat the animation cyclically after the animation ends. If the value is of the number type and is greater than or equal to 1, this parameter indicates the number of times the animation plays. |
| renderer          | any                                      | Animation rendering object, which depends on the rendering type.                  |
| animationID       | string                                   | Animation ID.                                   |
| timeCompleted     | number                                   | Number of frames that are played for an animation sequence. The value is affected by the setting of **AnimationSegment** and is the same as the value of **totalFrames**.|
| segmentPos        | number                                   | ID of the current animation segment. The value is a positive integer greater than or equal to 0.            |
| isSubframeEnabled | boolean                                  | Whether the precision of **currentFrame** is a floating point number.               |
| segments          | AnimationSegment \| AnimationSegment[] | Current segment of the animation.                              |
Z
zengyawen 已提交
268 269 270 271 272


## AnimationItem.play

play(name?: string): void
Z
zengyawen 已提交
273 274 275

Plays an animation.

E
esterzhou 已提交
276 277
**Parameters**

E
ester.zhou 已提交
278 279 280
| Name  | Type    | Mandatory  | Description             |
| ---- | ------ | ---- | --------------- |
| name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
281

E
esterzhou 已提交
282 283
**Example**

E
ester.zhou 已提交
284
  ```ts
Z
zengyawen 已提交
285 286 287 288 289 290 291
  this.animateItem.play()
  ```


## AnimationItem.destroy

destroy(name?: string): void
Z
zengyawen 已提交
292 293 294

Destroys an animation.

E
esterzhou 已提交
295 296
**Parameters**

E
ester.zhou 已提交
297 298 299
| Name  | Type    | Mandatory  | Description             |
| ---- | ------ | ---- | --------------- |
| name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
300

E
esterzhou 已提交
301 302
**Example**

E
ester.zhou 已提交
303
  ```ts
Z
zengyawen 已提交
304 305 306 307 308 309 310 311 312 313
  this.animateItem.destroy()
  ```


## AnimationItem.pause

pause(name?: string): void

Pauses an animation. When the **play** interface is called next time, the animation is played from the current frame.

E
esterzhou 已提交
314 315
**Parameters**

E
ester.zhou 已提交
316 317 318
| Name  | Type    | Mandatory  | Description             |
| ---- | ------ | ---- | --------------- |
| name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
319

E
esterzhou 已提交
320 321
**Example**

E
ester.zhou 已提交
322
  ```ts
Z
zengyawen 已提交
323 324 325 326 327 328 329 330 331 332
  this.animateItem.pause()
  ```


## AnimationItem.togglePause

togglePause(name?: string): void

Pauses or plays an animation. This method is equivalent to the switching between **play** and **pause**.

E
esterzhou 已提交
333 334
**Parameters**

E
ester.zhou 已提交
335 336 337
| Name  | Type    | Mandatory  | Description             |
| ---- | ------ | ---- | --------------- |
| name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
338

E
esterzhou 已提交
339 340
**Example**

E
ester.zhou 已提交
341
  ```ts
Z
zengyawen 已提交
342 343 344 345 346 347 348 349 350 351
  this.animateItem.togglePause()
  ```


## AnimationItem.stop

stop(name?: string): void

Stops an animation. When the **play** interface is called next time, the animation is played from the first frame.

E
esterzhou 已提交
352 353
**Parameters**

E
ester.zhou 已提交
354 355 356
| Name  | Type    | Mandatory  | Description             |
| ---- | ------ | ---- | --------------- |
| name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
357

E
esterzhou 已提交
358 359
**Example**

E
ester.zhou 已提交
360
  ```ts
Z
zengyawen 已提交
361 362 363 364 365 366 367
  this.animateItem.stop()
  ```


## AnimationItem.setSpeed

setSpeed(speed: number): void
Z
zengyawen 已提交
368 369 370

Sets the playback speed of an animation.

E
esterzhou 已提交
371 372
**Parameters**

E
ester.zhou 已提交
373 374 375
| Name   | Type    | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---------------------------------------- |
| speed | number | Yes   | Playback speed. The value is a floating-point number. If the value is greater than 0, the animation plays forward. If the value is less than 0, the animation plays backward. If the value is 0, the animation is paused.|If the value is **1.0** or **-1.0**, the animation plays at the normal speed.|
Z
zengyawen 已提交
376

E
esterzhou 已提交
377 378
**Example**

E
ester.zhou 已提交
379
  ```ts
Z
zengyawen 已提交
380 381
  this.animateItem.setSpeed(5);
  ```
Z
zengyawen 已提交
382 383


Z
zengyawen 已提交
384
## AnimationItem.setDirection
Z
zengyawen 已提交
385

Z
zengyawen 已提交
386
setDirection(direction: AnimationDirection): void
Z
zengyawen 已提交
387

Z
zengyawen 已提交
388
Sets the playback direction of an animation.
Z
zengyawen 已提交
389

E
esterzhou 已提交
390 391
**Parameters**

E
ester.zhou 已提交
392 393 394
| Name       | Type                | Mandatory  | Description                                      |
| --------- | ------------------ | ---- | ---------------------------------------- |
| direction | AnimationDirection | Yes   | Direction in which the animation plays. **1**: forwards; **-1**: backwards. When set to play backwards, the animation plays from the current playback progress to the first frame. When this setting is combined with **loop** being set to **true**, the animation plays backwards continuously. When the value of **speed** is less than 0, the animation also plays backwards.<br>AnimationDirection: 1 \| -1.|
Z
zengyawen 已提交
395

E
esterzhou 已提交
396 397
**Example**

E
ester.zhou 已提交
398
  ```ts
Z
zengyawen 已提交
399 400
  this.animateItem.setDirection(-1)
  ```
Z
zengyawen 已提交
401 402


Z
zengyawen 已提交
403
## AnimationItem.goToAndStop
Z
zengyawen 已提交
404

E
ester.zhou 已提交
405
goToAndStop(value: number, isFrame?: boolean): void
Z
zengyawen 已提交
406 407 408

Sets the animation to stop at the specified frame or time.

E
esterzhou 已提交
409 410
**Parameters**

E
ester.zhou 已提交
411 412 413 414 415
| Name     | Type     | Mandatory  | Description                                      |
| ------- | ------- | ---- | ---------------------------------------- |
| value   | number  | Yes   | Frame ID (greater than or equal to 0) or time progress (ms) at which the animation will stop.                    |
| isFrame | boolean | No   | Whether to set the animation to stop at the specified frame. The value **true** means to set the animation to stop at the specified frame, and **false** means to set the animation to stop at the specified time progress. The default value is **false**.|
| name    | string  | No   | Name of the target animation. By default, the value is null.                         |
Z
zengyawen 已提交
416

E
esterzhou 已提交
417 418
**Example**

E
ester.zhou 已提交
419
  ```ts
Z
zengyawen 已提交
420 421 422 423 424 425 426 427 428
  // Set the animation to stop at the specified frame.
  this.animateItem.goToAndStop(25, true)
  // Set the animation to stop at the specified time progress.
  this.animateItem.goToAndStop(300, false, this.animateName)
  ```


## AnimationItem.goToAndPlay

E
ester.zhou 已提交
429
goToAndPlay(value: number, isFrame: boolean, name?: string): void
Z
zengyawen 已提交
430 431 432

Sets the animation to start from the specified frame or time progress.

E
esterzhou 已提交
433 434
**Parameters**

E
ester.zhou 已提交
435 436 437 438 439
| Name     | Type     | Mandatory  | Description                                      |
| ------- | ------- | ---- | ---------------------------------------- |
| value   | number  | Yes   | Frame ID (greater than or equal to 0) or time progress (ms) at which the animation will start.                     |
| isFrame | boolean | Yes   | Whether to set the animation to start from the specified frame. The value **true** means to set the animation to start from the specified frame, and **false** means to set the animation to start from the specified time progress. The default value is **false**.|
| name    | string  | No   | Name of the target animation. By default, the value is null.                         |
Z
zengyawen 已提交
440

E
esterzhou 已提交
441 442
**Example**

E
ester.zhou 已提交
443
  ```ts
Z
zengyawen 已提交
444 445 446 447 448 449 450 451 452 453
  // Set the animation to stop at the specified frame.
  this.animateItem.goToAndPlay(25, true)
  // Set the animation to stop at the specified time progress.
  this.animateItem.goToAndPlay(300, false, this.animateName)
  ```


## AnimationItem.playSegments

playSegments(segments: AnimationSegment | AnimationSegment[], forceFlag: boolean): void
Z
zengyawen 已提交
454 455 456

Sets the animation to play only the specified segment.

E
esterzhou 已提交
457 458
**Parameters**

E
ester.zhou 已提交
459 460 461 462
| Name       | Type                                      | Mandatory  | Description                                      |
| --------- | ---------------------------------------- | ---- | ---------------------------------------- |
| segments  | AnimationSegment = [number, number] \| AnimationSegment[] | Yes   | Segment or segment list.<br>If all segments in the segment list are played, only the last segment is played in the next cycle.|
| forceFlag | boolean                                  | Yes   | Whether the settings take effect immediately. The value **true** means the settings take effect immediately, and **false** means the settings take effect until the current cycle of playback is completed.          |
Z
zengyawen 已提交
463

E
esterzhou 已提交
464 465
**Example**

E
ester.zhou 已提交
466
  ```ts
Z
zengyawen 已提交
467 468 469 470 471
  // Set the animation to play the specified segment.
  this.animateItem.playSegments([10, 20], false)
  // Set the animation to play the specified segment list.
  this.animateItem.playSegments([[0, 5], [20, 30]], true)
  ```
Z
zengyawen 已提交
472 473


Z
zengyawen 已提交
474
## AnimationItem.resetSegments
Z
zengyawen 已提交
475

Z
zengyawen 已提交
476
resetSegments(forceFlag: boolean): void
Z
zengyawen 已提交
477

Z
zengyawen 已提交
478
Resets the settings configured by the **playSegments** method to play all the frames.
Z
zengyawen 已提交
479

E
esterzhou 已提交
480 481
**Parameters**

E
ester.zhou 已提交
482 483 484
| Name       | Type     | Mandatory  | Description                            |
| --------- | ------- | ---- | ------------------------------ |
| forceFlag | boolean | Yes   | Whether the settings take effect immediately. The value **true** means the settings take effect immediately, and **false** means the settings take effect until the current cycle of playback is completed.|
Z
zengyawen 已提交
485

E
esterzhou 已提交
486 487
**Example**

E
ester.zhou 已提交
488
  ```ts
Z
zengyawen 已提交
489 490
  this.animateItem.resetSegments(true)
  ```
Z
zengyawen 已提交
491 492


Z
zengyawen 已提交
493 494 495 496 497 498
## AnimationItem.resize

resize(): void

Resizes the animation layout.

E
esterzhou 已提交
499 500
**Example**

E
ester.zhou 已提交
501
  ```ts
Z
zengyawen 已提交
502 503
  this.animateItem.resize()
  ```
Z
zengyawen 已提交
504 505


Z
zengyawen 已提交
506
## AnimationItem.setSubframe
Z
zengyawen 已提交
507

Z
zengyawen 已提交
508
setSubframe(useSubFrame: boolean): void
Z
zengyawen 已提交
509

Z
zengyawen 已提交
510
Sets the precision of the **currentFrame** attribute to display floating-point numbers.
Z
zengyawen 已提交
511

E
esterzhou 已提交
512 513
**Parameters**

E
ester.zhou 已提交
514 515 516
| Name          | Type     | Mandatory  | Description                                      |
| ------------ | ------- | ---- | ---------------------------------------- |
| useSubFrames | boolean | Yes   | Whether the **currentFrame** attribute displays floating-point numbers. By default, the attribute displays floating-point numbers.<br>**true**: The **currentFrame** attribute displays floating-point numbers.<br>**false**: The **currentFrame** attribute displays an integer and does not display floating-point numbers.|
Z
zengyawen 已提交
517

E
esterzhou 已提交
518 519
**Example**

E
ester.zhou 已提交
520
  ```ts
Z
zengyawen 已提交
521 522 523 524 525 526 527 528 529 530
  this.animateItem.setSubframe(false)
  ```


## AnimationItem.getDuration

getDuration(inFrames?: boolean): void

Obtains the duration (irrelevant to the playback speed) or number of frames for playing an animation sequence. The settings are related to the input parameter **initialSegment** of the **Lottie.loadAnimation** interface.

E
esterzhou 已提交
531 532
**Parameters**

E
ester.zhou 已提交
533 534 535
| Name      | Type     | Mandatory  | Description                                      |
| -------- | ------- | ---- | ---------------------------------------- |
| inFrames | boolean | No   | Whether to obtain the duration or number of frames.<br>**true**: number of frames.<br>**false**: duration, in ms.<br>Default value: **false**|
E
esterzhou 已提交
536 537

**Example**
Z
zengyawen 已提交
538

E
ester.zhou 已提交
539
  ```ts
Z
zengyawen 已提交
540 541
  this.animateItem.getDuration(true)
  ```
Z
zengyawen 已提交
542

Z
zengyawen 已提交
543 544 545 546

## AnimationItem.addEventListener

addEventListener&lt;T = any&gt;(name: AnimationEventName, callback: AnimationEventCallback&lt;T&gt;): () =&gt; void
Z
zengyawen 已提交
547 548 549

Adds an event listener. After the event is complete, the specified callback function is triggered. This method returns the function object that can delete the event listener.

E
esterzhou 已提交
550 551
**Parameters**

E
ester.zhou 已提交
552 553 554 555
| Name      | Type                             | Mandatory  | Description                                      |
| -------- | ------------------------------- | ---- | ---------------------------------------- |
| name     | AnimationEventName              | Yes   | Animation event type. The available options are as follows:<br>'enterFrame', 'loopComplete', 'complete', 'segmentStart', 'destroy', 'config_ready', 'data_ready', 'DOMLoaded', 'error', 'data_failed', 'loaded_images'|
| callback | AnimationEventCallback&lt;T&gt; | Yes   | Custom callback.                               |
Z
zengyawen 已提交
556

E
esterzhou 已提交
557 558
**Example**

E
ester.zhou 已提交
559
  ```ts
Z
zengyawen 已提交
560 561 562 563
  private callbackItem: any = function() {
      console.log("grunt loopComplete")
  }
  let delFunction = this.animateItem.addEventListener('loopComplete', this.animateName)
E
ester.zhou 已提交
564

Z
zengyawen 已提交
565 566 567
  // Delete the event listener.
  delFunction()
  ```
Z
zengyawen 已提交
568 569


Z
zengyawen 已提交
570 571 572 573 574 575
## AnimationItem.removeEventListener

removeEventListener&lt;T = any&gt;(name: AnimationEventName, callback?: AnimationEventCallback&lt;T&gt;): void

Removes an event listener.

E
esterzhou 已提交
576 577
**Parameters**

E
ester.zhou 已提交
578 579 580 581
| Name      | Type                             | Mandatory  | Description                                      |
| -------- | ------------------------------- | ---- | ---------------------------------------- |
| name     | AnimationEventName              | Yes   | Animation event type. The available options are as follows:<br>'enterFrame', 'loopComplete', 'complete', 'segmentStart', 'destroy', 'config_ready', 'data_ready', 'DOMLoaded', 'error', 'data_failed', 'loaded_images'|
| callback | AnimationEventCallback&lt;T&gt; | No   | Custom callback. By default, the value is null, meaning that all callbacks of the event will be removed.           |
Z
zengyawen 已提交
582

E
esterzhou 已提交
583 584
**Example**

E
ester.zhou 已提交
585
  ```ts
Z
zengyawen 已提交
586 587 588 589 590
  this.animateItem.removeEventListener('loopComplete', this.animateName)
  ```


## AnimationItem.triggerEvent
Z
zengyawen 已提交
591

Z
zengyawen 已提交
592
triggerEvent&lt;T = any&gt;(name: AnimationEventName, args: T): void
Z
zengyawen 已提交
593

Z
zengyawen 已提交
594 595
Directly triggers all configured callbacks of a specified event.

E
esterzhou 已提交
596 597
**Parameters**

E
ester.zhou 已提交
598 599 600 601
| Name  | Type                | Mandatory  | Description       |
| ---- | ------------------ | ---- | --------- |
| name | AnimationEventName | Yes   | Animation event type. |
| args | any                | Yes   | Custom callback parameters.|
Z
zengyawen 已提交
602

E
esterzhou 已提交
603 604
**Example**

E
ester.zhou 已提交
605
  ```ts
Z
zengyawen 已提交
606 607 608
  private triggerCallBack: any = function(item) {
      console.log("trigger loopComplete, name:" + item.name)
  }
E
ester.zhou 已提交
609

Z
zengyawen 已提交
610 611 612 613
  this.animateItem.addEventListener('loopComplete', this.triggerCallBack)
  this.animateItem.triggerEvent('loopComplete', this.animateItem)
  this.animateItem.removeEventListener('loopComplete', this.triggerCallBack)
  ```