animate View Color Change - Android User Interface

Android examples for User Interface:View Animation

Description

animate View Color Change

Demo Code


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

import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;

import android.view.View;

public class Main {
    public static final String COLOR_PROPERTY = "color";

    public static void animateColorChange(View view, int fromColor,
            int toColor, int duration, Animator.AnimatorListener listener) {
        if (view.getWindowToken() == null) {
            return;
        }//from w  w  w . j  av a2 s . c  o  m
        AnimatorSet animation = new AnimatorSet();
        ObjectAnimator colorAnimator = ObjectAnimator.ofInt(view,
                COLOR_PROPERTY, fromColor, toColor);
        colorAnimator.setEvaluator(new ArgbEvaluator());
        colorAnimator.setDuration(duration);
        if (listener != null)
            animation.addListener(listener);
        animation.play(colorAnimator);
        animation.start();
    }
}

Related Tutorials