-
Notifications
You must be signed in to change notification settings - Fork 789
/
MotionLayoutDemo.kt
204 lines (200 loc) · 6.45 KB
/
MotionLayoutDemo.kt
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.guru.composecookbook.ui.home.motionlayout
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.MotionLayout
import com.guru.composecookbook.data.AlbumsDataProvider
@Preview(group = "motion7")
@Composable
fun MotionLayoutDemo() {
Column(Modifier.background(Color.White)) {
ButtonsMotionExample()
Spacer(modifier = Modifier.height(200.dp))
ImageMotionExample()
}
}
@Composable
private fun ButtonsMotionExample() {
var animateButton by remember { mutableStateOf(false) }
val buttonAnimationProgress by animateFloatAsState(
targetValue = if (animateButton) 1f else 0f,
animationSpec = tween(1000)
)
Spacer(modifier = Modifier.height(16.dp))
MotionLayout(
ConstraintSet(
""" {
button1: {
width: "spread",
height: 60,
start: ['parent', 'start', 16],
end: ['parent', 'end', 16],
top: ['parent', 'top', 16]
},
button2: {
width: "spread",
height: 60,
start: ['parent', 'start', 16],
end: ['parent', 'end', 16],
top: ['button1', 'bottom', 16]
},
button3: {
width: "spread",
height: 60,
start: ['parent', 'start', 16],
end: ['parent', 'end', 16],
top: ['button2', 'bottom', 16]
}
} """
),
ConstraintSet(
""" {
button1: {
width: 100,
height: 60,
start: ['parent', 'start', 16],
end: ['button2', 'start', 16]
},
button2: {
width: 100,
height: 60,
start: ['button1', 'end', 16],
end: ['button2', 'start', 16]
},
button3: {
width: 100,
height: 60,
start: ['button2', 'end', 16],
end: ['parent', 'end', 16]
}
} """
),
progress = buttonAnimationProgress,
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.background(Color.White)
) {
Button(
onClick = { animateButton = !animateButton },
modifier = Modifier.layoutId("button1")
) {
Text(text = "Button1")
}
Button(
onClick = { animateButton = !animateButton },
modifier = Modifier.layoutId("button2")
) {
Text(text = "Button2")
}
Button(
onClick = { animateButton = !animateButton },
modifier = Modifier.layoutId("button3")
) {
Text(text = "Button3")
}
}
}
@Composable
private fun ImageMotionExample() {
val albums = AlbumsDataProvider.albums.take(4)
var animateImage by remember { mutableStateOf(false) }
val imageAnimationProgress by animateFloatAsState(
targetValue = if (animateImage) 1f else 0f,
animationSpec = tween(1000)
)
MotionLayout(
ConstraintSet(
""" {
image1: {
width: 150,
height: 150,
start: ['parent', 'start', 16]
},
image2: {
width: 150,
height: 150,
start: ['parent', 'start', 24]
},
image3: {
width: 150,
height: 150,
start: ['parent', 'start', 32]
},
image4: {
width: 150,
height: 150,
start: ['parent', 'start', 40]
}
} """
),
ConstraintSet(
""" {
image1: {
width: 150,
height: 150,
start: ['parent', 'start', 16]
},
image2: {
width: 150,
height: 150,
start: ['image1', 'end', 16]
},
image3: {
width: 150,
height: 150,
start: ['parent', 'start', 16],
top: ['image1', 'bottom', 16]
},
image4: {
width: 150,
height: 150,
start: ['image1', 'end', 16],
top: ['image1', 'bottom', 16]
}
} """
),
progress = imageAnimationProgress,
modifier = Modifier
.fillMaxSize()
.background(Color.White)
) {
Image(
painter = painterResource(id = albums[0].imageId),
contentDescription = "",
modifier = Modifier
.layoutId("image1")
.clickable { animateImage = !animateImage })
Image(
painter = painterResource(id = albums[1].imageId),
contentDescription = "",
modifier = Modifier
.layoutId("image2")
.clickable { animateImage = !animateImage })
Image(
painter = painterResource(id = albums[2].imageId),
contentDescription = "",
modifier = Modifier
.layoutId("image3")
.clickable { animateImage = !animateImage })
Image(
painter = painterResource(id = albums[3].imageId),
contentDescription = "",
modifier = Modifier
.layoutId("image4")
.clickable { animateImage = !animateImage })
}
}