morristech
3/8/2019 - 5:35 AM

Change color of background view

Change color of background view

/*
 * Copyright (c) 2016. Emmanuel Gutierrez Hernandez
 *                      Licensed under the Apache License, Version 2.0 (the "License");
 *                      you may not use this file except in compliance with the License.
 *                      You may obtain a copy of the License at
 *
 *                            http://www.apache.org/licenses/LICENSE-2.0
 *
 *                       Unless required by applicable law or agreed to in writing, software
 *                       distributed under the License is distributed on an "AS IS" BASIS,
 *                       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *                       See the License for the specific language governing permissions and
 *                       limitations under the License
 *                       github.com/EmmanuelGuther                                        twitter.com/EmmanuelGuther
 */

package com.emmanuelguther.me.utils;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.view.View;

public class ViewChangeColor implements Animator.AnimatorListener{
    View view;
    int colorFrom;
    int colorTo;
    int duration;


    public ViewChangeColor(View view, int colorFrom, int colorTo, int duration){
        this.view = view;
        this.colorFrom = colorFrom;
        this.colorTo = colorTo;
        this.duration = duration;
    }

    public void backgroundColorAnimation() {
        final float[] from = new float[3],
                to = new float[3];

        Color.colorToHSV(colorFrom, from);   // from white
        Color.colorToHSV(colorTo, to);     // to red

        ValueAnimator anim = ValueAnimator.ofFloat(0, 1);   // animate from 0 to 1

        anim.setDuration(duration);

        final float[] hsv = new float[3];                  // transition color
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // Transition along each axis of HSV (hue, saturation, value)
                hsv[0] = from[0] + (to[0] - from[0]) * animation.getAnimatedFraction();
                hsv[1] = from[1] + (to[1] - from[1]) * animation.getAnimatedFraction();
                hsv[2] = from[2] + (to[2] - from[2]) * animation.getAnimatedFraction();

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

        anim.start();

    }
    private void backgroundColorAnimationReverse() {
        final float[] from = new float[3],
                to = new float[3];

        Color.colorToHSV(colorFrom, to);   // from white
        Color.colorToHSV(colorTo, from);     // to red

        ValueAnimator anim = ValueAnimator.ofFloat(0, 1);   // animate from 0 to 1

        anim.setDuration(duration);

        final float[] hsv = new float[3];                  // transition color
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // Transition along each axis of HSV (hue, saturation, value)
                hsv[0] = from[0] + (to[0] - from[0]) * animation.getAnimatedFraction();
                hsv[1] = from[1] + (to[1] - from[1]) * animation.getAnimatedFraction();
                hsv[2] = from[2] + (to[2] - from[2]) * animation.getAnimatedFraction();

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

        anim.start();
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
       
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
}