Created
May 24, 2019 01:27
-
-
Save pachun/b3f2b2e3aa2a13c0d5621dfbd83397d3 to your computer and use it in GitHub Desktop.
Progressive Image from https://medium.com/react-native-training/progressive-image-loading-in-react-native-e7a01827feb7 updated for typescript
This file contains hidden or 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
| import * as React from "react" | |
| import { View, StyleSheet, Animated } from "react-native" | |
| const styles = StyleSheet.create({ | |
| imageOverlay: { | |
| position: "absolute", | |
| left: 0, | |
| right: 0, | |
| bottom: 0, | |
| top: 0, | |
| }, | |
| container: { | |
| backgroundColor: "#e1e4e8", | |
| }, | |
| }) | |
| interface IProps { | |
| thumbnailSource: string | |
| source: string | |
| style: object | |
| } | |
| class ProgressiveImage extends React.Component<IProps> { | |
| private thumbnailAnimated = new Animated.Value(0) | |
| private imageAnimated = new Animated.Value(0) | |
| public render() { | |
| const { thumbnailSource, source, style, ...props } = this.props | |
| return ( | |
| <View style={styles.container}> | |
| <Animated.Image | |
| {...props} | |
| source={thumbnailSource} | |
| style={[style, { opacity: this.thumbnailAnimated }]} | |
| onLoad={this.handleThumbnailLoad} | |
| blurRadius={1} | |
| /> | |
| <Animated.Image | |
| {...props} | |
| source={source} | |
| style={[styles.imageOverlay, { opacity: this.imageAnimated }, style]} | |
| onLoad={this.onImageLoad} | |
| /> | |
| </View> | |
| ) | |
| } | |
| private handleThumbnailLoad = () => { | |
| Animated.timing(this.thumbnailAnimated, { | |
| toValue: 1, | |
| }).start() | |
| } | |
| private onImageLoad = () => { | |
| Animated.timing(this.imageAnimated, { | |
| toValue: 1, | |
| }).start() | |
| } | |
| } | |
| export default ProgressiveImage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment