只給這一個鏡頭,以及
你也可以等待Image onLayout回調來獲取其佈局屬性並使用它來更新維度。我爲此創建了一個組件:
import * as React from 'react';
import { Dimensions, Image, ImageProperties, LayoutChangeEvent, StyleSheet, ViewStyle } from 'react-native';
export interface FullWidthImageState {
width: number;
height: number;
stretched: boolean;
}
export default class FullWidthImage extends React.Component<ImageProperties, FullWidthImageState> {
constructor(props: ImageProperties) {
super(props);
this.state = { width: 100, height: 100, stretched: false };
}
render() {
return <Image {...this.props} style={this.getStyle()} onLayout={this.resizeImage} />;
}
private resizeImage = (event: LayoutChangeEvent) => {
if (!this.state.stretched) {
const width = Dimensions.get('window').width;
const height = width * event.nativeEvent.layout.height/event.nativeEvent.layout.width;
this.setState({ width, height, stretched: true });
}
};
private getStyle =(): ViewStyle => {
const style = [StyleSheet.flatten(this.props.style)];
style.push({ width: this.state.width, height: this.state.height });
return StyleSheet.flatten(style);
};
}
這將更新圖像的尺寸以匹配屏幕的寬度。
看看[react-native-scalable-image](https://www.npmjs.com/package/react-native-scalable-image) –