模板版本:v0.4.0

react-native-vector-drawable

Supported platforms License

[Github 地址](https://github.com/react-native-oh-library/react-native-vector-drawable)

安装与使用

该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/react-native-vector-drawable 版本所属关系如下:

三方库名称 三方库版本 发布信息 支持RN版本 Autolink 编译API版本 社区基线版本 npm地址
@react-native-ohos/react-native-vector-drawable ~0.5.2 Gitcode Releases 0.77/82 API12+ 0.5.1 Npm Address
@react-native-oh-tpl/react-native-vector-drawable ~0.5.1-0.0.1 Gitcode Releases 0.72 API12+ 0.5.1 Npm Address

对于未发布到npm的旧版本,请参考安装指南安装tgz包。

进入到工程目录并输入以下命令:

npm

# for RN0.72
npm install @react-native-oh-tpl/react-native-vector-drawable

# for RN0.77/82
npm install @react-native-ohos/react-native-vector-drawable

yarn

# for RN0.72
yarn add @react-native-oh-tpl/react-native-vector-drawable

# for RN0.77/82
yarn add @react-native-ohos/react-native-vector-drawable

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

使用时 import 的库名不变。


import React from "react";
import { View } from 'react-native';
import VectorDrawable from "react-native-vector-drawable";
export default function VectorDrawableDemo(): JSX.Element {
    return <View>
            <VectorDrawable
               resourceName="ic_drawable_name"
               style={{ width: 50, height: 50, tintColor: 'blue' }}
            />
    </View>
}

约束与限制

兼容性

要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。

在下述版本验证通过:

  1. RNOH:0.72.20; SDK: HarmonyOS NEXT Developer Beta1; IDE: DevEco Studio 5.0.3.200; ROM: 3.0.0.18;
  2. RNOH:0.77.18; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.868; ROM: 6.0.0.112;
  3. RNOH:0.82.18; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1 Release; ROM: 6.0.0.120 SP7;

属性

原库中Android平台实现是通过android.graphics.drawable.Drawable方法加载xml矢量图片文件,在iOS平台是通过React-Native View来实现,HarmonyOS与iOS保持一致,属性见React-Native View组件

"Platform"列表示该属性在原三方库上支持的平台。

"HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。

Name Description Type Required Platform HarmonyOS Support
resourceName Android 矢量图资源的名称。 string true Android No
style 样式配置,详见下方“style 属性”。注意:不支持 border(边框)相关属性。 object false Android No

style Props

Name Description Type Required Platform HarmonyOS Support
resizeMode 当显示区域(frame)与原图尺寸不匹配时,决定图片的缩放方式。可选值有:cover(覆盖)、contain(包含/适应)、stretch(拉伸)、center(居中)。 string false Android No
tintColor 将所有非透明像素的颜色更改为指定的色调颜色。 string false Android No

其他

开源协议

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