Animate change of view background color on Android

前端 未结 16 983
孤街浪徒
孤街浪徒 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条回答
  •  萌比男神i
    2020-11-22 14:23

    If you want color animation like this,

    this code will help you:

    ValueAnimator anim = ValueAnimator.ofFloat(0, 1);   
    anim.setDuration(2000);
    
    float[] hsv;
    int runColor;
    int hue = 0;
    hsv = new float[3]; // Transition color
    hsv[1] = 1;
    hsv[2] = 1;
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
    
            hsv[0] = 360 * animation.getAnimatedFraction();
    
            runColor = Color.HSVToColor(hsv);
            yourView.setBackgroundColor(runColor);
        }
    });
    
    anim.setRepeatCount(Animation.INFINITE);
    
    anim.start();
    

提交回复
热议问题