如果您想要在 Android React Native 应用中使用 @react-native-community/art,您可以使用以下步骤:
1.安装 @react-native-community/art:
npm install @react-native-community/art --save
2.完全重建已编译的JavaScript:
react-native bundle --entry-file index.js --platform android --dev false --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
3.在 app/build.gradle 文件中添加以下内容:
dependencies {
...
implementation project(":react-native-svg")
}
4.在 MainApplication.java 中注册 ARTPackage:
import com.facebook.react.shell.MainReactPackage;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import com.oblador.vectoricons.VectorIconsPackage;
// 在这里添加以下两行
import com.reactnativecommunity.art.ARTPackage;
import com.horcrux.svg.SvgPackage;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new VectorIconsPackage(),
// 添加以下两行
new ARTPackage(),
new SvgPackage()
);
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
5.在您想要使用 ART 的文件中导入该库:
import React, { Component } from 'react';
import { ART } from '@react-native-community/art';
// 更多代码
6.您可以使用 ART 画布及其组件,如下所示:
import React, { Component } from 'react';
import {
StyleSheet,
View,
ART,
} from '@react-native-community/art';
const {
Surface,
Group,
Shape,
Path,
Text,
} = ART;
// 更多代码