简体   繁体   English

大小未知的View中的本机图像大小

[英]React-Native Image sizing in View with unknown size

React-Naive suggests developers to state image dimension before runtime. React-Naive建议开发人员在运行时声明图像尺寸。 I know this is a good practice. 我知道这是个好习惯。 My question is... How about if I want to resize an <Image> to the same width as its contained <View> with unknown width? 我的问题是...如果我想将<Image>调整为与其包含的<View>相同的宽度,且宽度未知,该怎么办?

For example, I have an <Image> in a <View> with rowDirection: 'row', flex: 0.8 . 例如,我有一个<Image><View>rowDirection: 'row', flex: 0.8 In different device I have different screen width. 在不同的设备中,我具有不同的屏幕宽度。 How do I know the actual width of 80% of the device? 我如何知道设备80%的实际宽度?

PS I have tried all resizeMode but no luck. PS我尝试了所有的resizeMode但没有运气。

Can anyone help me thanks! 谁能帮我谢谢!

UPDATE 1 更新1

I have tried this snippet 我已经试过了

<View style={{ flexDirection: 'row', flex: 1, backgroundColor: '#CCF' }}>
    <Image
        source={require('../resources/images/side-menu-header.png')}
        style={{ width: null, height: null, flex: 0.8 }} />
    <View style={{ flex: 0.2 }} />
</View>

Result image of above code 上面代码的结果图

However, yes, this image is now set to 0.8 in width, but some its area has been cropped. 但是,是的,此图像的宽度现在设置为0.8,但是其某些区域已被裁剪。 This is not what I want to get. 这不是我想要的。 Therefore, I set its resizeMode to contain as below snippet 因此,我将其resizeMode设置为contain以下代码段

<View style={{ flexDirection: 'row', flex: 1, backgroundColor: '#CCF' }}>
    <Image
        resizeMode='contain'
        source={require('../resources/images/side-menu-header.png')}
        style={{ width: null, height: null, flex: 0.8 }} />
    <View style={{ flex: 0.2 }} />
</View>

Result image of above code 上面代码的结果图

This is not what I expected too! 这不是我所期望的! As you see, the image is now become 0.8 in width and the whole image is shown, but the top and bottom space is occupied by its original dimension. 如您所见,图像的宽度现在变为0.8,并且显示了整个图像,但是顶部和底部空间被其原始尺寸占据。 (The Original image dimension is large) (原始图像尺寸较大)

I think I can simplify this question in few words: How do I show a full width image in my app? 我想我可以用几个词简化这个问题:如何在我的应用程序中显示完整宽度的图像? I cannot set its width to 1080 since not all device has a width of 1080 pixel. 我无法将其宽度设置为1080,因为并非所有设备的宽度都为1080像素。

Use Dimensions API for making image size dynamic. 使用Dimensions API使图像尺寸动态变化。

Sample code: 样例代码:

const { width, height } = Dimensions.get('window');
<Image
    source={{uri: `${url}`}}
    style={{ width: width * 0.8, height: height * 0.8 }}
/>

The width * 0.8 and height * 0.8 is the 80 percentage for your screen size width and height respectively. 宽度* 0.8和高度* 0.8分别是屏幕尺寸宽度和高度的80%。

nullwidthheight传递给style道具,如下所示:

<Image resizeMode='cover' source={{ uri: 'image.png' }} style={{ width: null, height: null, flex: 0.8 }} />

As far as i'm concerned, what you need is a api to get the size of device. 就我而言,您需要的是一个API以获取设备的大小。

Try Dimensions, use like this: 尝试使用Dimensions,使用方法如下:

 let {height, width} = Dimensions.get('window'); 

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

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