如何在我的相机胶卷中完美添加margin: 1
?因为当我在margin: 1
中添加<Image .../>
时,顺序将不正确。
我想要的结果就像在Instagram中一样。左侧和右侧没有边距,对吗?
就像这张图片:
这是我的代码:
render() {
return(
<View style={styles.container}>
{/* <SelectedImage source={{uri: this.state.pickedImage}}/> */}
<Image source={{uri: this.state.pickedImage}} style={styles.image}/>
<ScrollView contentContainerStyle={styles.scrollView} showsVerticalScrollIndicator={false}>
{this.state.photos.map((photos, index) => {
return(
<TouchableHighlight
style={{opacity: index === this.state.index ? .5 : 1}}
onPress={() => this.setState({pickedImage: photos.node.image.uri})}
key={index}
underlayColor='transparent'
>
<Image
style={{width: width / 3, height: width /3}}
source={{uri: photos.node.image.uri}}
resizeMode='cover'
/>
</TouchableHighlight>
);
})}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
height: '100%',
width: '100%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white'
},
scrollView: {
flexWrap: 'wrap',
flexDirection: 'row'
},
scrollViewContainer: {
flex: 1,
flexDirection: 'row',
},
image: {
width: '100%',
height: 300,
backgroundColor: 'black'
}
});
这是我自己的结果:(不必担心“黑色边框”,我只是没有完美地裁剪,而且我的Android模拟器中只有6张照片)。
建议的答案:
componentDidMount() {
this.getPhotos();
this.rightButtonIcon();
requestExternalStoragePermission();
photoFilter = () => {
var arr = this.state.photos;
var number = 0;
arr.forEach((item) => {
switch(number) {
case 0:
number = 1;
item.position="left"
break;
case 1:
number = 2;
item.position="center"
break;
case 2:
number = 0;
item.position="right"
break;
default:
return null;
}
})
this.setState({photos: arr})
}
};
imageStylePositionCalc = (pos) =>{
if(pos==="left") return {marginRight:1,marginBottom:1}
if(pos==="center") return {marginRight:1,marginBottom:1}
if(pos==="right") return {marginBottom:1}
}
render() {
return(
<View style={styles.container}>
{/* <SelectedImage source={{uri: this.state.pickedImage}}/> */}
<Image source={{uri: this.state.pickedImage}} style={styles.image}/>
<ScrollView contentContainerStyle={styles.scrollView} showsVerticalScrollIndicator={false}>
{this.state.photos.map((photos, index) => {
return(
<TouchableHighlight
style={{opacity: index === this.state.index ? .5 : 1}}
onPress={() => this.setState({pickedImage: photos.node.image.uri})}
key={index}
underlayColor='transparent'
>
<Image
style={[{width: width / 3, height: width /3}, this.imageStylePositionCalc(photos.position)]}
source={{uri: photos.node.image.uri}}
resizeMode='cover'
/>
</TouchableHighlight>
);
})}
</ScrollView>
</View>
);
}
}
这是另一个问题,页边距:1占据了屏幕的整个宽度,第二行没有页边距:
答案 0 :(得分:1)
在绘制地图之前,我建议遍历数组的每个部分,并添加一个值,该值指示将其放置在右侧,左侧还是中央。 像这样:
photoFilter=()=>{
var arr=this.state.photos;
var number=0;
arr.forEach((item)=>{
switch (number){
case 0:
number=1;
item.position="left"
break;
case 1:
number=2;
item.position="center"
break;
case 2:
number=0;
item.position="right"
break;
default:
return null
}
})
this.setState({photos:arr})
}
然后,在渲染图像时:
<Image
style={[{width: width / 3, height: width /3},this.imageStylePositionCalc(item.position)]}
source={{ uri: photos.node.image.uri }}
resizeMode="cover"
/>
然后添加一个功能:
imageStylePositionCalc=(pos)=>{
if(pos==="left") return {marginRight:1,marginBottom:1}
if(pos==="center") return {marginRight:1,marginBottom:1}
if(pos==="right") return {marginBottom:1}
}
这可能不是最佳答案,但应该可以解决
更新: 问题是您在didMount中定义了photoFilter函数。
当我说在componentDidMount内部调用它时,我的意思是:
componentDidMount() {
this.getPhotos();
this.rightButtonIcon();
requestExternalStoragePermission();
this.photoFilter
}
photoFilter = () => {
var arr = this.state.photos;
var number = 0;
arr.forEach((item) => {
switch(number) {
case 0:
number = 1;
item.position="left"
break;
case 1:
number = 2;
item.position="center"
break;
case 2:
number = 0;
item.position="right"
break;
default:
return null;
}
})
this.setState({photos: arr})
}
答案 1 :(得分:0)
对于您的问题,我有一个解决方案,尽管它可能不是最好的解决方案,因为我不确定如何摆脱最高和最低利润,但是无论如何。
const styles = StyleSheet.create({
container: {
height: '100%',
width: '100%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white'
},
scrollView: {
flexWrap: 'wrap',
flexDirection: 'row',
justifyContent: 'space-between', // This one is new
marginHorizontal: -3 // This one is new
},
scrollViewContainer: {
flex: 1,
flexDirection: 'row'
},
image: {
width: '100%',
height: 300,
backgroundColor: 'black'
}
});
/* Rest of the code stays the same, only the image style gets changed to the follwoing */
<Image
style={{
width: width / 3,
height: width / 3,
margin: 1
}}
source={{ uri: photos.node.image.uri }}
resizeMode="cover"
/>
希望这会有所帮助。