我一直在寻找有关如何使用react native在图像中绘制矩形的参考,但是我什么也没找到。
我想做的是像传递参数,照片和矩形对角线的顶点坐标作为参数,然后返回带有该绘制矩形的图像。我该怎么办?
答案 0 :(得分:0)
想简单..就像用背景色创建div一样。 这是示例:
import * as React from 'react';
import { Text, View, StyleSheet, Image } from 'react-native';
import { Constants } from 'expo';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.rectangle}></View>
<Image source={require('assets/snack-icon.png')} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
position: 'relative',
},
rectangle: {
height: 128,
width: 128,
backgroundColor: 'salmon',
position: 'absolute',
zIndex: 99,
top: '50%',
left: '40%'
},
});
结果:
答案 1 :(得分:0)
创建一个以Image和View(Rectangle box)作为子视图的视图。通过将绝对位置设置为矩形将Image上的矩形框放置在Image上。要定位,必须设置上,下,左和右的矩形样式值。已经创建了一个传递位置值的函数。
检查此示例:
import * as React from "react";
import { Text, View, StyleSheet, Image } from "react-native";
export default class App extends React.Component {
renderImage = (topPosition, bottomPosition, leftPosition, rightPosition) => {
return (
<View style={styles.imageContainer}>
<Image
source={{
uri:
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRzwZW9gvrxF2McRF-wP5TxCIBU_3fA2XDl9DESsm1uqowjSvZ1"
}}
style={styles.image}
resizeMode="stretch"
/>
<View
style={[
styles.rectangle,
{
top: topPosition,
bottom: bottomPosition,
left: leftPosition,
right: rightPosition
}
]}
/>
</View>
);
};
render() {
return (
<View style={styles.container}>{this.renderImage(80, 55, 30, 70)}</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "#ecf0f1",
padding: 8
},
imageContainer: {
width: 300,
height: 250,
alignSelf: "center"
},
image: {
width: 300,
height: 250
},
rectangle: {
borderWidth: 3,
borderColor: "red",
position: "absolute"
}
});