Last active
March 8, 2019 05:35
-
-
Save EmmanuelGuther/507f783b1cfd42f7d989 to your computer and use it in GitHub Desktop.
Change color of background view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment