一款使用react-native 加载Harmony OS Svga动画开源插件

这是一款使用ReactNative 加载HarmonyOS Svga动画的播放器插件 三端Svga动画统一使用点击这里

版本:v1.1.5

react-native-ohos-svgaplayer

!TIP\] [Github 地址](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fyrjwcharm%2Freact-native-svga-player "https://github.com/yrjwcharm/react-native-svga-player")

安装与使用

npm
bash 复制代码
npm install react-native-ohos-svgaplayer  
yarn
bash 复制代码
yarn add react-native-ohos-svgaplayer

下面的代码展示了这个库的基本使用场景:

!WARNING\] 使用时 import 的库名不变。alias: react-native-svga-player 主要是统一 android/ios import导入

js 复制代码
import React from "react";
import { View, Dimensions, StyleSheet } from "react-native";
import RNSvgaPlayer from 'react-native-svga-player'

export function App() {
  return (
   <RNSvgaPlayer
    source="https://raw.githubusercontent.com/yyued/SVGAPlayer-iOS/master/SVGAPlayer/Samples/Goddess.svga"
        style={{
          width: 300,
          height: 150,
        }}
      />
  );
}

const styles = StyleSheet.create({
   container: {
    flex: 1,
    justifyContent: "flex-start",
    alignItems: "center",
  },
});

目前 HarmonyOS 暂不支持 AutoLink,所以 Link 步骤需要手动配置。

1、执行 package.json里的 codegen脚本命令 yarn codegen

js 复制代码
  "scripts": {
    "codegen": "react-native codegen-harmony --cpp-output-path ./harmony/entry/src/main/cpp/generated --rnoh-module-path ./harmony/entry/oh_modules/@rnoh/react-native-openharmony --no-safety-check"
  }

2.执行完codegen以后 会在 harmony工程 entry/src/main/cpp/generated下生成对应的头文件,该库默认有三个文件,特别注意生成的RNOHGeneratedPackage.h文件

3、接下来使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony

  • 1.在工程根目录的 oh-package.json5 添加 overrides 字段
json 复制代码
{
  ...
  "overrides": {
    "@rnoh/react-native-openharmony": "file:../libs/react_native_openharmony_release.har",//这个你项目工程怎么引入的就怎么引入
  
  }
}
  • 2.引入原生端代码 ,目前有两种方法:

      1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
      1. 直接链接源码。

方法一:通过 har 包引入(推荐)

!TIP\] har 包位于三方库安装路径的 `harmony` 文件夹下。

打开 entry/oh-package.json5,添加以下依赖

json 复制代码
"dependencies": {
    "@rnoh/react-native-openharmony": "file:../libs/react_native_openharmony_release.har",//这个你项目工程怎么引入的就怎么引入
    "@react-native-ohos/react-native-svga-player": "file:../../node_modules/react-native-ohos-svgaplayer/harmony/svgaplayer.har",
  },

点击右上角的 sync 按钮

或者在终端执行:

bash 复制代码
cd entry
ohpm install

方法二:直接链接源码

!TIP\] 从react-native-ohos-svga-player获取到svgaplayer源码文件,直接在harmony工程中通过File-\>New-\>Import-\>Import Module导入即可 主工程`entry/oh-package.json5`中添加

json 复制代码
"dependencies": {
    "@rnoh/react-native-openharmony": "file:../react_native_openharmony",
    "@react-native-ohos/react-native-svga-player": "file:../svgaplayer",

  }
  • 3.打开 entry/src/main/cpp/PackageProvider.cpp,添加:

    diff 复制代码
    #include "RNOH/PackageProvider.h"
    #include "SamplePackage.h"
    + #include "generated/RNOHGeneratedPackage.h"
    
    using namespace rnoh;
    
    std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
        return {
          std::make_shared<SamplePackage>(ctx),
    +     std::make_shared<RNOHGeneratedPackage>(ctx),
    
        };
    }
  • 4.在 ArkTs 侧引入 SvgaPlayerView 组件

找到 function buildCustomRNComponent() ,一般位于 entry/src/main/ets/pages/index.etsentry/src/main/ets/rn/LoadBundle.ets,添加:

diff 复制代码
  ...
+ import { SvgaPlayerView } from '@react-native-ohos/react-native-svga-player';

  @Builder
export function buildCustomRNComponent(ctx: ComponentBuilderContext) {
  ...
+  if (ctx.componentName === SvgaPlayerView.NAME) {
+     SvgaPlayerView({
+       ctx: ctx.rnComponentContext,
+       tag: ctx.tag,
+     })
+   }
    ...
  }
  ...
    1. !TIP\] 本库使用了混合方案,需要添加组件名。

entry/src/main/ets/pages/index.etsentry/src/main/ets/rn/LoadBundle.ets 找到常量 arkTsComponentNames 在其数组里添加组件名

diff 复制代码
const arkTsComponentNames: Array<string> = [
  SampleView.NAME,
  GeneratedSampleView.NAME,
  PropsDisplayer.NAME,
+ SvgaPlayerView.NAME
  ];

运行

点击右上角的 sync 按钮

或者在终端执行:

bash 复制代码
cd entry
ohpm install

然后编译、运行即可。

兼容三端[Android、iOS、Harmony]的 Svga动画 demo示例 -> github.com/yrjwcharm/r...

开源不易,希望您可以动一动小手点点小⭐⭐
👴希望大家如有好的需求踊跃提交,如有问题请提交issue,空闲时间会扩充与修复优化

开源协议

本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。

相关推荐
koooo~1 小时前
【无标题】
前端
Attacking-Coder1 小时前
前端面试宝典---前端水印
前端
姑苏洛言4 小时前
基于微信公众号小程序的课表管理平台设计与实现
前端·后端
烛阴4 小时前
比UUID更快更小更强大!NanoID唯一ID生成神器全解析
前端·javascript·后端
Alice_hhu4 小时前
ResizeObserver 解决 echarts渲染不出来,内容宽度为 0的问题
前端·javascript·echarts
逃逸线LOF5 小时前
CSS之动画(奔跑的熊、两面反转盒子、3D导航栏、旋转木马)
前端·css
萌萌哒草头将军6 小时前
⚡️Vitest 3.2 发布,测试更高效;🚀Nuxt v4 测试版本发布,焕然一新;🚗Vite7 beta 版发布了
前端
技术小丁6 小时前
使用 HTML + JavaScript 在高德地图上实现物流轨迹跟踪系统
前端·javascript·html
小小小小宇7 小时前
React 并发渲染笔记
前端
stark张宇7 小时前
Web - 面向对象
前端·javascript