React Native使用原生UI组件
发布时间:2020-12-15 07:30:08 所属栏目:百科 来源:网络整理
导读:在之前的一篇文章中,我记录了已有的Android项目如何接入React Native,介绍了RN如何调用原生的方法,本篇文章上在之前的一篇的文章的基础上续写的,这一篇文章中我将记录Android如何封装原生组件,然后RN来使用它。如果对接入RN还不太了解的,可以看看我的
在之前的一篇文章中,我记录了已有的Android项目如何接入React Native,介绍了RN如何调用原生的方法,本篇文章上在之前的一篇的文章的基础上续写的,这一篇文章中我将记录Android如何封装原生组件,然后RN来使用它。如果对接入RN还不太了解的,可以看看我的另一篇文章 Android原生项目接入React Native
从图中可以很清楚地看到实现步骤,具体如何实现,下面一步一步来介绍:
public class NativePaintViewManager extends SimpleViewManager<MyPaintView> {
public static final String REACT_CLASS = "RCTMyPaintView";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected MyPaintView createViewInstance(ThemedReactContext reactContext) {
return new MyPaintView(reactContext);
}
@ReactProp(name = "color")
public void setColor(MyPaintView paintView,String color) {
paintView.setColor(color);
}
@ReactProp(name = "raduis")
public void setRaduis(MyPaintView paintView,Integer raduis) {
paintView.setRaduis(raduis);
}
}
public class MyPaintView extends View {
/** * 画笔 */
private Paint mPaint;
/** * 圆的半径 */
private float raduis=100;
/** * view的宽和高 */
private int width;
private int height;
public MyPaintView(Context context) {
this(context,null);
}
public MyPaintView(Context context,AttributeSet attrs) {
this(context,attrs,0);
}
public MyPaintView(Context context,AttributeSet attrs,int defStyleAttr) {
super(context,defStyleAttr);
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(50);
}
@Override
protected void onSizeChanged(int w,int h,int oldw,int oldh) {
super.onSizeChanged(w,h,oldw,oldh);
width = w;
height = h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(width / 2,height / 2);
canvas.drawCircle(0,0,raduis,mPaint);
}
/** * 设置颜色 */
public void setColor(String color) {
mPaint.setColor(Color.parseColor(color));
invalidate();
}
/** * 设置圆的 半径 */
public void setRaduis(Integer raduis) {
this.raduis = raduis;
invalidate();
}
}
public class MyReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
//将我们创建的类添加进原生模块列表中
modules.add(new MyNativeModule(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
//返回值需要修改
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
//返回值需要修改
// return Collections.emptyList();
// 因为自定义的原生View,需要返回native的ViewManager
return Arrays.<ViewManager>asList(new NativePaintViewManager());
}
}
import { PropTypes } from 'react';
import { requireNativeComponent,View } from 'react-native';
var iface = {
name: 'RCTMyPaintView',propTypes: {
color: PropTypes.string,raduis: PropTypes.number,...View.propTypes // 包含默认的View的属性
},};
module.exports = requireNativeComponent('RCTMyPaintView',iface);
//导入组件
import PaintView from './NativePaintView';
// 使用
<PaintView style={styles.paintview}
color={"#FF5984"}
raduis={100}
/>
最后看下效果:最后提供代码地址github(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |