Maintain aspect ratio of image with full width in React Native

前端 未结 12 1511
终归单人心
终归单人心 2020-12-07 13:02

I have a query regarding tag. I want an image to take entire width of parent which I do using alignSelf:stretch, but I also want the height to be according to the aspect ra

12条回答
  •  感情败类
    2020-12-07 13:44

    You can calculate the image height based on the width/height ratio.

    So if the image originally is 200x100, after setting its resizeMode to stretch:

    var deviceWidth: Dimensions.get('window').width;
    
    ...
    
    myImage {
        width: deviceWidth,
        height: deviceWidth * 0.5
    }
    

    I know maybe it is not the best practice, but it helped me a lot with images of all sizes that needed to mantain a certain relation with other images, etc.

提交回复
热议问题