anastr / SpeedView

Dynamic Speedometer and Gauge for Android. amazing, powerful, and multi shape :zap:
Apache License 2.0
1.28k stars 321 forks source link

Speedview animation not working with float, but works with int. #242

Closed SgtPrinsen closed 1 year ago

SgtPrinsen commented 1 year ago

I feel like I'm doing something silly but I can't figure it out. When I use int, this code works fine and will animate the speedview between the different values with a one second interval. When I use float, the speedview sits at 0 and then at 5 seconds, it it will animate to the last value in the array (in the below example, 5f). Any idea?

This works

int[] intArray = {10,20,15,25,5};
final ValueAnimator anim = ValueAnimator.ofInt(intArray);
anim.addUpdateListener(animation -> {
int value = (int) anim.getAnimatedValue();
speedView.speedTo(value, 1000L);
});
anim.setDuration(5000).start();

This doesn't

float[] floatArray = {10f, 20f, 15f, 25f, 5f}; final ValueAnimator anim = ValueAnimator.ofFloat(floatArray); anim.addUpdateListener(animation -> { float value = (float) anim.getAnimatedValue()); speedView.speedTo(value, 1000L); }); anim.setDuration(5000).start();

anastr commented 1 year ago

Hi @SgtPrinsen,

When you're using Int, the ValueAnimator has only 5 values to jump into. While in float it has an unlimited number of values. And in both cases, you're trying to set a new speed value with animation. Which means (in the float animator) you will start a new animation for every millisecond.

You can try to set the speed value to the speedometer immediately without animation by using the setSpeedAt function to see I mean.

SgtPrinsen commented 1 year ago

Thanks for the quick response! What do you mean by float has an unlimited number of values? In my float array I only gave it the 5 float values, same as int. So with five values in the array and the animator duration set to 5000ms, I'd expect the ValueAnimator to start an animation every 1000ms.

anastr commented 1 year ago

For a float animator, you will get as a value while the animation is running a lot of decimal numbers between each 2 values from your array. For example if your array is something like {10f, 20f, 15f, 25f, 5f} and you start the animator with a 5 seconds duration, for the first second the values you will get is something like [10.01, 10.03, 10.12, ..., 20] and so on.

SgtPrinsen commented 1 year ago

Sorry I'm having a tough time understanding, but even if it has a lot of values over that second [10.01 -> 20] as long as it ends at 20, why doesn't the speedView move to 20 at the end of a second?

anastr commented 1 year ago

As I mentioned previously, the speedTo function also starts running an animator. Try to use the setSpeedAt function instead.

SgtPrinsen commented 1 year ago

setSpeedAt works! Thanks for your help on this!