HarmonyOS 关于lottie加载问题

代码如下:

import lottie, { AnimationItem } from '@ohos/lottie'
import StringUtils from 'lrs_commonlib/src/main/ets/utils/StringUtils'
import { JSON } from '@kit.ArkTS'
import { common } from '@kit.AbilityKit'

@Component
@Preview
export struct AnimatorLottieBase {
  @State animateName: string = ""
  //以下网络或者本地路径二选一
  //来自于网络
  @State uri: string = ""
  //来自于本地
  @State imagePath: string = ""
  @State path: string = ""
  // 动画填充模式 支持填充模式:Fill, Top, Cover, Bottom, Contain  默认模式是:Contain
  @State contentMode: string = "Contain"
  private mainRenderingSettings: RenderingContextSettings = new RenderingContextSettings(true)
  private mainCanvasRenderingContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.mainRenderingSettings)
  private animationItem?: AnimationItem

  aboutToAppear(): void {
    this.loadLottieAnimation()
  }
  aboutToDisappear(): void {
  }

  //播放Lottie动画
  loadLottieAnimation() {
    console.log("animationItem.addEventListener loadLottieAnimation", getContext().filesDir)
    if (StringUtils.isNotNilOrWhitespace(this.path)) {
      console.log("animationItem.addEventListener loadLottieAnimation 1", this.path)
      this.animationItem = lottie.loadAnimation({
        container: this.mainCanvasRenderingContext, // 渲染上下文
        renderer: 'canvas', // canvas 渲染模式
        loop: false, // 是否循环播放,默认true
        autoplay: true, // 是否自动播放,默认true
        imagePath: this.imagePath, // 加载读取指定路径下的图片资源
        path: this.path, // json路径
        contentMode: this.contentMode, // 填充的模式
        frameRate: 30, //设置animator的刷帧率为30
      })
    } else {
      console.log("animationItem.addEventListener loadLottieAnimation 2", this.uri)
      this.animationItem = lottie.loadAnimation({
        uri: this.uri, // uri网络资源
        container: this.mainCanvasRenderingContext, // 渲染上下文
        renderer: 'canvas', // canvas 渲染模式
        loop: false, // 是否循环播放,默认true
        autoplay: false, // 是否自动播放,默认true
        name: this.animateName, // 动画名
        contentMode: this.contentMode, // 填充的模式
        frameRate: 30, //设置animator的刷帧率为30
        isNetwork: false,
      })
    }
    setTimeout(() => {
      this.animationItem?.play() //当前指定animationItem动画播放
    }, 1000)
    this.animationItem.addEventListener("enterFrame", (data: object) => {
      console.log("animationItem.addEventListener enterFrame", JSON.stringify(data))
    })
    this.animationItem.addEventListener("loopComplete", (data: object) => {
      console.log("animationItem.addEventListener loopComplete", JSON.stringify(data))
    })
    this.animationItem.addEventListener("complete", (data: object) => {
      console.log("animationItem.addEventListener complete", JSON.stringify(data))
    })
    this.animationItem.addEventListener("segmentStart", (data: object) => {
      console.log("animationItem.addEventListener segmentStart", JSON.stringify(data))
    })
    this.animationItem.addEventListener("destroy", () => {
      console.log("animationItem.addEventListener destroy")
    })
    this.animationItem.addEventListener("config_ready", (data: object) => {
      console.log("animationItem.addEventListener config_ready", JSON.stringify(data))
    })
    this.animationItem.addEventListener("data_ready", (data: object) => {
      console.log("animationItem.addEventListener data_ready", JSON.stringify(data))
    })
    this.animationItem.addEventListener("DOMLoaded", (data: object) => {
      console.log("animationItem.addEventListener DOMLoaded", JSON.stringify(data))
    })
    this.animationItem.addEventListener("error", (data: object) => {
      console.log("animationItem.addEventListener error", JSON.stringify(data))
    })
    this.animationItem.addEventListener("data_failed", (data: object) => {
      console.log("animationItem.addEventListener data_failed",  data)
    })
    this.animationItem.addEventListener("loaded_images", (data: object) => {
      console.log("animationItem.addEventListener loaded_images", JSON.stringify(data))
    })
  }

  build() {
    Canvas(this.mainCanvasRenderingContext)
      .width("100%")
      .height("100%")
      .backgroundColor(Color.Transparent)
      .onAppear(() => {
        console.log("开始展示")
        // this.loadLottieAnimation()
      })
      .onReady(() => {
        //抗锯齿的设置
        this.mainCanvasRenderingContext.imageSmoothingEnabled = true;
        this.mainCanvasRenderingContext.imageSmoothingQuality = 'medium'
       })
      .onDisAppear(() => {
        console.log("结束展示")
        this.animationItem?.destroy()
      })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.

1,放在本地的资源如何播放

2,上面提供的那种再在里面包装一层,把lottie单独封装成一个组件就播放不了了,是为什么?

HarmonyOS
2025-01-10 08:11:31
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

问题一:加载本地动画,將动画需要的json文件放到pages同级别目录下,然后引用。path 参数:只支持加载entry/src/main/ets 文件夹下的相对路径,不支持跨包查找文件。注意:json文件路径不能使用 ./ 或者 …/ 等相对路径,相对路径获取不到动画源数据,会导致动画加载不出来,传递给loadAnimation 方法的路径是相对于pages父文件夹为基准的,而index页面内引入的相对路径的动画是以index.ets文件为基准的,两者基准不一致。

问题2:删除多余的监听,自定义组件里面使用动画,可以正常播放。

import lottie, { AnimationItem } from '@ohos/lottie'
import { JSON } from '@kit.ArkTS'

@Entry
@Component
@Preview
export struct AnimatorLottieBase {
  @State animateName: string = "火箭发射"
  //以下网络或者本地路径二选一
  //来自于网络
  @State uri: string = "xxxx"
  //来自于本地
  @State imagePath: string = ""
  @State path: string = ""
  // 动画填充模式 支持填充模式:Fill, Top, Cover, Bottom, Contain 默认模式是:Contain
  @State contentMode: string = "Contain"
  private mainRenderingSettings: RenderingContextSettings = new RenderingContextSettings(true)
  private mainCanvasRenderingContext: CanvasRenderingContext2D =
    new CanvasRenderingContext2D(this.mainRenderingSettings)
  private animationItem?: AnimationItem | null = null;
  aboutToDisappear() {
    lottie.destroy(this.animateName);
  }
  //播放Lottie动画
  loadLottieAnimation() {
    console.log("animationItem.addEventListener loadLottieAnimation", getContext().filesDir)
    if (StringUtils.isNotNilOrWhitespace(this.path)) {
      console.log("animationItem.addEventListener loadLottieAnimation 1", this.path)
      this.animationItem = lottie.loadAnimation({
        container: this.mainCanvasRenderingContext, // 渲染上下文
        renderer: 'canvas', // canvas 渲染模式
        loop: false, // 是否循环播放,默认true
        autoplay: true, // 是否自动播放,默认true
        imagePath: this.imagePath, // 加载读取指定路径下的图片资源
        path: this.path, // json路径
        contentMode: this.contentMode, // 填充的模式
        frameRate: 30, //设置animator的刷帧率为30
      })
    } else {
      console.log("animationItem.addEventListener loadLottieAnimation 2", this.uri)
      this.animationItem = lottie.loadAnimation({
        uri: this.uri, // uri网络资源
        container: this.mainCanvasRenderingContext, // 渲染上下文
        renderer: 'canvas', // canvas 渲染模式
        loop: false, // 是否循环播放,默认true
        autoplay: false, // 是否自动播放,默认true
        name: this.animateName, // 动画名
        isNetwork: true,
      })
      this.animationItem.addEventListener("error", (data: object) => {
        console.log("error", JSON.stringify(data))
      })
    }
    setTimeout(() => {
      this.animationItem?.play() //当前指定animationItem动画播放
    }, 300)
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Row({ space: 10 }) {
        // 关联画布
        Canvas(this.mainCanvasRenderingContext)
          .width(200)
          .height(200)
          .backgroundColor(Color.Gray)
          .onAppear(() => {
            console.log("开始展示")
          })
          .onReady(() => {
            this.animationItem?.resize();
            //抗锯齿的设置
            this.mainCanvasRenderingContext.imageSmoothingEnabled = true;
            this.mainCanvasRenderingContext.imageSmoothingQuality = 'medium'
            //this.loadLottieAnimation()
          })
          .onDisAppear(() => {
            console.log("结束展示")
            this.animationItem?.destroy()
            lottie.destroy(this.animateName);
          })
      }
      Row() {
        Button("'加载网络Zip'")
          .onClick(() => {
            lottie.destroy(this.animateName)
            this.loadLottieAnimation()
          })
      }
    }
  }
}
// 就是个判空的方法。判断是用本地的资源还是网络uri
export default class StringUtils {
  public static isNotNilOrWhitespace(input: string) {
    return (input?.trim()?.length ?? 0) > 0;
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
分享
微博
QQ
微信
回复
2025-01-10 11:58:17


相关问题
HarmonyOS Lottie无法全屏加载
517浏览 • 1回复 待解决
HarmonyOS 如何加载lottie动画
540浏览 • 1回复 待解决
HarmonyOS 怎样加载Lottie动画
475浏览 • 1回复 待解决
HarmonyOS lottie使用问题
761浏览 • 1回复 待解决
HarmonyOS lottie框架问题
404浏览 • 1回复 待解决
HarmonyOS Lottie动画加载不出来
558浏览 • 1回复 待解决
HarmonyOS Lottie动画有加载的指导吗
407浏览 • 1回复 待解决
关于Image组件加载网络图片的问题
1332浏览 • 1回复 待解决
关于自定义的XComponent加载so的问题
612浏览 • 1回复 待解决
HarmonyOS lottie动画
506浏览 • 1回复 待解决
HarmonyOS RN使用lottie
382浏览 • 1回复 待解决
HarmonyOS 关于cookie问题
658浏览 • 1回复 待解决
HarmonyOS 关于加固问题
430浏览 • 1回复 待解决
HarmonyOS 关于crash问题
1004浏览 • 1回复 待解决
HarmonyOS 关于手势问题
471浏览 • 1回复 待解决
HarmonyOS关于navigation问题
1161浏览 • 1回复 待解决
HarmonyOS lottie支持url吗?
662浏览 • 1回复 待解决
HarmonyOS RN是否适配lottie
369浏览 • 1回复 待解决
HarmonyOS 本地lottie动画无法播放
1101浏览 • 1回复 待解决