简体   繁体   English

如何在视图中放置大图像而不在React Native中拉伸/裁剪图像?

[英]How to fit a large Image in a View without stretching/cropping the Image in React Native?

I want to use a Image in a View without affecting it's quality. 我想在View使用Image而不影响它的质量。 I don't want to crop the Image or stretch it. 我不想裁剪图像或拉伸它。 I also tried using with ImageBackground . 我也尝试过使用ImageBackground

https://snack.expo.io/r1kJ_rD5V?fbclid=IwAR0CVRu3YyQnbdVo9t2Vy1ygN-fGapT5coCQe-JUARJ3KNkkFNpx0KIh-OI https://snack.expo.io/r1kJ_rD5V?fbclid=IwAR0CVRu3YyQnbdVo9t2Vy1ygN-fGapT5coCQe-JUARJ3KNkkFNpx0KIh-OI

Please check this above link if you could help me using it. 如果您可以帮我使用,请查看以上链接。

did you try resizeMode="contain" or "cover" 你尝试过resizeMode =“contains”或“cover”吗?

https://facebook.github.io/react-native/docs/image.html#resizemode https://facebook.github.io/react-native/docs/image.html#resizemode

Firsly, we need to get screen dimensions for screen width; 首先,我们需要获得屏幕宽度的屏幕尺寸;

const { width } = Dimensions.get('window');

After that, we need to specify an image canvas for drawing our images and set resizeMode = {"contain"} to maintain the image's aspect ratio. 之后,我们需要指定一个用于绘制图像的图像画布,并设置resizeMode = {"contain"}以保持图像的纵横比。

_renderItem = item => {
  return (
    <Image
      style={{
        width: width, // We need to give specific width to place images
        height: '100%', // We need to set height to 100%
      }}
      source={item.images}
      resizeMode={'contain'}
    />
  );
};

Now we need to divide the screen height to 70:30 ; 现在我们需要将屏幕高度分成70:30 ;

return (
  <View style={styles.container}>
    <FlatList
      style={{ height: '70%' }} // We need to share the screen height with the View("A small view")
      data={images}
      renderItem={({ item }) => this._renderItem(item)}
      horizontal={true}
      showsHorizontalScrollIndicator={false}
      pagingEnabled={true}
    />
    <View style={{ height: '30%' }}>
      <Text>A small view</Text>
    </View>
  </View>
);

And last thing is to set a padding value to separate the view from status bar; 最后一件事是设置一个填充值来将视图与状态栏分开;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 20, // Just for iOS
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM