/
AnimatableExamplesPage.kt
169 lines (160 loc) · 5.55 KB
/
AnimatableExamplesPage.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
package com.airbnb.lottie.sample.compose.examples
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Slider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.airbnb.lottie.compose.rememberLottieAnimatable
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.LottieConstants
import com.airbnb.lottie.compose.rememberLottieComposition
import com.airbnb.lottie.sample.compose.R
@Composable
fun AnimatableExamplesPage() {
UsageExamplePageScaffold {
Column(
modifier = Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState())
) {
ExampleCard("Example 1", "Repeat Forever") {
Example1()
}
ExampleCard("Example 2", "Draggable Progress Slider") {
Example2()
}
ExampleCard("Example 3", "Draggable Speed Slider") {
Example3()
}
ExampleCard("Example 4", "Repeat once. Click to repeat again") {
Example4()
}
ExampleCard("Example 5", "Click to toggle playback") {
Example5()
}
}
}
}
@Composable
private fun Example1() {
val anim = rememberLottieAnimatable()
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
LaunchedEffect(composition) {
anim.animate(
composition,
iterations = LottieConstants.IterateForever,
)
}
LottieAnimation(anim.composition, { anim.progress })
}
@Composable
private fun Example2() {
val anim = rememberLottieAnimatable()
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
var sliderGestureProgress: Float? by remember { mutableStateOf(null) }
LaunchedEffect(composition, sliderGestureProgress) {
when (val p = sliderGestureProgress) {
null -> anim.animate(
composition,
iterations = LottieConstants.IterateForever,
initialProgress = anim.progress,
continueFromPreviousAnimate = false,
)
else -> anim.snapTo(progress = p)
}
}
Box {
LottieAnimation(anim.composition, { anim.progress })
Slider(
value = sliderGestureProgress ?: anim.progress,
onValueChange = { sliderGestureProgress = it },
onValueChangeFinished = { sliderGestureProgress = null },
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 8.dp)
)
}
}
@Composable
private fun Example3() {
val anim = rememberLottieAnimatable()
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
var speed by remember { mutableStateOf(1f) }
LaunchedEffect(composition, speed) {
anim.animate(
composition,
iterations = LottieConstants.IterateForever,
speed = speed,
initialProgress = anim.progress,
)
}
Box {
LottieAnimation(composition, { anim.progress })
Slider(
value = speed,
onValueChange = { speed = it },
valueRange = -3f..3f,
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 8.dp)
)
Box(
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 24.dp)
.size(width = 1.dp, height = 16.dp)
.background(Color.Black)
)
}
}
@Composable
private fun Example4() {
var nonce by remember { mutableStateOf(1) }
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
val animatable = rememberLottieAnimatable()
LaunchedEffect(composition, nonce) {
composition ?: return@LaunchedEffect
animatable.animate(
composition,
continueFromPreviousAnimate = false,
)
}
LottieAnimation(
composition,
{ animatable.progress },
modifier = Modifier
.clickable { nonce++ }
)
}
@Composable
private fun Example5() {
var shouldPlay by remember { mutableStateOf(true) }
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
val animatable = rememberLottieAnimatable()
LaunchedEffect(composition, shouldPlay) {
if (composition == null || !shouldPlay) return@LaunchedEffect
animatable.animate(composition, iteration = LottieConstants.IterateForever)
}
LottieAnimation(
composition,
{ animatable.progress },
modifier = Modifier
.clickable { shouldPlay = !shouldPlay }
)
}