Skip to content

Commit 8724f46

Browse files
committed
Proposed fix to better time Compose UI animations
1 parent 555f00b commit 8724f46

13 files changed

+127
-14
lines changed

paparazzi-gradle-plugin/src/test/projects/compose/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ android {
2525

2626
dependencies {
2727
implementation libs.composeUi.material
28+
implementation libs.composeUi.uiTooling
2829
}
2930

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package app.cash.paparazzi.plugin.test
2+
3+
import androidx.compose.animation.AnimatedVisibility
4+
import androidx.compose.animation.animateColor
5+
import androidx.compose.animation.core.RepeatMode
6+
import androidx.compose.animation.core.animateFloat
7+
import androidx.compose.animation.core.infiniteRepeatable
8+
import androidx.compose.animation.core.rememberInfiniteTransition
9+
import androidx.compose.animation.core.tween
10+
import androidx.compose.animation.fadeIn
11+
import androidx.compose.animation.fadeOut
12+
import androidx.compose.foundation.background
13+
import androidx.compose.foundation.layout.Box
14+
import androidx.compose.foundation.layout.fillMaxSize
15+
import androidx.compose.foundation.layout.size
16+
import androidx.compose.material.Text
17+
import androidx.compose.runtime.Composable
18+
import androidx.compose.runtime.LaunchedEffect
19+
import androidx.compose.runtime.getValue
20+
import androidx.compose.runtime.mutableStateOf
21+
import androidx.compose.runtime.remember
22+
import androidx.compose.runtime.setValue
23+
import androidx.compose.ui.Modifier
24+
import androidx.compose.ui.draw.scale
25+
import androidx.compose.ui.graphics.Color
26+
import androidx.compose.ui.tooling.preview.Preview
27+
import androidx.compose.ui.unit.dp
28+
29+
@Preview
30+
@Composable
31+
fun SimpleAnimation() {
32+
Box(Modifier.fillMaxSize()) {
33+
var visible by remember {
34+
mutableStateOf(false)
35+
}
36+
37+
LaunchedEffect(Unit) {
38+
visible = true
39+
}
40+
41+
val infiniteTransition = rememberInfiniteTransition(label = "infinite-transition")
42+
val boxColor by infiniteTransition.animateColor(
43+
initialValue = Color.Red,
44+
targetValue = Color.Blue,
45+
animationSpec = infiniteRepeatable(tween(200, 300), RepeatMode.Reverse),
46+
label = "color"
47+
)
48+
49+
AnimatedVisibility(
50+
visible = visible,
51+
enter = fadeIn(tween(delayMillis = 200)),
52+
exit = fadeOut(tween(delayMillis = 100))
53+
) {
54+
Box(
55+
Modifier
56+
.size(100.dp)
57+
.background(boxColor)
58+
)
59+
}
60+
61+
val scale by
62+
infiniteTransition.animateFloat(
63+
initialValue = 0.2f,
64+
targetValue = 1f,
65+
animationSpec = infiniteRepeatable(tween(200, 100), RepeatMode.Reverse),
66+
label = "scale"
67+
)
68+
69+
Text(
70+
modifier = Modifier.scale(scale),
71+
text = "Hello, Paparazzi"
72+
)
73+
}
74+
}

paparazzi-gradle-plugin/src/test/projects/compose/src/test/java/app/cash/paparazzi/plugin/test/ComposeTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.material.Text
77
import androidx.compose.ui.Alignment
88
import androidx.compose.ui.Modifier
99
import androidx.compose.ui.graphics.Color
10+
import androidx.compose.ui.platform.ComposeView
1011
import app.cash.paparazzi.Paparazzi
1112
import org.junit.Rule
1213
import org.junit.Test
@@ -35,4 +36,21 @@ class ComposeTest {
3536
}
3637
}
3738
}
39+
40+
@Test
41+
fun animation() {
42+
val view = ComposeView(paparazzi.context).apply {
43+
setContent { SimpleAnimation() }
44+
}
45+
46+
paparazzi.gif(view, fps = 120)
47+
paparazzi.gif(view, name = "start-end", fps = 2, end = 500)
48+
paparazzi.gif(view, name = "middle-anim", start = 200, fps = 60)
49+
paparazzi.snapshot(view = view, offsetMillis = 1, name = "1ms")
50+
paparazzi.snapshot(view = view, offsetMillis = 100, name = "100ms")
51+
paparazzi.snapshot(view = view, offsetMillis = 200, name = "200ms")
52+
paparazzi.snapshot(view = view, offsetMillis = 300, name = "300ms")
53+
paparazzi.snapshot(view = view, offsetMillis = 400, name = "400ms")
54+
paparazzi.snapshot(view = view, offsetMillis = 500, name = "500ms")
55+
}
3856
}
3.77 KB
Loading
3.77 KB
Loading
4.86 KB
Loading
5.86 KB
Loading
5.75 KB
Loading
4.01 KB
Loading
78.8 KB
Loading

0 commit comments

Comments
 (0)