Skip to content

Commit 641f03d

Browse files
committed
clicking a thumbnail opens the image and occupies the screen
1 parent c326a3a commit 641f03d

File tree

1 file changed

+85
-18
lines changed

1 file changed

+85
-18
lines changed

src/HomeScreen.js

+85-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import React, { Component } from 'react'
2-
import { Platform, StyleSheet, View, Image, Dimensions, ImageBackground } from 'react-native';
2+
import {
3+
StyleSheet,
4+
View,
5+
Dimensions,
6+
ImageBackground,
7+
TouchableWithoutFeedback,
8+
Animated
9+
} from 'react-native';
310
import { Text } from 'react-native-ui-kitten'
411
import { SafeAreaView } from 'react-navigation'
5-
import { FlatList } from 'react-native-gesture-handler';
12+
import { FlatList, TouchableOpacity } from 'react-native-gesture-handler';
613

714
const WINDOW_WIDTH = Dimensions.get('window').width
815

@@ -16,25 +23,71 @@ class HomeScreen extends Component {
1623
state = {
1724
data: [],
1825
images: [
19-
{ title: "Breaking Bad", image: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
20-
{ title: "Breaking Bad", image: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
21-
{ title: "Breaking Bad", image: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
22-
{ title: "Breaking Bad", image: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
23-
{ title: "Breaking Bad", image: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
24-
]
26+
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
27+
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
28+
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
29+
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
30+
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
31+
],
32+
activeImage: null
33+
}
34+
35+
componentWillMount() {
36+
this.allImagesRefs = {}
37+
this.oldPosition = {}
38+
this.position = new Animated.ValueXY()
39+
this.dimensions = new Animated.ValueXY()
40+
}
41+
42+
43+
_openImage = (index) => {
44+
const ref = this.allImagesRefs[index]
45+
ref.measure((x, y, width, height, pageX, pageY) => {
46+
this.oldPosition.x = pageX
47+
this.oldPosition.y = pageY
48+
this.oldPosition.width = width
49+
this.oldPosition.height = height
50+
51+
this.position.setValue({
52+
x: pageX,
53+
y: pageY
54+
})
55+
56+
this.dimensions.setValue({
57+
x: width,
58+
y: height
59+
})
60+
61+
const img = this.state.images[index]
62+
this.setState({
63+
activeImage: img
64+
})
65+
})
2566
}
2667

2768
_keyExtractor = (item, index) => index.toString()
2869

29-
_renderItem = ({ item }) => {
30-
const { title, image } = item
70+
_renderItem = ({ item, index }) => {
71+
const { title, imageUrl } = item
3172

3273
return (
33-
<ImageBackground source={{ uri: image }} style={styles.largeThumbnail} imageStyle={{ borderRadius: 12 }} >
34-
<View style={{ position: 'absolute', bottom: 0 }}>
35-
< Text category="h4" style={styles.titleText}>{title} </Text>
36-
</View>
37-
</ImageBackground >
74+
<TouchableWithoutFeedback
75+
onPress={() => this._openImage(index)}
76+
key={index}>
77+
<Animated.View style={styles.imageContainer}>
78+
<View
79+
ref={imgRef => (this.allImagesRefs[index] = imgRef)}>
80+
<ImageBackground
81+
source={{ uri: imageUrl }}
82+
style={styles.largeThumbnail}
83+
imageStyle={{ borderRadius: 12 }}>
84+
<View style={{ position: 'absolute', bottom: 0 }}>
85+
<Text category="h4" style={styles.titleText}>{title} </Text>
86+
</View>
87+
</ImageBackground >
88+
</View>
89+
</Animated.View>
90+
</TouchableWithoutFeedback>
3891
)
3992
}
4093

@@ -49,6 +102,18 @@ class HomeScreen extends Component {
49102
renderItem={this._renderItem.bind(this)}
50103
keyExtractor={this._keyExtractor.bind(this)}
51104
/>
105+
<View
106+
style={StyleSheet.absoluteFill}
107+
pointerEvents={this.state.activeImage ? 'auto' : 'none'}
108+
>
109+
<View style={{ flex: 2 }}>
110+
<Animated.Image
111+
style={{ height: "80%", width: "100%" }}
112+
source={{ uri: this.state.activeImage ? this.state.activeImage.imageUrl : null }}
113+
>
114+
</Animated.Image>
115+
</View>
116+
</View>
52117
</SafeAreaView>
53118
)
54119
}
@@ -67,13 +132,15 @@ const styles = StyleSheet.create({
67132
marginBottom: 10,
68133
color: 'white'
69134
},
70-
largeThumbnail: {
71-
width: WINDOW_WIDTH - 40,
72-
height: WINDOW_WIDTH - 40,
135+
imageContainer: {
73136
marginRight: 20,
74137
marginLeft: 20,
75138
marginBottom: 40,
76139
borderRadius: 12,
140+
},
141+
largeThumbnail: {
142+
width: WINDOW_WIDTH - 40,
143+
height: WINDOW_WIDTH - 40,
77144
elevation: 4,
78145
shadowOffset: { height: 4, width: 4 },
79146
shadowOpacity: 0.8,

0 commit comments

Comments
 (0)