animate View Background Color - Android User Interface

Android examples for User Interface:View Animation

Description

animate View Background Color

Demo Code


//package com.java2s;

import android.animation.ValueAnimator;
import android.graphics.Color;

import android.view.View;

public class Main {
    public static final long BACKGROUND_DURATION = 500;

    public static void animateBackgroundColor(final View view, int start,
            int end) {

        final float[] startHsv = new float[3];
        final float[] endHsv = new float[3];

        Color.colorToHSV(start, startHsv);
        Color.colorToHSV(end, endHsv);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
        valueAnimator.setDuration(BACKGROUND_DURATION);

        final float[] hsv = new float[3];

        valueAnimator//w w  w.  j  a  v  a  2 s.c  o m
                .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        hsv[0] = startHsv[0] + (endHsv[0] - startHsv[0])
                                * animation.getAnimatedFraction();
                        hsv[1] = startHsv[1] + (endHsv[1] - startHsv[1])
                                * animation.getAnimatedFraction();
                        hsv[2] = startHsv[2] + (endHsv[2] - startHsv[2])
                                * animation.getAnimatedFraction();

                        view.setBackgroundColor(Color.HSVToColor(hsv));
                    }
                });

        valueAnimator.start();
    }
}

Related Tutorials