Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the frame rate for PropertyAnimation?

The Android documentation says you can set a refresh rate:

Frame refresh delay: You can specify how often to refresh frames of your animation. The default is set to refresh every 10 ms, but the speed in which your application can refresh frames is ultimately dependent on how busy the system is overall and how fast the system can service the underlying timer.

However, it doesn't tell you how. I've searched everywhere, but there is no such method in ObjectAnimator, PropertyAnimator, or Animator.

Edit: I'm currently using an animation updater which only reacts once every 5 frames -

colorFade.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
       int interpolator = 0;

       @Override
       public void onAnimationUpdate(ValueAnimator animation) {
           if ((interpolator++) % 5 == 0)
                invalidate(mHandlerBounds);
        }
  });
like image 220
Yervant Avatar asked Aug 17 '15 16:08

Yervant


2 Answers

As @pskink says, use the:

public static void setFrameDelay(long frameDelay)

of the ValueAnimator class. You can see this in the doc:

The amount of time, in milliseconds, between each frame of the animation. This is a requested time that the animation will attempt to honor, but the actual delay between frames may be different, depending on system load and capabilities. This is a static function because the same delay will be applied to all animations, since they are all run off of a single timing loop. The frame delay may be ignored when the animation system uses an external timing source, such as the display refresh rate (vsync), to govern animations.

like image 104
Alex Sifuentes Avatar answered Sep 22 '22 02:09

Alex Sifuentes


You can implement the TimeInterpolator interface to create a custom Interpolator, map the elapsed fraction to a new fraction that represent the frame rate. For example, I want to rotate a loading image view enter image description here to show loading indication enter image description here to user:

ObjectAnimator animator = ObjectAnimator.ofFloat(loadingView, "rotation", 360f);
animator.setDuration(600);
animator.setInterpolator(new TimeInterpolator() {
    @Override
    public float getInterpolation(float input) {
        return (int)(input * 12) / 12.0f;
    }
});
animator.start();

In that example, loadingView will be rotated in 360 degrees in 600ms. The default frame rate is (1000ms / 10ms). If I don't set the custom Interpolator, loadingView will rotate very fast that lead to loadingView become a circle not a indication. I need to make the frame rate equal to loading image's "leap", that is 12.

I map the animation duration to 12 parts, the elapsed fraction received will be mapped into the part contains the fraction.

So, just change the 12 to what you want the frame rate should be in your code, the final frame rate will be achieved.

like image 43
zwh Avatar answered Sep 18 '22 02:09

zwh