forked from DylanVann/react-native-progressive-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (77 loc) · 2.24 KB
/
index.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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Animated, View, Image, StyleSheet } from 'react-native'
export default class ProgressiveImage extends Component {
constructor(props) {
super(props)
this.state = {
imageOpacity: new Animated.Value(0),
thumbnailOpacity: new Animated.Value(0),
}
}
onLoadThumbnail() {
Animated.timing(this.state.thumbnailOpacity, {
toValue: 1,
duration: this.props.thumbnailFadeDuration,
}).start()
this.props.onLoadThumbnail()
}
onLoadImage() {
Animated.timing(this.state.imageOpacity, {
toValue: 1,
duration: this.props.imageFadeDuration,
}).start()
this.props.onLoadImage()
}
render() {
return (
<View style={this.props.style}>
<Image
resizeMode="cover"
style={[styles.image, this.props.style]}
source={this.props.placeHolderSource}
/>
<Animated.Image
resizeMode="cover"
style={[styles.image, { opacity: this.state.thumbnailOpacity }, this.props.style]}
source={this.props.thumbnailSource}
onLoad={() => this.onLoadThumbnail()}
blurRadius={this.props.thumbnailBlurRadius}
/>
<Animated.Image
resizeMode="cover"
style={[styles.image, { opacity: this.state.imageOpacity }, this.props.style]}
source={this.props.imageSource}
onLoad={() => this.onLoadImage()}
/>
</View>
)
}
}
const styles = StyleSheet.create({
image: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
},
})
ProgressiveImage.propTypes = {
placeHolderColor: PropTypes.string,
placeHolderSource: PropTypes.number,
imageSource: PropTypes.object.isRequired,
imageFadeDuration: PropTypes.number.isRequired,
onLoadThumbnail: PropTypes.func.isRequired,
onLoadImage: PropTypes.func.isRequired,
thumbnailSource: PropTypes.object.isRequired,
thumbnailFadeDuration: PropTypes.number.isRequired,
thumbnailBlurRadius: PropTypes.number,
}
ProgressiveImage.defaultProps = {
thumbnailFadeDuration: 250,
imageFadeDuration: 250,
thumbnailBlurRadius: 5,
onLoadThumbnail: Function.prototype,
onLoadImage: Function.prototype,
}