Created
November 17, 2024 10:44
-
-
Save itsSiddharthGupta/8606db51fff7f1db3ad1efbd0204c3de to your computer and use it in GitHub Desktop.
Shimmer Modifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Modifier.shimmerEffect() = composed { | |
val size = remember { mutableStateOf(IntSize(0, 0)) } | |
val transition = rememberInfiniteTransition() | |
val startOffsetX = transition.animateFloat( | |
initialValue = -2 * size.value.width.toFloat(), | |
targetValue = 2 * size.value.width.toFloat(), | |
animationSpec = infiniteRepeatable( | |
animation = tween(1000) | |
) | |
) | |
background( | |
brush = Brush.linearGradient( | |
colors = listOf( | |
Color(0xFFB8B5B5), | |
Color(0xFF8F8B8B), | |
Color(0xFFB8B5B5), | |
), | |
start = Offset(startOffsetX.value, 0f), | |
end = Offset(startOffsetX.value + size.value.width.toFloat(), size.value.height.toFloat()) | |
) | |
).onGloballyPositioned { | |
size.value = it.size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment