Skip to content

Commit e97df7d

Browse files
Refactor animateFloatAsState example (#484)
Refactor the animateFloatAsState example to use the lambda-based `graphicsLayer` modifier, preventing continuous recomposition during animation.
1 parent c5c0b45 commit e97df7d

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,15 @@ private fun AnimateAsStateSimple() {
280280
// [START android_compose_animations_animate_as_state]
281281
var enabled by remember { mutableStateOf(true) }
282282

283-
val alpha: Float by animateFloatAsState(if (enabled) 1f else 0.5f, label = "alpha")
283+
val animatedAlpha: Float by animateFloatAsState(if (enabled) 1f else 0.5f, label = "alpha")
284284
Box(
285285
Modifier
286286
.fillMaxSize()
287-
.graphicsLayer(alpha = alpha)
287+
.graphicsLayer { alpha = animatedAlpha }
288288
.background(Color.Red)
289289
)
290290
// [END android_compose_animations_animate_as_state]
291+
{ Button(onClick = { enabled = !enabled }) { Text("Animate me!") } }
291292
}
292293

293294
@Preview

0 commit comments

Comments
 (0)