In my example I am moving some game pieces around on a board. Sometimes the pieces are moving long distances and sometimes the pieces are moving short distances.
I am achieving the animation using animateOffsetAsState()
and pass in my new offset like so:
val offset by animateOffsetAsState(
targetValue = position.toOffset(squareSize),
animationSpec = tween(200, easing = LinearEasing)
)
Each animation runs for 200 ms. So for short distances the piece moves really slowly and for long distances the piece moves really quick to cover the distance.
Is there some built in animation spec that I can use to define a velocity that I want to use rather than a time so that, regardless of distance, the animation is always running at the same velocity?
Since velocity is t = x/v you can calculate which duration to pass to tween with LinearEasing as
and create a state with constant speed
Red circles use animateAsSate while green circle uses constant speed
Demo
Another Demo