React Native封装原生UI组件
我的博客原文地址 封装组件创建ViewManager的子类创建 public class LottieViewManager extends SimpleViewManager<LottieAnimationView> {
private static final String REACT_CLASS = "LottieAnimationView";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected LottieAnimationView createViewInstance(ThemedReactContext reactContext) {
return new LottieAnimationView(reactContext);
}
}
导出属性的设置方法要导出给JavaScript使用的属性,需要申明带有 @ReactProp(name = "sourceName")
public void setSourceName(LottieAnimationView view,String name) {
view.setAnimation(name);
}
@ReactProp(name = "progress",defaultFloat = 0f)
public void setProgress(LottieAnimationView view,float progress) {
view.setProgress(progress);
}
@ReactProp(name = "loop")
public void setLoop(LottieAnimationView view,boolean loop) {
view.loop(loop);
}
导出一些命令private static final int COMMAND_PLAY = 1;
private static final int COMMAND_RESET = 2;
@Override
public Map<String,Integer> getCommandsMap() {
return MapBuilder.of(
"play",COMMAND_PLAY,"reset",COMMAND_RESET
);
}
@Override
public void receiveCommand(final LottieAnimationView view,int commandId,@Nullable ReadableArray args) {
switch (commandId) {
case COMMAND_PLAY: {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override public void run() {
if (ViewCompat.isAttachedToWindow(view)) {
view.setProgress(0f);
view.playAnimation();
}
}
});
}
break;
case COMMAND_RESET: {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override public void run() {
if (ViewCompat.isAttachedToWindow(view)) {
view.cancelAnimation();
view.setProgress(0f);
}
}
});
}
break;
}
}
注册ViewManager实现 public class LottiePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(
new LottieViewManager()
);
}
}
添加组件在 @Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),new LottiePackage()
);
}
或者是在 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new LottiePackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager,"HelloWorld",null);
setContentView(mReactRootView);
}
实现对应的JavaScript模块创建Lottie.js import React,{ PropTypes } from 'react';
import {
requireNativeComponent,View,UIManager,findNodeHandle,ReactNative,Platform } from 'react-native';
/*
var LottieView = {
name: 'LottieView',defaultProps: {
progress: 0,loop: true,},propTypes: {
sourceName :PropTypes.string,progress: PropTypes.number,loop: PropTypes.bool,...View.propTypes // 包含默认的View的属性
},};
module.exports = requireNativeComponent('LottieAnimationView',LottieView);
*/
const Lottie = requireNativeComponent('LottieAnimationView',LottieView);
class LottieView extends React.Component {
constructor(props) {
super(props);
}
play() {
this.runCommand('play');
}
reset() {
this.runCommand('reset');
}
runCommand(name,args = []) {
return Platform.select({
android: () => UIManager.dispatchViewManagerCommand(
this.getHandle(),UIManager.LottieAnimationView.Commands[name],args
),ios: () => LottieViewManager[name](this.getHandle(),...args),})();
}
getHandle() {
return findNodeHandle(this.refs.lottieView);
}
render() {
return <Lottie ref="lottieView" {...this.props} loop={true} />;
}
}
module.exports = LottieView;
调用组件在index.android.js里面调用: import LottieView from './Lottie'
……
onClicked(){
this.refs.lottie.play();
}
render() {
return (
<TouchableWithoutFeedback onPress={() => this.onClicked()}>
<View style={styles.lottieContainer} >
<LottieView ref="lottie" style={styles.lottie} sourceName='LogoSmall.json' loop={true}>
</LottieView>
</View>
</TouchableWithoutFeedback>
)
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |