1. 如果是本地圖片
主要是 aspect-radio css標(biāo)簽用法,保持縱橫比,需用看圖軟件查看圖片寬高然后填入aspectRadio 屬性,比如這里我的 sss.png 圖片 1700 是寬,600 是高則填寫 1700 / 600
Example: 寬度100%,高度自適應(yīng)
<Image source={require('../../assetc/images/sss.png')} style={{width: '100%', aspectRadio: 1700 / 600}} />
2. 如果是遠(yuǎn)程圖片
遠(yuǎn)程圖片需要異步先獲取一下尺寸,再動(dòng)態(tài)更改 Image 的 height
import {Image, useWindowDimensions} from 'react-native';
const Example = () => {
const [imageHeight, setImageHeight] = React.useState(0);
const {width} = useWindowDimensions();
React.useEffect(() => {
init();
}, []);
async function init(){
// todo..獲取遠(yuǎn)程API
Image.getSize("https://xx.com/ddd.png", (w, h) => {
setImageHeight(width / w * h); // 根據(jù)屏幕寬得到圖片寬縮放比例然后乘到高度上
});
}
return <View>
<Image source={require('../../assetc/images/sss.png')} style={{width: '100%', height: imageHeight}} />
</View>
}