HarmonyOS 鸿蒙Next 如何实现lottie加载json动画

HarmonyOS 鸿蒙Next 如何实现lottie加载json动画

使用lottie加载动画

2 回复
import lottie, { AnimationItem } from '@ohos/lottie';

@Entry
@Component
struct Index {
  // 构建上下文
  private renderingSettings: RenderingContextSettings = new RenderingContextSettings(true)
  private canvasRenderingContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.renderingSettings)
  private renderingSettings1: RenderingContextSettings = new RenderingContextSettings(true)
  private canvasRenderingContext1: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.renderingSettings1)
  private renderingSettings2: RenderingContextSettings = new RenderingContextSettings(true)
  private canvasRenderingContext2: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.renderingSettings2)
  private animateItem: AnimationItem | null = null;
  private animateItem1: AnimationItem | null = null;
  private animateItem2: AnimationItem | null = null;

  // 页面销毁时释放动画资源
  aboutToDisappear(): void {
    console.info('aboutToDisappear');
    lottie.destroy();
  }

  build() {
    Column() {
      Row() {
        // 关联画布
        Canvas(this.canvasRenderingContext)
          .width(50)
          .height(50)
          .backgroundColor(Color.Gray)
          .onReady(() => {
            // 加载动画
            this.animateItem?.resize();
          })
        Canvas(this.canvasRenderingContext1)
          .width(50)
          .height(50)
          .backgroundColor(Color.Gray)
          .onReady(() => {
            // 加载动画
            this.animateItem1?.resize();
          })
        Canvas(this.canvasRenderingContext2)
          .width(50)
          .height(50)
          .backgroundColor(Color.Gray)
          .onReady(() => {
            // 加载动画
            this.animateItem2?.resize();
          })
      }

      Button('点赞')
        .onClick(() => {
          lottie.destroy('2016')
          this.animateItem = lottie.loadAnimation({
            container: this.canvasRenderingContext,
            renderer: 'canvas', // canvas 渲染模式
            loop: true,
            autoplay: true,
            name: '点赞',
            contentMode: 'Contain',
            path: "common/lottie/test.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径
            // path: "common/lottie/data_rawfile.json"
            // path: "common/lottie/data_base64.json"
          })

          this.animateItem1 = lottie.loadAnimation({
            container: this.canvasRenderingContext1,
            renderer: 'canvas', // canvas 渲染模式
            loop: true,
            autoplay: true,
            name: '点赞',
            contentMode: 'Contain',
            path: "common/lottie/test1.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径
            // path: "common/lottie/data_rawfile.json"
            // path: "common/lottie/data_base64.json"
          })

          this.animateItem2 = lottie.loadAnimation({
            container: this.canvasRenderingContext2,
            renderer: 'canvas', // canvas 渲染模式
            loop: true,
            autoplay: true,
            name: '点赞',
            contentMode: 'Contain',
            path: "common/lottie/test2.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径
            // path: "common/lottie/data_rawfile.json"
            // path: "common/lottie/data_base64.json"
          })
        })

      Button('销毁').onClick(() => {
        this.destroy();
      })
    }

  }

  destroy() {
    if (this.animateItem != null) {
      this.animateItem = null;
    }
    if (this.animateItem1 != null) {
      this.animateItem1 = null;
    }
    if (this.animateItem2 != null) {
      this.animateItem2 = null;
    }
    lottie.destroy();
  }

}

json动画文件放在模块的src/main/ets/common/lottie/路径下

更多关于HarmonyOS 鸿蒙Next 如何实现lottie加载json动画的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)系统中实现Lottie加载JSON动画,你可以通过以下步骤操作:

  1. 集成Lottie库:首先,你需要在项目中集成Lottie库。对于HarmonyOS应用,你可能需要寻找适用于该系统的Lottie库版本或者适配的动画库。确保库版本与你的开发环境兼容。

  2. 添加JSON动画文件:将你的Lottie JSON动画文件添加到项目的资源目录中。

  3. 加载和播放动画:使用Lottie库提供的API来加载和播放JSON动画。通常,你需要创建一个Lottie动画视图(例如LottieAnimationView),并设置其动画源为你的JSON文件。然后,你可以调用播放方法来显示动画。

  4. 自定义动画行为:根据需要,你可以设置动画的循环次数、播放速度等属性。

  5. 运行和测试:编译并运行你的应用,确保Lottie动画正确加载并播放。

注意,由于HarmonyOS的开发工具和生态系统与Android有所不同,具体的实现细节可能会有所变化。如果Lottie库尚未直接支持HarmonyOS,你可能需要寻找替代方案或自行实现动画解析和播放功能。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部