ts-components-canvas-lottie.md 24.5 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 6 7
> **NOTE**
>
> 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 10

## Required Permissions
Z
zengyawen 已提交
11 12 13 14

None


Z
zengyawen 已提交
15 16
## Modules to Import

Z
zengyawen 已提交
17
```
E
ester.zhou 已提交
18
import lottie from '@ohos/lottieETS'
Z
zengyawen 已提交
19 20
```

21
> **NOTE**
E
ester.zhou 已提交
22
>
23
> In the **Terminal** window, run the `npm install @ohos/lottieETS` command to download Lottie.
E
ester.zhou 已提交
24 25
>
> 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 已提交
26 27 28 29 30 31 32 33


## lottie.loadAnimation

loadAnimation(

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

E
ester.zhou 已提交
34
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 已提交
35 36

- Parameters
E
ester.zhou 已提交
37 38 39 40 41 42 43 44 45
  | 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 已提交
46 47 48 49 50 51 52 53 54


## 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()**.

- Parameters
E
ester.zhou 已提交
55 56 57
  | 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 已提交
58 59

- Example
E
ester.zhou 已提交
60 61
  ```ts
  // xxx.ets
E
ester.zhou 已提交
62
  import lottie from '@ohos/lottieETS'
E
ester.zhou 已提交
63

Z
zengyawen 已提交
64 65 66 67 68 69 70
  @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 已提交
71 72

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

Z
zengyawen 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    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 已提交
92
          })
Z
zengyawen 已提交
93
        })
E
ester.zhou 已提交
94

Z
zengyawen 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        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 已提交
112

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

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


Z
zengyawen 已提交
128
## lottie.play
Z
zengyawen 已提交
129

Z
zengyawen 已提交
130
play(name: string): void
Z
zengyawen 已提交
131 132 133

Plays a specified animation.

Z
zengyawen 已提交
134
- Parameters
E
ester.zhou 已提交
135 136 137
  | 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 已提交
138 139

- Example
E
ester.zhou 已提交
140
  ```ts
Z
zengyawen 已提交
141 142 143 144 145 146 147 148 149 150 151
  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.

- Parameters
E
ester.zhou 已提交
152 153 154
  | 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 已提交
155 156

- Example
E
ester.zhou 已提交
157
  ```ts
Z
zengyawen 已提交
158 159 160 161 162 163 164 165 166 167 168
  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()**.

- Parameters
E
ester.zhou 已提交
169 170 171
  | 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 已提交
172 173

- Example
E
ester.zhou 已提交
174
  ```ts
Z
zengyawen 已提交
175 176 177 178 179 180 181 182 183 184 185
  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.

- Parameters
E
ester.zhou 已提交
186 187 188
  | 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 已提交
189 190

- Example
E
ester.zhou 已提交
191
  ```ts
Z
zengyawen 已提交
192 193 194 195 196 197 198
  lottie.stop(this.animateName)
  ```


## lottie.setSpeed

setSpeed(speed: number, name: string): void
Z
zengyawen 已提交
199 200 201

Sets the playback speed of the specified animation.

Z
zengyawen 已提交
202
- Parameters
E
ester.zhou 已提交
203 204 205 206
  | 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 in 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.|
  | 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 已提交
207 208

- Example
E
ester.zhou 已提交
209
  ```ts
Z
zengyawen 已提交
210 211 212 213 214 215 216 217 218 219 220
  lottie.setSpeed(5, this.animateName)
  ```


## lottie.setDirection

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

Sets the direction in which the specified animation plays.

- Parameters
E
ester.zhou 已提交
221 222 223 224
  | 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 已提交
225 226

- Example
E
ester.zhou 已提交
227
  ```ts
Z
zengyawen 已提交
228 229 230 231 232 233 234 235
  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 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| 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 已提交
258 259 260 261 262


## AnimationItem.play

play(name?: string): void
Z
zengyawen 已提交
263 264 265

Plays an animation.

Z
zengyawen 已提交
266
- Parameters
E
ester.zhou 已提交
267 268 269
  | Name | Type    | Mandatory  | Description             |
  | ---- | ------ | ---- | --------------- |
  | name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
270 271

- Example
E
ester.zhou 已提交
272
  ```ts
Z
zengyawen 已提交
273 274 275 276 277 278 279
  this.animateItem.play()
  ```


## AnimationItem.destroy

destroy(name?: string): void
Z
zengyawen 已提交
280 281 282

Destroys an animation.

Z
zengyawen 已提交
283
- Parameters
E
ester.zhou 已提交
284 285 286
  | Name | Type    | Mandatory  | Description             |
  | ---- | ------ | ---- | --------------- |
  | name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
287 288

- Example
E
ester.zhou 已提交
289
  ```ts
Z
zengyawen 已提交
290 291 292 293 294 295 296 297 298 299 300
  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.

- Parameters
E
ester.zhou 已提交
301 302 303
  | Name | Type    | Mandatory  | Description             |
  | ---- | ------ | ---- | --------------- |
  | name | string | No   | Name of the target animation. By default, the value is null.|
Z
zengyawen 已提交
304 305

- Example
E
ester.zhou 已提交
306
  ```ts
Z
zengyawen 已提交
307 308 309 310 311 312 313 314 315 316 317
  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**.

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

- Example
E
ester.zhou 已提交
323
  ```ts
Z
zengyawen 已提交
324 325 326 327 328 329 330 331 332 333 334
  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.

- 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 339

- Example
E
ester.zhou 已提交
340
  ```ts
Z
zengyawen 已提交
341 342 343 344 345 346 347
  this.animateItem.stop()
  ```


## AnimationItem.setSpeed

setSpeed(speed: number): void
Z
zengyawen 已提交
348 349 350

Sets the playback speed of an animation.

Z
zengyawen 已提交
351
- Parameters
E
ester.zhou 已提交
352 353 354
  | 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.|
Z
zengyawen 已提交
355 356

- Example
E
ester.zhou 已提交
357
  ```ts
Z
zengyawen 已提交
358 359
  this.animateItem.setSpeed(5);
  ```
Z
zengyawen 已提交
360 361


Z
zengyawen 已提交
362
## AnimationItem.setDirection
Z
zengyawen 已提交
363

Z
zengyawen 已提交
364
setDirection(direction: AnimationDirection): void
Z
zengyawen 已提交
365

Z
zengyawen 已提交
366
Sets the playback direction of an animation.
Z
zengyawen 已提交
367

Z
zengyawen 已提交
368
- Parameters
E
ester.zhou 已提交
369 370 371
  | 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 已提交
372

Z
zengyawen 已提交
373
- Example
E
ester.zhou 已提交
374
  ```ts
Z
zengyawen 已提交
375 376
  this.animateItem.setDirection(-1)
  ```
Z
zengyawen 已提交
377 378


Z
zengyawen 已提交
379
## AnimationItem.goToAndStop
Z
zengyawen 已提交
380

E
ester.zhou 已提交
381
goToAndStop(value: number, isFrame?: boolean): void
Z
zengyawen 已提交
382 383 384

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

Z
zengyawen 已提交
385
- Parameters
E
ester.zhou 已提交
386 387 388 389 390
  | 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 已提交
391 392

- Example
E
ester.zhou 已提交
393
  ```ts
Z
zengyawen 已提交
394 395 396 397 398 399 400 401 402
  // 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 已提交
403
goToAndPlay(value: number, isFrame: boolean, name?: string): void
Z
zengyawen 已提交
404 405 406

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

Z
zengyawen 已提交
407
- Parameters
E
ester.zhou 已提交
408 409 410 411 412
  | 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 已提交
413 414

- Example
E
ester.zhou 已提交
415
  ```ts
Z
zengyawen 已提交
416 417 418 419 420 421 422 423 424 425
  // 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 已提交
426 427 428

Sets the animation to play only the specified segment.

Z
zengyawen 已提交
429
- Parameters
E
ester.zhou 已提交
430 431 432 433
  | 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 已提交
434

Z
zengyawen 已提交
435
- Example
E
ester.zhou 已提交
436
  ```ts
Z
zengyawen 已提交
437 438 439 440 441
  // 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 已提交
442 443


Z
zengyawen 已提交
444
## AnimationItem.resetSegments
Z
zengyawen 已提交
445

Z
zengyawen 已提交
446
resetSegments(forceFlag: boolean): void
Z
zengyawen 已提交
447

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

Z
zengyawen 已提交
450
- Parameters
E
ester.zhou 已提交
451 452 453
  | 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 已提交
454 455

- Example
E
ester.zhou 已提交
456
  ```ts
Z
zengyawen 已提交
457 458
  this.animateItem.resetSegments(true)
  ```
Z
zengyawen 已提交
459 460


Z
zengyawen 已提交
461 462 463 464 465 466 467
## AnimationItem.resize

resize(): void

Resizes the animation layout.

- Example
E
ester.zhou 已提交
468
  ```ts
Z
zengyawen 已提交
469 470
  this.animateItem.resize()
  ```
Z
zengyawen 已提交
471 472


Z
zengyawen 已提交
473
## AnimationItem.setSubframe
Z
zengyawen 已提交
474

Z
zengyawen 已提交
475
setSubframe(useSubFrame: boolean): void
Z
zengyawen 已提交
476

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

Z
zengyawen 已提交
479
- Parameters
E
ester.zhou 已提交
480 481 482
  | 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 已提交
483

Z
zengyawen 已提交
484
- Example
E
ester.zhou 已提交
485
  ```ts
Z
zengyawen 已提交
486 487 488 489 490 491 492 493 494 495 496
  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.

- Parameters
E
ester.zhou 已提交
497 498 499
  | 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. The default value is **false**.|
Z
zengyawen 已提交
500 501

- Example
E
ester.zhou 已提交
502
  ```ts
Z
zengyawen 已提交
503 504
  this.animateItem.getDuration(true)
  ```
Z
zengyawen 已提交
505

Z
zengyawen 已提交
506 507 508 509

## AnimationItem.addEventListener

addEventListener&lt;T = any&gt;(name: AnimationEventName, callback: AnimationEventCallback&lt;T&gt;): () =&gt; void
Z
zengyawen 已提交
510 511 512

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.

Z
zengyawen 已提交
513
- Parameters
E
ester.zhou 已提交
514 515 516 517
  | 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 已提交
518 519

- Example
E
ester.zhou 已提交
520
  ```ts
Z
zengyawen 已提交
521 522 523 524
  private callbackItem: any = function() {
      console.log("grunt loopComplete")
  }
  let delFunction = this.animateItem.addEventListener('loopComplete', this.animateName)
E
ester.zhou 已提交
525

Z
zengyawen 已提交
526 527 528
  // Delete the event listener.
  delFunction()
  ```
Z
zengyawen 已提交
529 530


Z
zengyawen 已提交
531 532 533 534 535 536 537
## AnimationItem.removeEventListener

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

Removes an event listener.

- Parameters
E
ester.zhou 已提交
538 539 540 541
  | 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 已提交
542 543

- Example
E
ester.zhou 已提交
544
  ```ts
Z
zengyawen 已提交
545 546 547 548 549
  this.animateItem.removeEventListener('loopComplete', this.animateName)
  ```


## AnimationItem.triggerEvent
Z
zengyawen 已提交
550

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

Z
zengyawen 已提交
553 554 555
Directly triggers all configured callbacks of a specified event.

- Parameters
E
ester.zhou 已提交
556 557 558 559
  | Name  | Type                | Mandatory  | Description       |
  | ---- | ------------------ | ---- | --------- |
  | name | AnimationEventName | Yes   | Animation event type. |
  | args | any                | Yes   | Custom callback parameters.|
Z
zengyawen 已提交
560 561

- Example
E
ester.zhou 已提交
562
  ```ts
Z
zengyawen 已提交
563 564 565
  private triggerCallBack: any = function(item) {
      console.log("trigger loopComplete, name:" + item.name)
  }
E
ester.zhou 已提交
566

Z
zengyawen 已提交
567 568 569 570
  this.animateItem.addEventListener('loopComplete', this.triggerCallBack)
  this.animateItem.triggerEvent('loopComplete', this.animateItem)
  this.animateItem.removeEventListener('loopComplete', this.triggerCallBack)
  ```