|
| 1 | +// eslint-disable-next-line import/default |
| 2 | +import React, { useState } from 'react'; |
| 3 | +import { Dimensions, Image, StyleSheet, TouchableOpacity, View } from 'react-native'; |
| 4 | +import { Blurhash } from 'gl-react-blurhash'; |
| 5 | +import { Surface } from 'gl-react-expo'; |
| 6 | + |
| 7 | +const screenWidth = Math.round(Dimensions.get('window').width); |
| 8 | + |
| 9 | +const styles = StyleSheet.create({ |
| 10 | + container: { |
| 11 | + flex: 1, |
| 12 | + backgroundColor: '#fff', |
| 13 | + alignItems: 'center', |
| 14 | + justifyContent: 'center' |
| 15 | + }, |
| 16 | + image: { |
| 17 | + width: screenWidth * 0.8, |
| 18 | + height: (screenWidth * 0.8) / 1.5 |
| 19 | + } |
| 20 | +}); |
| 21 | + |
| 22 | +const data = { |
| 23 | + hash: 'LPKA$w{H_c05b{Nqwbx^grotMnNf', |
| 24 | + uri: |
| 25 | + 'https://images.pexels.com/photos/1209843/pexels-photo-1209843.jpeg?crop=entropy&cs=srgb&dl=person-with-body-painting-1209843.jpg&fit=crop&fm=jpg&h=853&w=1280' |
| 26 | +}; |
| 27 | + |
| 28 | +function ImageWithBlurhash({ blurhashVisible }: { blurhashVisible: boolean }) { |
| 29 | + if (blurhashVisible) { |
| 30 | + return ( |
| 31 | + <Surface style={styles.image}> |
| 32 | + <Blurhash hash={data.hash} /> |
| 33 | + </Surface> |
| 34 | + ); |
| 35 | + } |
| 36 | + return <Image source={{ uri: data.uri }} style={styles.image}></Image>; |
| 37 | +} |
| 38 | + |
| 39 | +export default function App() { |
| 40 | + const [blurhashVisible, setBlurhashVisible] = useState(true); |
| 41 | + const onPress = () => setBlurhashVisible(!blurhashVisible); |
| 42 | + return ( |
| 43 | + <View style={styles.container}> |
| 44 | + <TouchableOpacity onPress={onPress}> |
| 45 | + <ImageWithBlurhash blurhashVisible={blurhashVisible} /> |
| 46 | + </TouchableOpacity> |
| 47 | + </View> |
| 48 | + ); |
| 49 | +} |
0 commit comments