最佳答案
在我的反应原生应用程序中,我从一个未知尺寸的 API 中获取图像。如果我知道我想要的宽度,如何自动缩放高度?
例如:
我设置宽度为 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???<<<
}
)