Last active
September 6, 2024 20:11
-
-
Save Merkost/a9fb9ce32b659ab74f358a617f57ea53 to your computer and use it in GitHub Desktop.
Spotify-Inspired Audio Buffering Slider with Jetpack Compose
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.pulsatingEffect( | |
currentValue: Float, | |
isVisible: Boolean, | |
color: Color = Color.Gray, | |
): Modifier = composed { | |
var trackWidth by remember { mutableFloatStateOf(0f) } | |
val thumbX by remember(currentValue) { | |
mutableFloatStateOf(trackWidth * currentValue) | |
} | |
val transition = rememberInfiniteTransition(label = "trackAnimation") | |
val animationProgress by transition.animateFloat( | |
initialValue = 0f, | |
targetValue = 1f, | |
animationSpec = infiniteRepeatable( | |
animation = tween( | |
durationMillis = 800, | |
delayMillis = 200, | |
) | |
), label = "width" | |
) | |
this then Modifier | |
.onGloballyPositioned { coordinates -> | |
trackWidth = coordinates.size.width.toFloat() | |
} | |
.drawWithContent { | |
drawContent() | |
val strokeWidth = size.height | |
val y = size.height / 2f | |
val startOffset = thumbX | |
val endOffset = thumbX + animationProgress * (trackWidth - thumbX) | |
val dynamicAlpha = (1f - animationProgress).coerceIn(0f, 1f) | |
if (isVisible) { | |
drawLine( | |
color = color.copy(alpha = dynamicAlpha), | |
start = Offset(startOffset, y), | |
end = Offset(endOffset, y), | |
cap = StrokeCap.Round, | |
strokeWidth = strokeWidth | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment