Skip to content

Instantly share code, notes, and snippets.

@Merkost
Last active April 5, 2024 00:53
Show Gist options
  • Save Merkost/a9fb9ce32b659ab74f358a617f57ea53 to your computer and use it in GitHub Desktop.
Save Merkost/a9fb9ce32b659ab74f358a617f57ea53 to your computer and use it in GitHub Desktop.
Spotify-Inspired Audio Buffering Slider with Jetpack Compose
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