使用React Native的网络图片进度指示器——react-native-image-progress
import React from 'react';
import { View, Text, Image } from 'react-native';
import FastImage from 'react-native-fast-image';
export default class ImageWithProgress extends React.Component {
render() {
return (
<View>
<FastImage
style={{ width: 100, height: 100 }}
source={{
uri: 'https://example.com/your-image.jpg',
priority: FastImage.priority.normal,
}}
resizeMode={FastImage.resizeMode.contain}
onLoadStart={() => this.setState({ progress: 0 })}
onProgress={({ nativeEvent }) => {
this.setState({ progress: nativeEvent.loaded / nativeEvent.total });
}}
onLoad={() => this.setState({ progress: 1 })}
/>
<Text>{Math.round(this.state.progress * 100)}%</Text>
</View>
);
}
}
这个例子使用了react-native-fast-image
库来替代react-native-image-progress
,因为react-native-fast-image
更加现代,并且是react-native-image-progress
的主要维护者推荐的替代品。代码展示了如何在React Native应用中加载一个网络图片,并且实时显示加载进度。
评论已关闭