Skip to content

Instantly share code, notes, and snippets.

@ahmed3elshaer
Last active February 25, 2024 08:59
Show Gist options
  • Save ahmed3elshaer/92aca37086a56f20e540e1b183c738d7 to your computer and use it in GitHub Desktop.
Save ahmed3elshaer/92aca37086a56f20e540e1b183c738d7 to your computer and use it in GitHub Desktop.
Loading Animation like Gemini in Jetpack Compose
fun Modifier.animatedGradient(
primaryColor: Color,
containerColor: Color
): Modifier = composed {
var size by remember { mutableStateOf(IntSize.Zero) }
val transition = rememberInfiniteTransition(label = "")
val colors = listOf(
primaryColor,
containerColor,
primaryColor
)
val offsetXAnimation by transition.animateFloat(
initialValue = -size.width.toFloat(),
targetValue = size.width.toFloat(),
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 1500, easing = LinearEasing),
repeatMode = RepeatMode.Restart
), label = "gradientAnimation"
)
background(
brush = Brush.linearGradient(
colors = colors,
start = Offset(x = offsetXAnimation, y = 0f),
end = Offset(x = offsetXAnimation + size.width.toFloat(), y = size.height.toFloat())
),
shape = RoundedCornerShape(24.dp)
)
.onGloballyPositioned {
size = it.size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment