Skip to content

Instantly share code, notes, and snippets.

@matinnasiri01
Last active September 22, 2023 10:50
Show Gist options
  • Save matinnasiri01/7ff5ef79c2c244404da80c895f2dcf70 to your computer and use it in GitHub Desktop.
Save matinnasiri01/7ff5ef79c2c244404da80c895f2dcf70 to your computer and use it in GitHub Desktop.
Add shimmer effect to your view without adding additional libraries.
/**
* Shimmer Effect Use Custom Code In Compose.
* @param[customColorList] You can set Your custom color for effect
*/
fun Modifier.shimmerEffect(customColorList: List<Color>? = null): Modifier = composed {
var size by remember {
mutableStateOf(IntSize.Zero)
}
val transition = rememberInfiniteTransition(label = "")
val startOffsetX by transition.animateFloat(
initialValue = -2 * size.width.toFloat(),
targetValue = 2 * size.width.toFloat(),
animationSpec = infiniteRepeatable(tween(durationMillis = 1000)),
label = ""
)
background(
brush = Brush.linearGradient(
colors = customColorList ?: listOf(
Color(0xFFB8B5B5),
Color(0xFF8F8B8B),
Color(0xFFB8B5B5)
),
start = Offset(startOffsetX, 0f),
end = Offset(startOffsetX + size.width.toFloat(), size.height.toFloat())
)
)
.onGloballyPositioned {
size = it.size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment