Animate change of view background color on Android

前端 未结 16 978
孤街浪徒
孤街浪徒 2020-11-22 13:23

How do you animate the change of background color of a view on Android?

For example:

I have a view with a red background color. The background color of the

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 14:05

    best way is to use ValueAnimator and ColorUtils.blendARGB

     ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
     valueAnimator.setDuration(325);
     valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
    
                  float fractionAnim = (float) valueAnimator.getAnimatedValue();
    
                  view.setBackgroundColor(ColorUtils.blendARGB(Color.parseColor("#FFFFFF")
                                        , Color.parseColor("#000000")
                                        , fractionAnim));
            }
    });
    valueAnimator.start();
    

提交回复
热议问题