-
-
Save alinz/d10a92d3dd0192a3df1197f9393de7eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import React, { Component } from 'react' | |
import { Image, Animated, View } from 'react-native' | |
type ProgressiveImageState = { | |
thumbnailOpacity: number, | |
key: string | |
} | |
type ProgressiveImageProps = { | |
backgroundColor: string, | |
style: any, | |
width: number, | |
height: number, | |
source: string, | |
thumbnail: string | |
} | |
let i = 0 | |
const genKey = (): string => { | |
return `key:${++i}` | |
} | |
export class ProgressiveImage extends Component { | |
props: ProgressiveImageProps | |
state: ProgressiveImageState | |
constructor(props: any, context: any) { | |
super(props, context) | |
this.state = { | |
thumbnailOpacity: new Animated.Value(0), | |
key: genKey() | |
} | |
} | |
onLoad = () => { | |
Animated.timing(this.state.thumbnailOpacity, { | |
toValue: 0, | |
duration: 250 | |
}).start(); | |
} | |
onThumbnailLoad = () => { | |
Animated.timing(this.state.thumbnailOpacity, { | |
toValue: 1, | |
duration: 250 | |
}).start(); | |
} | |
render() { | |
const { key, thumbnailOpacity } = this.state | |
const { width, height, source, thumbnail, style } = this.props | |
return ( | |
<View | |
width={width} | |
height={height} | |
backgroundColor={'#CCC'}> | |
<Animated.Image | |
resizeMode={'contain'} | |
key={key} | |
style={[ | |
style, | |
{ | |
position: 'absolute', | |
} | |
]} | |
source={source} | |
onLoad={this.onLoad} /> | |
<Animated.Image | |
resizeMode={'contain'} | |
key={key} | |
style={[ | |
style, | |
{ | |
opacity: thumbnailOpacity | |
} | |
]} | |
source={thumbnail} | |
onLoad={this.onThumbnailLoad} /> | |
</View> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@AFMW updated:
https://gist.github.com/DeVoresyah/d8888331509643edd612bca8a786bcb8