W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
提供Lottie動(dòng)畫(huà)。
從 API Version 8 開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
- import lottie from '@ohos/lottie'
在第一次使用Lottie之前,需要在Terminal窗口運(yùn)行ohpm install @ohos/lottie 命令下載Lottie。
loadAnimation(path: string, container: object, render: string, loop: boolean, autoplay: boolean, name: string ): AnimationItem
加載動(dòng)畫(huà),須提前聲明Animator('__lottie_ets')對(duì)象,并在Canvas完成布局后調(diào)用。可配合Canvas組件生命周期接口onReady()使用。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
path | string | 是 | hap包內(nèi)動(dòng)畫(huà)資源文件路徑,僅支持json格式。示例:path: "common/lottie/data.json" |
container | object | 是 | canvas繪圖上下文,聲明范式需提前聲明CanvasRenderingContext2D。 |
render | string | 是 | 渲染類型,僅支持“canvas”。 |
loop | boolean | number | 否 | 動(dòng)畫(huà)播放結(jié)束后,是否循環(huán)播放,默認(rèn)值true。值類型為number,且大于等于1時(shí)為設(shè)置的重復(fù)播放的次數(shù)。 |
autoplay | boolean | 否 | 是否自動(dòng)播放動(dòng)畫(huà),默認(rèn)值true。 |
name | string | 否 | 開(kāi)發(fā)者自定義的動(dòng)畫(huà)名稱,后續(xù)支持通過(guò)該名稱引用控制動(dòng)畫(huà),默認(rèn)為空。 |
initialSegment | [number, number] | 否 | 指定動(dòng)畫(huà)播放的起始幀號(hào),指定動(dòng)畫(huà)播放的結(jié)束幀號(hào)。 |
destroy(name: string): void
銷毀動(dòng)畫(huà),頁(yè)面退出時(shí),必須調(diào)用??膳浜螩anvas組件生命周期接口使用,比如onDisappear()與onPageHide()。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 是 | 被指定的動(dòng)畫(huà)名,同loadAnimation接口參數(shù)name, 缺省時(shí)銷毀所有動(dòng)畫(huà)。 |
示例:
- // xxx.ets
- import lottie from '@ohos/lottie'
- @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,
- })
- })
- 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%')
- }
- }
play(name: string): void
播放指定動(dòng)畫(huà)。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 是 | 被指定的動(dòng)畫(huà)名, 同loadAnimation接口參數(shù)name,缺省時(shí)播放所有動(dòng)畫(huà)。 |
示例:
- lottie.play(this.animateName)
pause(name: string): void
暫停指定動(dòng)畫(huà),下次調(diào)用lottie.play()從當(dāng)前幀開(kāi)始。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 是 | 被指定的動(dòng)畫(huà)名,同loadAnimation接口入?yún)ame,缺省時(shí)暫停所有動(dòng)畫(huà)。 |
示例:
- lottie.pause(this.animateName)
togglePause(name: string): void
暫?;虿シ胖付▌?dòng)畫(huà),等效于lottie.play()與lottie.pause()切換調(diào)用。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 是 | 被指定的動(dòng)畫(huà)名,同loadAnimation接口參數(shù)name,缺省時(shí)停止所有動(dòng)畫(huà)。 |
示例:
- lottie.togglePause(this.animateName)
stop(name: string): void
停止指定動(dòng)畫(huà),下次調(diào)用lottie.play()從第一幀開(kāi)始。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 是 | 被指定的動(dòng)畫(huà)名,同loadAnimation接口參數(shù)name,缺省時(shí)停止所有動(dòng)畫(huà)。 |
示例:
- lottie.stop(this.animateName)
setSpeed(speed: number, name: string): void
設(shè)置指定動(dòng)畫(huà)播放速度。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
speed | number | 是 | 值為浮點(diǎn)類型, speed>0正向播放, speed<0反向播放, speed=0暫停播放, speed=1.0/-1.0正常速度播放。 |
name | string | 是 | 被指定的動(dòng)畫(huà),同loadAnimation接口參數(shù)name,缺省時(shí)停止所有動(dòng)畫(huà)。 |
示例:
- lottie.setSpeed(5, this.animateName)
setDirection(direction: AnimationDirection, name: string): void
設(shè)置指定動(dòng)畫(huà)播放順序。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
direction | AnimationDirection | 是 | 1為正向,-1為反向; 當(dāng)設(shè)置為反向時(shí),從當(dāng)前播放進(jìn)度開(kāi)始回播直到首幀,loop值為true時(shí)可無(wú)限倒放;speed<0疊加時(shí)也是倒放。 AnimationDirection:1 | -1 |
name | string | 是 | 被指定的動(dòng)畫(huà)名,同loadAnimation接口參數(shù)name,缺省時(shí)設(shè)置所有動(dòng)畫(huà)方向。 |
示例:
- lottie.setDirection(-1, this.animateName)
loadAnimation接口的返回對(duì)象,具有屬性與接口。屬性描述如下:
參數(shù)名稱 | 參數(shù)類型 | 參數(shù)描述 |
---|---|---|
name | string | 動(dòng)畫(huà)名稱。 |
isLoaded | boolean | 動(dòng)畫(huà)是否已加載。 |
currentFrame | number | 當(dāng)前播放的幀號(hào), 默認(rèn)精度為>=0.0的浮點(diǎn)數(shù), 調(diào)用setSubframe(false)后精度為去小數(shù)點(diǎn)后的正整數(shù)。 |
currentRawFrame | number | 當(dāng)前播放幀數(shù), 精度為>=0.0的浮點(diǎn)數(shù)。 |
firstFrame | number | 當(dāng)前播放片段的第一幀幀號(hào)。 |
totalFrames | number | 當(dāng)前播放片段的總幀數(shù)。 |
frameRate | number | 幀率 (frame/s)。 |
frameMult | number | 幀率 (frame/ms)。 |
playSpeed | number | 值為浮點(diǎn)類型, speed>0正向播放, speed<0反向播放, speed=0暫停播放, speed=1.0 | -1.0正常速度播放。 |
playDirection | number | 播放方向, 1為正放, -1為倒放。 |
playCount | number | 動(dòng)畫(huà)完成播放的次數(shù)。 |
isPaused | boolean | 當(dāng)前動(dòng)畫(huà)是否已暫停, 值為true動(dòng)畫(huà)已暫停。 |
autoplay | boolean | 加載動(dòng)畫(huà)后是否自動(dòng)播放, 若值為false需要再調(diào)用play()接口開(kāi)始播放。 |
loop | boolean | number | 類型為boolean時(shí)是否循環(huán)播放, 類型為number時(shí)播放次數(shù)。 |
renderer | any | 動(dòng)畫(huà)渲染對(duì)象, 根據(jù)渲染類型而定。 |
animationID | string | 動(dòng)畫(huà)ID。 |
timeCompleted | number | 當(dāng)前動(dòng)畫(huà)片段完成單次播放的幀數(shù), 受AnimationSegment設(shè)置影響, 與totalFrames屬性值相同。 |
segmentPos | number | 當(dāng)前動(dòng)畫(huà)片段序號(hào), 值為>=0的正整數(shù)。 |
isSubframeEnabled | boolean | 關(guān)聯(lián)了currentFrame的精度是否為浮點(diǎn)數(shù)。 |
segments | AnimationSegment | AnimationSegment[] | 當(dāng)前動(dòng)畫(huà)的播放片段。 |
play(name?: string): void
播放動(dòng)畫(huà)。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- this.animateItem.play()
destroy(name?: string): void
銷毀動(dòng)畫(huà)。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- this.animateItem.destroy()
pause(name?: string): void
暫停動(dòng)畫(huà),下次調(diào)用play接口從當(dāng)前幀開(kāi)始播放。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- this.animateItem.pause()
togglePause(name?: string): void
暫?;虿シ艅?dòng)畫(huà),等效于play接口與pause接口之間輪換調(diào)用。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- this.animateItem.togglePause()
stop(name?: string): void
停止動(dòng)畫(huà),下次調(diào)用play接口從第一幀開(kāi)始播放。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- this.animateItem.stop()
setSpeed(speed: number): void
設(shè)置動(dòng)畫(huà)播放速度。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
speed | number | 是 | 值為浮點(diǎn)類型, speed>0正向播放, speed<0反向播放, speed=0暫停播放, speed=1.0 | -1.0正常速度播放。 |
示例:
- this.animateItem.setSpeed(5);
setDirection(direction: AnimationDirection): void
設(shè)置動(dòng)畫(huà)播放順序。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
direction | AnimationDirection | 是 | 1為正向,-1為反向; 當(dāng)設(shè)置為反向時(shí),從當(dāng)前播放進(jìn)度開(kāi)始回播直到首幀,loop值為true時(shí)可無(wú)限倒放;speed<0疊加時(shí)也是倒放。 AnimationDirection:1 | -1。 |
示例:
- this.animateItem.setDirection(-1)
goToAndStop(value: number, isFrame?: boolean): void
設(shè)置動(dòng)畫(huà)停止在指定幀或時(shí)間進(jìn)度。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
value | number | 是 | 幀號(hào)(值大于等于0)或時(shí)間進(jìn)度(ms)。 |
isFrame | boolean | 否 | true: 按指定幀控制,false:按指定時(shí)間控制,缺省默認(rèn)false。 |
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- // 按幀號(hào)控制
- this.animateItem.goToAndStop(25, true)
- // 按時(shí)間進(jìn)度控制
- this.animateItem.goToAndStop(300, false, this.animateName)
goToAndPlay(value: number, isFrame: boolean, name?: string): void
設(shè)置動(dòng)畫(huà)從指定幀或時(shí)間進(jìn)度開(kāi)始播放。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
value | number | 是 | 幀號(hào)(值大于等于0)或時(shí)間進(jìn)度(ms) |
isFrame | boolean | 是 | true:按指定幀控制, false:按指定時(shí)間控制,缺省默認(rèn)false。 |
name | string | 否 | 被指定的動(dòng)畫(huà)名,缺省默認(rèn)為空。 |
示例:
- // 按幀號(hào)控制
- this.animateItem.goToAndPlay(25, true)
- // 按時(shí)間進(jìn)度控制
- this.animateItem.goToAndPlay(300, false, this.animateName)
playSegments(segments: AnimationSegment | AnimationSegment[], forceFlag: boolean): void
設(shè)置動(dòng)畫(huà)僅播放指定片段。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
segments | AnimationSegment = [number, number] | AnimationSegment[] | 是 | 片段或片段列表; 如果片段列表全部播放完畢后,下輪循環(huán)播放僅播放最后一個(gè)片段 |
forceFlag | boolean | 是 | true:即時(shí)生效播放,false:延遲到下輪循環(huán)播放再生效 |
示例:
- // 指定播放片段
- this.animateItem.playSegments([10, 20], false)
- // 指定播放片段列表
- this.animateItem.playSegments([[0, 5], [20, 30]], true)
resetSegments(forceFlag: boolean): void
重置動(dòng)畫(huà)播放片段,播放全幀。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
forceFlag | boolean | 是 | true:即時(shí)生效播放,false:延遲到下輪循環(huán)播放再生效 |
示例:
- this.animateItem.resetSegments(true)
setSubframe(useSubFrame: boolean): void
設(shè)置屬性currentFrame的精度顯示浮點(diǎn)數(shù)。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
useSubFrames | boolean | 是 | currentFrame屬性默認(rèn)顯示浮點(diǎn)數(shù),該接口參數(shù)將影響currentFrame屬性的精度。 true:屬性currentFrame顯示浮點(diǎn)。 false:屬性currentFrame去浮點(diǎn)數(shù)顯示整數(shù)。 |
示例:
- this.animateItem.setSubframe(false)
getDuration(inFrames?: boolean): void
獲取動(dòng)畫(huà)單次完整播放的時(shí)間(與播放速度無(wú)關(guān))或幀數(shù), 與Lottie.loadAnimation接口入?yún)nitialSegment有關(guān)。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
inFrames | boolean | 否 | true:獲取幀數(shù), false:獲取時(shí)間(單位ms),缺省默認(rèn)false。 |
示例:
- this.animateItem.getDuration(true)
addEventListener<T = any>(name: AnimationEventName, callback: AnimationEventCallback<T>): () => void
添加偵聽(tīng)事件, 事件完成后會(huì)觸發(fā)指定回調(diào)函數(shù)。返回可刪除該偵聽(tīng)事件的函數(shù)對(duì)象。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | AnimationEventName | 是 | 指定動(dòng)畫(huà)事件類型,Lottie內(nèi)置動(dòng)畫(huà)事件類型AnimationEventName: 'enterFrame'、'loopComplete'、'complete'、'segmentStart'、'destroy'、'config_ready'、'data_ready'、'DOMLoaded'、'error'、'data_failed'、'loaded_images' |
callback | AnimationEventCallback<T> | 是 | 用戶自定義回調(diào)函數(shù) |
示例:
- private callbackItem: any = function() {
- console.log("grunt loopComplete")
- }
- let delFunction = this.animateItem.addEventListener('loopComplete', this.animateName)
- // 刪除偵聽(tīng)
- delFunction()
removeEventListener<T = any>(name: AnimationEventName, callback?: AnimationEventCallback<T>): void
刪除偵聽(tīng)事件。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | AnimationEventName | 是 | 指定動(dòng)畫(huà)事件類型,Lottie內(nèi)置動(dòng)畫(huà)事件類型AnimationEventName: 'enterFrame'、'loopComplete'、'complete'、'segmentStart'、'destroy'、'config_ready'、'data_ready'、'DOMLoaded'、'error'、'data_failed'、'loaded_images' |
callback | AnimationEventCallback<T> | 否 | 用戶自定義回調(diào)函數(shù);缺省為空時(shí),刪除此事件的所有回調(diào)函數(shù)。 |
示例:
- this.animateItem.removeEventListener('loopComplete', this.animateName)
triggerEvent<T = any>(name: AnimationEventName, args: T): void
直接觸發(fā)指定事件的所有已設(shè)置的回調(diào)函數(shù)。
參數(shù):
參數(shù) | 類型 | 必填 | 描述 |
---|---|---|---|
name | AnimationEventName | 是 | 指定動(dòng)畫(huà)事件類型 |
args | any | 是 | 用戶自定義回調(diào)參數(shù) |
示例:
- 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)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: