在我的React Native應用程序中,我從具有未知尺寸的API中提取圖像。如果我知道我想要的寬度,如何自動縮放高度?使用React Native自動縮放圖像高度
實施例:
我設置寬度爲Dimensions.get('window').width
。如何設置高度並保持相同的比例?
export default class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
imgUrl: 'http://someimg.com/coolstuff.jpg'
}
}
componentDidMount() {
// sets the image url to state
this.props.getImageFromAPi()
}
render() {
return (
<View>
<Image
source={uri: this.state.imgUrl}
style={styles.myImg}
/>
<Text>Some description</Text>
</View>
)
}
}
const styles = StyleSheet.create(
myImg: {
width: Dimensions.get('window').width,
height: >>>???what goes here???<<<
}
)
看一看[反應 - 本機可擴展的圖像](https://www.npmjs.com/package/react-native-scalable-image) –