【问题标题】:React Native - Adding margin: 1 in Camera RollReact Native - 添加边距:相机胶卷中的 1
【发布时间】:2019-07-10 13:08:49
【问题描述】:

如何在我的相机胶卷中完美添加margin: 1?因为当我在<Image .../> 中添加margin: 1 时,顺序将不正确。

我想要的结果就像在 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>
); 
}
}

这是另一个问题,margin: 1 占据了屏幕的整个宽度,第二行没有margin:

【问题讨论】:

  • 您是否尝试将borderColor:"#fff" 添加到图像(或touchableOpacity)?
  • 不,我还没试过。
  • 正如我所提到的,我不希望第一列在左侧有边距,第三列我不希望在右侧有边距。
  • 如何渲染图像?每行是否有 2 个,或者它们可以像您发布的图片一样具有随机尺寸?
  • 当前图像是什么样的?请给我看结果屏幕。

标签: react-native stylesheet camera-roll


【解决方案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})
    }

【讨论】:

  • 它给了我错误“找不到变量:项目”,我应该在哪里传递 photoFilter() 函数?
  • 你可以把它放在你的 componentDidMount 函数中。将项目 item 更改为 photos
  • 我认为什么都没发生?
  • 你的意思是arr.forEach(item)arr.forEach(photos)吗?
  • 我的意思是当你调用 imageStylePositionCalc() 时设置样式时在 渲染中的那个
【解决方案2】:

对于您的问题,我有一个解决方案,虽然它可能不是最好的,因为我不确定如何摆脱上边距和下边距,但无论如何。

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"
/>

希望这会有所帮助。

【讨论】:

  • 不去我想要的设计哈哈。无论如何,感谢您的尝试。
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2021-04-04
  • 2016-11-26
相关资源
最近更新 更多