Skip to content

Instantly share code, notes, and snippets.

@markus2610
Forked from adamp/AnimationClocks.kt
Created November 8, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markus2610/fe9414bfebe236516bc26d9303df88dd to your computer and use it in GitHub Desktop.
Save markus2610/fe9414bfebe236516bc26d9303df88dd to your computer and use it in GitHub Desktop.
Working with animation frame time in Jetpack Compose
/**
* Returns a [State] holding a local animation time in milliseconds.
* The value always starts at `0L` and stops updating when
* the call leaves the composition.
*/
@Composable
fun animationTimeMillis(): State<Long> {
val millisState = state { 0L }
val lifecycleOwner = LifecycleOwnerAmbient.current
launchInComposition {
val startTime = awaitFrameMillis { it }
lifecycleOwner.whenStarted {
while (true) {
awaitFrameMillis { frameTime ->
millisState.value = frameTime - startTime
}
}
}
}
return millisState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment