harmony 鸿蒙Lottie

2022-08-09 浏览 (858)

Lottie

Lottie allows you to implement animation-specific operations.

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.

Modules to Import

import lottie from '@ohos/lottieETS'

NOTE

In the Terminal window, run the npm install @ohos/lottieETS command to download Lottie. The download requires the related permission.

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.

lottie.loadAnimation

loadAnimation(

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

Loads an animation. Before calling this API, declare the Animator('__lottie_ets') object and make sure the canvas layout is complete. This API can be used together with the lifecycle callback onReady() of the Canvas component.

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the animation resource file in the HAP file. The resource file must be in JSON format. Example: path: "common/lottie/data.json"
containerobjectYesCanvas drawing context. A CanvasRenderingContext2D object must be declared in advance.
renderstringYesRendering type. The value can only be "canvas".
loopboolean |numberNoIf 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.
autoplaybooleanNoWhether to automatically play the animation.
Default value: true
namestringNoCustom animation name. In later versions, the name can be used to reference and control the animation.
Default value: null
initialSegment[number, number]NoStart frame and end frame of the animation, respectively.

lottie.destroy

destroy(name: string): void

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

Parameters

NameTypeMandatoryDescription
namestringYesName of the animation to destroy, which is the same as the name in the loadAnimation API. By default, all animations are destroyed.

Example

// xxx.ets
import lottie from '@ohos/lottieETS'

@Entry
@Component
struct Index {
  private controller: CanvasRenderingContext2D = new CanvasRenderingContext2D()
  private animateName: string = "animate"
  private animatePath: string = "common/lottie/data.json"
  private animateItem: any = null

  onPageHide(): void {
    console.log('onPageHide')
    lottie.destroy()
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.controller)
      .width('30%')
      .height('20%')
      .backgroundColor('#0D9FFB')
      .onReady(() => {
        console.log('canvas onAppear');
        this.animateItem = lottie.loadAnimation({
          container: this.controller,
          renderer: 'canvas',
          loop: true,
          autoplay: true,
          name: this.animateName,
          path: this.animatePath,
        })
      })

      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],
        })
      })

      Button('destroy animation')
        .onClick(() => {
          lottie.destroy(this.animateName)
          this.animateItem = null
        })
    }
    .width('100%')
    .height('100%')
  }
}

en-us_image_0000001194352468

lottie.play

play(name: string): void

Plays a specified animation.

Parameters

NameTypeMandatoryDescription
namestringYesName of the animation to play, which is the same as the name in the loadAnimation API. By default, all animations are played.

Example

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

NameTypeMandatoryDescription
namestringYesName of the animation to pause, which is the same as the name in the loadAnimation API. By default, all animations are paused.

Example

lottie.pause(this.animateName)

lottie.togglePause

togglePause(name: string): void

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

Parameters

NameTypeMandatoryDescription
namestringYesName of the target animation, which is the same as the name in the loadAnimation API. By default, all animations are paused or played.

Example

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

NameTypeMandatoryDescription
namestringYesName of the target animation, which is the same as the name in the loadAnimation API. By default, all animations are stopped.

Example

lottie.stop(this.animateName)

lottie.setSpeed

setSpeed(speed: number, name: string): void

Sets the playback speed of the specified animation.

Parameters

NameTypeMandatoryDescription
speednumberYesPlayback 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.
namestringYesName of the target animation, which is the same as the name in the loadAnimation API. By default, all animations are set.

Example

lottie.setSpeed(5, this.animateName)

lottie.setDirection

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

Sets the direction in which the specified animation plays.

Parameters

NameTypeMandatoryDescription
directionAnimationDirectionYesDirection 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.
AnimationDirection: 1 |-1
namestringYesName of the target animation, which is the same as the name in the loadAnimation API. By default, all animations are set.

Example

lottie.setDirection(-1, this.animateName)

AnimationItem

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

NameTypeDescription
namestringAnimation name.
isLoadedbooleanWhether the animation is loaded.
currentFramenumberFrame 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.
currentRawFramenumberNumber of frames that are being played. The precision is a floating point number greater than or equal to 0.0.
firstFramenumberFirst frame of the animation segment that is being played.
totalFramesnumberTotal number of frames in the animation segment that is being played.
frameRatenumberFrame rate (frame/s).
frameMultnumberFrame rate (frame/ms).
playSpeednumberPlayback 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.
playDirectionnumberPlayback direction.
1: forward.
-1: backward.
playCountnumberNumber of times the animation plays.
isPausedbooleanWhether the current animation is paused. The value true means that the animation is paused.
autoplaybooleanWhether to automatically play the animation upon completion of the loading. The value false means that the play() API needs to be called to start playing.
loopboolean |numberIf 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.
rendereranyAnimation rendering object, which depends on the rendering type.
animationIDstringAnimation ID.
timeCompletednumberNumber 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.
segmentPosnumberID of the current animation segment. The value is a positive integer greater than or equal to 0.
isSubframeEnabledbooleanWhether the precision of currentFrame is a floating point number.
segmentsAnimationSegment |AnimationSegment[]Current segment of the animation.

AnimationItem.play

play(name?: string): void

Plays an animation.

Parameters

NameTypeMandatoryDescription
namestringNoName of the target animation. By default, the value is null.

Example

this.animateItem.play()

AnimationItem.destroy

destroy(name?: string): void

Destroys an animation.

Parameters

NameTypeMandatoryDescription
namestringNoName of the target animation. By default, the value is null.

Example

this.animateItem.destroy()

AnimationItem.pause

pause(name?: string): void

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

Parameters

NameTypeMandatoryDescription
namestringNoName of the target animation. By default, the value is null.

Example

this.animateItem.pause()

AnimationItem.togglePause

togglePause(name?: string): void

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

Parameters

NameTypeMandatoryDescription
namestringNoName of the target animation. By default, the value is null.

Example

this.animateItem.togglePause()

AnimationItem.stop

stop(name?: string): void

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

Parameters

NameTypeMandatoryDescription
namestringNoName of the target animation. By default, the value is null.

Example

this.animateItem.stop()

AnimationItem.setSpeed

setSpeed(speed: number): void

Sets the playback speed of an animation.

Parameters

NameTypeMandatoryDescription
speednumberYesPlayback 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.

Example

this.animateItem.setSpeed(5);

AnimationItem.setDirection

setDirection(direction: AnimationDirection): void

Sets the playback direction of an animation.

Parameters

NameTypeMandatoryDescription
directionAnimationDirectionYesDirection 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.
AnimationDirection: 1 |-1.

Example

this.animateItem.setDirection(-1)

AnimationItem.goToAndStop

goToAndStop(value: number, isFrame?: boolean): void

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

Parameters

NameTypeMandatoryDescription
valuenumberYesFrame ID (greater than or equal to 0) or time progress (ms) at which the animation will stop.
isFramebooleanNoWhether 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.
Default value: false
namestringNoName of the target animation. By default, the value is null.

Example

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

goToAndPlay(value: number, isFrame: boolean, name?: string): void

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

Parameters

NameTypeMandatoryDescription
valuenumberYesFrame ID (greater than or equal to 0) or time progress (ms) at which the animation will start.
isFramebooleanYesWhether 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.
Default value: false
namestringNoName of the target animation.
Default value: null

Example

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

Sets the animation to play only the specified segment.

Parameters

NameTypeMandatoryDescription
segmentsAnimationSegment = [number, number] |AnimationSegment[]YesSegment or segment list.
If all segments in the segment list are played, only the last segment is played in the next cycle.
forceFlagbooleanYesWhether 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.

Example

// 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)

AnimationItem.resetSegments

resetSegments(forceFlag: boolean): void

Resets the settings configured by the playSegments API to play all the frames.

Parameters

NameTypeMandatoryDescription
forceFlagbooleanYesWhether 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.

Example

this.animateItem.resetSegments(true)

AnimationItem.resize

resize(): void

Resizes the animation layout.

Example

this.animateItem.resize()

AnimationItem.setSubframe

setSubframe(useSubFrame: boolean): void

Sets the precision of the currentFrame attribute to display floating-point numbers.

Parameters

NameTypeMandatoryDescription
useSubFramesbooleanYesWhether the currentFrame attribute displays floating-point numbers. By default, the attribute displays floating-point numbers.
true: The currentFrame attribute displays floating-point numbers.
false: The currentFrame attribute displays an integer and does not display floating-point numbers.

Example

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 API.

Parameters

NameTypeMandatoryDescription
inFramesbooleanNoWhether to obtain the duration or number of frames.
true: number of frames.
false: duration, in ms.
Default value: false

Example

this.animateItem.getDuration(true)

AnimationItem.addEventListener

addEventListener<T = any>(name: AnimationEventName, callback: AnimationEventCallback<T>): () => void

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

Parameters

NameTypeMandatoryDescription
nameAnimationEventNameYesAnimation event type. The available options are as follows:
'enterFrame', 'loopComplete', 'complete', 'segmentStart', 'destroy', 'config_ready', 'data_ready', 'DOMLoaded', 'error', 'data_failed', 'loaded_images'
callbackAnimationEventCallback<T>YesCustom callback.

Example

private callbackItem: any = function() {
    console.log("grunt loopComplete")
}
let delFunction = this.animateItem.addEventListener('loopComplete', this.animateName)

// Delete the event listener.
delFunction()

AnimationItem.removeEventListener

removeEventListener<T = any>(name: AnimationEventName, callback?: AnimationEventCallback<T>): void

Removes an event listener.

Parameters

NameTypeMandatoryDescription
nameAnimationEventNameYesAnimation event type. The available options are as follows:
'enterFrame', 'loopComplete', 'complete', 'segmentStart', 'destroy', 'config_ready', 'data_ready', 'DOMLoaded', 'error', 'data_failed', 'loaded_images'
callbackAnimationEventCallback<T>NoCustom callback. By default, the value is null, meaning that all callbacks of the event will be removed.

Example

this.animateItem.removeEventListener('loopComplete', this.animateName)

AnimationItem.triggerEvent

triggerEvent<T = any>(name: AnimationEventName, args: T): void

Directly triggers all configured callbacks of a specified event.

Parameters

NameTypeMandatoryDescription
nameAnimationEventNameYesAnimation event type.
argsanyYesCustom callback parameters.

Example

private triggerCallBack: any = function(item) {
    console.log("trigger loopComplete, name:" + item.name)
}

this.animateItem.addEventListener('loopComplete', this.triggerCallBack)
this.animateItem.triggerEvent('loopComplete', this.animateItem)
this.animateItem.removeEventListener('loopComplete', this.triggerCallBack)

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS-based Declarative Development Paradigm

harmony 鸿蒙Property Animator

harmony 鸿蒙Enums

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙Checkbox

harmony 鸿蒙CheckboxGroup

harmony 鸿蒙DataPanel

harmony 鸿蒙DatePicker

harmony 鸿蒙Divider

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/08f7d2a07cbf4b959008d18c86feb5b7