Animate change of view background color on Android

前端 未结 16 969
孤街浪徒
孤街浪徒 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:20

    Answer is given in many ways. You can also use ofArgb(startColor,endColor) of ValueAnimator.

    for API > 21:

    int cyanColorBg = ContextCompat.getColor(this,R.color.cyan_bg);
    int purpleColorBg = ContextCompat.getColor(this,R.color.purple_bg);
    
    ValueAnimator valueAnimator = ValueAnimator.ofArgb(cyanColorBg,purpleColorBg);
            valueAnimator.setDuration(500);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                  @Override
                  public void onAnimationUpdate(ValueAnimator valueAnimator) {
                       relativeLayout.setBackgroundColor((Integer)valueAnimator.getAnimatedValue());
                  }
            });
            valueAnimator.start();
    

提交回复
热议问题