Last active
September 22, 2023 10:50
-
-
Save matinnasiri01/7ff5ef79c2c244404da80c895f2dcf70 to your computer and use it in GitHub Desktop.
Add shimmer effect to your view without adding additional libraries.
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
/** | |
* 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