Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FloatingActionButton backgroundTint animation

Tags:

android

I'd like to animate the backgroundTint value (and ideally the alpha value too) of a FloatingActionButton, so that the FAB background color continuously switches between two colors.

My noob approach would be to use a timer that calls a function that updates this property when it fires. I'm sure there's a better way of doing this?

like image 875
user1889776 Avatar asked Dec 15 '22 08:12

user1889776


1 Answers

I got this to work using ObjectAnimator as suggested by @MH. above, but I had to override the onAnimationUpdate() callback:

final ValueAnimator animator = ValueAnimator.ofInt(Color.rgb(0, 121, 107), Color.rgb(226, 143, 34));
animator.setDuration(2000L);
animator.setEvaluator(new ArgbEvaluator());
animator.setInterpolator(new DecelerateInterpolator(2));
animator.addUpdateListener(new ObjectAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int animatedValue = (int) animation.getAnimatedValue();
        fab.setBackgroundTintList(ColorStateList.valueOf(animatedValue));
    }
});
animator.start();
like image 132
user1889776 Avatar answered Jan 08 '23 06:01

user1889776