animate View Color - Android User Interface

Android examples for User Interface:View Animation

Description

animate View Color

Demo Code


//package com.java2s;
import android.animation.ArgbEvaluator;

import android.animation.ValueAnimator;

import android.graphics.drawable.ColorDrawable;

import android.view.View;

public class Main {
    private static final int ANIM_DORITION = 400;

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

        ColorDrawable colorDrawable = (ColorDrawable) view.getBackground();
        int from = (colorDrawable == null) ? 0xffffff : colorDrawable
                .getColor();//from  w ww . ja  va  2  s. co m
        ValueAnimator anim = new ValueAnimator();
        anim.setIntValues(from, toColor);
        anim.setEvaluator(new ArgbEvaluator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                view.invalidate();
                view.setBackgroundColor((Integer) valueAnimator
                        .getAnimatedValue());
            }
        });
        anim.setDuration(ANIM_DORITION).start();
    }
}

Related Tutorials