animate Background to a specified Color - Android android.view.animation

Android examples for android.view.animation:Color Animation

Description

animate Background to a specified Color

Demo Code

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

public class Main {

  public static void animBackgroundColor(final View view, int toColor) {

    int beforeColor = Color.TRANSPARENT;
    Drawable background = view.getBackground();
    if (background instanceof ColorDrawable)
      beforeColor = ((ColorDrawable) background).getColor();

    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), beforeColor, toColor);
    colorAnimation.setDuration(500);//from w w w  .  j a v  a 2 s.co  m
    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animator) {
        int color = (Integer) animator.getAnimatedValue();
        view.setBackgroundColor(color);
      }
    });
    colorAnimation.start();
  }

}

Related Tutorials