Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save delacrixmorgan/26a78c04e7b0bfd9265f58ff7863b362 to your computer and use it in GitHub Desktop.
Save delacrixmorgan/26a78c04e7b0bfd9265f58ff7863b362 to your computer and use it in GitHub Desktop.
Compose Animate Gradient Vertically
val infiniteTransition = rememberInfiniteTransition(label = "background")
val targetOffset = with(LocalDensity.current) {
    1_000.dp.toPx()
}
val offset by infiniteTransition.animateFloat(
    initialValue = 0F,
    targetValue = targetOffset,
    animationSpec = infiniteRepeatable(
        tween(50_000, easing = LinearEasing),
        repeatMode = RepeatMode.Reverse,
    ),
    label = "offset",
)
    
Spacer(
    modifier = modifier
        .height(280.dp)
        .fillMaxWidth()
        .drawWithCache {
            val brush = Brush.linearGradient(
                colors = listOf(
                    Color(0xFFFFC1F7),
                    Color(0xFFFFD471),
                ),
                start = Offset(offset, offset),
                end = Offset(offset, offset + size.height),
                tileMode = TileMode.Mirror,
            )
            onDrawBehind {
                drawRect(brush)
            }
        },
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment