Skip to content

Instantly share code, notes, and snippets.

@Abhimanyu14
Created July 8, 2023 17:59
Show Gist options
  • Save Abhimanyu14/88168237661acee2b6ba8fd72211c935 to your computer and use it in GitHub Desktop.
Save Abhimanyu14/88168237661acee2b6ba8fd72211c935 to your computer and use it in GitHub Desktop.
suspend fun changeFloatValueOverTime(
initialValue: Float,
targetValue: Float,
durationMillis: Int,
onUpdate: (Float) -> Unit,
) {
val startTime = System.currentTimeMillis()
while (System.currentTimeMillis() - startTime < durationMillis) {
val elapsedTime = System.currentTimeMillis() - startTime
val fraction = elapsedTime.toFloat() / durationMillis
val interpolatedValue = lerp(initialValue, targetValue, fraction)
onUpdate(interpolatedValue)
delay(16)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment