只给这一个镜头,以及
你也可以等待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) –