I need to animate an angle value (0°-360°), and I ...
# compose
m
I need to animate an angle value (0°-360°), and I initially tried to use
animateFloatAsState()
. The problem is that as my target angle moves past 360° back into 0°, or decreases past 0° into 360°, the animation jumps all the way around. I need a change from 358° to 2° to animate through just 4 values, not go all the way back through 356 values. Is there a way to make the animation aware of the circular nature of my numbers?
t
How about 358 - 362?
r
How did you use
animateFloatAsState()
?
m
val animatedBearing by animateFloatAsState(targetValue = bearing)
This works fine except for the jumps at 0°/360°
The input value will always be in the range [0-360)
r
Do you compute that input?
Alternatively you can compute the difference between the two inputs and compare it to the modulo of the sum and pick the target that yields the smallest result
m
That seems reasonable, thanks I will try that.