If I wanted to infinity animate a ball bouncy arou...
# compose-android
j
If I wanted to infinity animate a ball bouncy around a full screen composable? What animation API should I use? I've taken a look at target based and decay based animations and I don't believe they are suitable for me. Fundamentally what I want is a game loop where I manually update the x y position of my ball based on velocity, wind and gravity (adjusting velocity when screen edges are hit). I suspect what I want could be achieved with target based animations and mutable properties but this feels more of an abuse of the API. Any recommendations?
z
If you actually want to run a physics simulation, the best approach is to use withInfiniteAnimationFrameMillis in an infinite loop. Note that you’ll want to do all your state updates in the
onFrame
callback so they are applied to the current frame. The function will only resume the coroutine after the composition/layout/draw phases for that frame.
j
I was unfamiliar with this API. Are there any examples using this function?
z
It’s basically the same as withFrameMillis, but “infinite” animations are not considered in tests when waiting for idle
but in terms of writing animation code, they’re interchangeable
this looks like a pretty good introduction to it
It’s a bit old, but the core concepts are there.
LifecycleOwnerAmbient
is now called
LocalLifecycleOwner
.
j
This is exactly what I needed. Thanks
👍🏻 1