gpt4 book ai didi

android - 从 React native Js 代码调用 Android Native UI 组件方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:45 28 4
gpt4 key购买 nike

我创建了一个 CustomView SignatureView.java,它扩展了 LinearLayout 以在 Android Native 中捕获签名。

并创建了 SignatureCapturePackage.java 和 SignatureCaptureViewManager.java

public class SignatureCaptureMainView extends LinearLayout {

....

public void saveImage(){
//Save image to file
}
}

这是包类

public class SignatureCapturePackage implements ReactPackage {
private Activity mCurrentActivity;

public RSSignatureCapturePackage(Activity activity) {
mCurrentActivity = activity;
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
return Arrays.<ViewManager>asList(new SignatureCaptureViewManager(mCurrentActivity));
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Arrays.asList();
}
}

这是 ViewManager 类

 public class SignatureCaptureViewManager extends      ViewGroupManager<SignatureCaptureMainView> {
private Activity mCurrentActivity;

public static final String PROPS_SAVE_IMAGE_FILE="saveImageFileInExtStorage";
public static final String PROPS_VIEW_MODE = "viewMode";

public RSSignatureCaptureViewManager(Activity activity) {
mCurrentActivity = activity;
}

@Override
public String getName() {
return "SignatureView";
}

@ReactProp(name = PROPS_SAVE_IMAGE_FILE)
public void setSaveImageFileInExtStorage(SignatureCaptureMainView view, @Nullable Boolean saveFile) {
Log.d("React View manager setSaveFileInExtStorage:", "" + saveFile);
if(view!=null){
view.setSaveFileInExtStorage(saveFile);
}
}

@ReactProp(name = PROPS_VIEW_MODE)
public void setViewMode(SignatureCaptureMainView view, @Nullable String viewMode) {
Log.d("React View manager setViewMode:", "" + viewMode);
if(view!=null){
view.setViewMode(viewMode);
}
}

@Override
public SignatureCaptureMainView createViewInstance(ThemedReactContext context) {
Log.d("React"," View manager createViewInstance:");
return new SignatureCaptureMainView(context, mCurrentActivity);
}


}

这是 Signature.js 包

var React = require('react-native');
var {
PropTypes,
requireNativeComponent,
View,
} = React;

class SignatureCapture extends React.Component {

constructor() {
super();
this.onChange = this.onChange.bind(this);
}

onChange(event) {
console.log("Signature ON Change Event");
if (!this.props.onSaveEvent) {
return;
}

this.props.onSaveEvent({
pathName: event.nativeEvent.pathName,
encoded: event.nativeEvent.encoded,
});
}

render() {
return (
<SignatureView {...this.props} style={{flex: 1}} onChange={this.onChange} />
);
}

save(){

}
}

SignatureCapture.propTypes = {
...View.propTypes,
saveImageFileInExtStorage: PropTypes.bool,
viewMode:PropTypes.string
};

var SignatureView = requireNativeComponent('SignatureView', SignatureCapture, {
nativeOnly: {onChange: true}
});

module.exports = SignatureCapture;

我像这样在 ReactNative 中使用模块

<SignatureCapture
onSaveEvent={this._onSaveEvent}
saveImageFileInExtStorage={false}
viewMode={"portrait"}/>

一切正常。但是只有当 react 端发生某些点击事件时,我才必须保存图像。即,我必须从 reactnative js 代码调用 SignatureCaptureMainView 的 saveImage() 方法。

我怎样才能实现它?请帮忙

最佳答案

根据@agent_hunt 给出的指针。

检查这个blog解释

我在 SignatureCaptureViewManager 中使用了 ui 管理器命令。发布我的解决方案

public class SignatureCaptureViewManager extends ViewGroupManager<SignatureCaptureMainView> {
private Activity mCurrentActivity;

public static final String PROPS_SAVE_IMAGE_FILE="saveImageFileInExtStorage";
public static final String PROPS_VIEW_MODE = "viewMode";

public static final int COMMAND_SAVE_IMAGE = 1;


public SignatureCaptureViewManager(Activity activity) {
mCurrentActivity = activity;
}

@Override
public String getName() {
return "SignatureView";
}

@ReactProp(name = PROPS_SAVE_IMAGE_FILE)
public void setSaveImageFileInExtStorage(SignatureCaptureMainView view, @Nullable Boolean saveFile) {
Log.d("React View manager setSaveFileInExtStorage:", "" + saveFile);
if(view!=null){
view.setSaveFileInExtStorage(saveFile);
}
}

@ReactProp(name = PROPS_VIEW_MODE)
public void setViewMode(SignatureCaptureMainView view, @Nullable String viewMode) {
Log.d("React View manager setViewMode:", "" + viewMode);
if(view!=null){
view.setViewMode(viewMode);
}
}

@Override
public SignatureCaptureMainView createViewInstance(ThemedReactContext context) {
Log.d("React"," View manager createViewInstance:");
return new SignatureCaptureMainView(context, mCurrentActivity);
}

@Override
public Map<String,Integer> getCommandsMap() {
Log.d("React"," View manager getCommandsMap:");
return MapBuilder.of(
"saveImage",
COMMAND_SAVE_IMAGE);
}

@Override
public void receiveCommand(
SignatureCaptureMainView view,
int commandType,
@Nullable ReadableArray args) {
Assertions.assertNotNull(view);
Assertions.assertNotNull(args);
switch (commandType) {
case COMMAND_SAVE_IMAGE: {
view.saveImage();
return;
}

default:
throw new IllegalArgumentException(String.format(
"Unsupported command %d received by %s.",
commandType,
getClass().getSimpleName()));
}
}


}

为了向 ViewManager 发送命令,我在 Signature Capture 组件中添加了这个方法

class SignatureCapture extends React.Component {

constructor() {
super();
this.onChange = this.onChange.bind(this);
}

onChange(event) {
console.log("Signature ON Change Event");
if (!this.props.onSaveEvent) {
return;
}

this.props.onSaveEvent({
pathName: event.nativeEvent.pathName,
encoded: event.nativeEvent.encoded,
});
}

render() {
return (
<SignatureView {...this.props} style={{flex: 1}} onChange= {this.onChange} />
);
}

saveImage(){
UIManager.dispatchViewManagerCommand(
React.findNodeHandle(this),
UIManager.SignatureView.Commands.saveImage,
[],
);
}
}

SignatureCapture.propTypes = {
...View.propTypes,
rotateClockwise: PropTypes.bool,
square:PropTypes.bool,
saveImageFileInExtStorage: PropTypes.bool,
viewMode:PropTypes.string
};

var SignatureView = requireNativeComponent('SignatureView', SignatureCapture, {
nativeOnly: {onChange: true}
});

module.exports = SignatureCapture;

这就是我在父 Signature 组件中使用 SignatureCapture 组件的方式

class Signature extends Component {

render() {

return (
<View style={{ flex: 1, flexDirection: "column" }}>

<SignatureCapture
style={{ flex: 8 }}
ref="sign",
onSaveEvent={this._onSaveEvent}
saveImageFileInExtStorage={false}
viewMode={"portrait"}/>

<TouchableHighlight style={{ flex: 2 }}
onPress={() => { this.saveSign() } } >
<Text>Save</Text>
</TouchableHighlight>

</View>
);
}
// Calls Save method of native view and triggers onSaveEvent callback
saveSign() {
this.refs["sign"].saveImage();
}

_onSaveEvent(result) {
//result.encoded - for the base64 encoded png
//result.pathName - for the file path name
console.log(result);
}

}

export default Signature;

关于android - 从 React native Js 代码调用 Android Native UI 组件方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36265725/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com