-
Notifications
You must be signed in to change notification settings - Fork 10
/
ImagePlaceholder.js
119 lines (111 loc) · 2.55 KB
/
ImagePlaceholder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React, { Component } from "react";
import {
ActivityIndicator,
Image,
StyleSheet,
Text,
View,
Animated
} from "react-native";
export default class ImagePlaceholder extends Component {
static defaultProps = {
duration: 750,
showActivityIndicator: true,
resizeMode: "cover"
};
constructor(props) {
super(props);
this.state = {
isLoading: true,
fadeAnim: new Animated.Value(1)
};
}
render() {
return (
<View style={[styles.container, this.props.style]}>
{this._renderPlaceholder.bind(this)()}
<Image
resizeMode={this.props.resizeMode}
onProgress={this._onProgress.bind(this)}
style={[styles.image, this.props.imageStyle]}
source={{ uri: this.props.src }}
/>
</View>
);
}
_onProgress(event) {
const progress = event.nativeEvent.loaded / event.nativeEvent.total;
this.setState({ isLoading: progress < 1 });
}
_renderPlaceholder() {
return (
<Animated.View style={this._getPlaceholderStyles()}>
<Image
resizeMode={this.props.resizeMode}
style={[styles.placeholder, this.props.placeholderStyle]}
source={{ uri: this.props.placeholder }}
/>
{this._renderActivityIndicator()}
</Animated.View>
);
}
_getPlaceholderStyles() {
let container = [styles.placeholderContainer];
if (!this.state.isLoading) {
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: this.props.duration
}).start();
container.push({ opacity: this.state.fadeAnim });
}
container.push(this.props.placeholderContainerStyle);
return container;
}
_renderActivityIndicator() {
if (this.props.showActivityIndicator) {
if (this.props.ActivityIndicator) {
return this.props.ActivityIndicator;
} else {
return (
<ActivityIndicator
{...this.props.activityIndicatorProps}
animating={this.state.isLoading}
/>
);
}
} else {
return null;
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignSelf: "stretch"
},
placeholderContainer: {
flex: 1,
alignSelf: "stretch",
zIndex: 2,
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
alignItems: "center",
justifyContent: "center"
},
placeholder: {
flex: 1,
alignSelf: "stretch",
zIndex: 2,
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0
},
image: {
flex: 1
}
});