Created
February 7, 2018 11:15
-
-
Save AlexMeuer/0f58c34d52854cfbb91ef252f999b7f7 to your computer and use it in GitHub Desktop.
Utility class for performing common color animations on Views. (Use of `MoreObjects` can be replaced with a turnary if you really hate Guava.)
This file contains 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
package foo.bar.baz; | |
import android.animation.ArgbEvaluator; | |
import android.animation.ObjectAnimator; | |
import android.animation.TimeInterpolator; | |
import android.animation.ValueAnimator; | |
import android.graphics.ColorMatrix; | |
import android.graphics.ColorMatrixColorFilter; | |
import android.support.annotation.ColorInt; | |
import android.support.annotation.FloatRange; | |
import android.support.annotation.NonNull; | |
import android.view.View; | |
import android.view.ViewPropertyAnimator; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.google.common.base.MoreObjects; | |
public final class ViewUtil { | |
private static ColorMatrixColorFilter greyscaleFilter; | |
private ViewUtil() { /* Instantiation is not allowed. */ } | |
/** | |
* Changes the transparency of a view over time. | |
* If setting to min opacity, sets visibility to GONE at the end, otherwise sets visibility to | |
* VISIBLE at the start. | |
* @param view The view to animate. | |
* @param alpha The new alpha value. | |
* @return The animation that was created. | |
*/ | |
@NonNull | |
public static ViewPropertyAnimator animateAlpha(@NonNull final View view, @FloatRange(from=0.0f, to=1.0f) float alpha, | |
long duration, @NonNull TimeInterpolator interpolator) { | |
final ViewPropertyAnimator animator = view.animate() | |
.alpha(alpha) | |
.setDuration(duration) | |
.setInterpolator(interpolator); | |
if (alpha <= 0.f) { | |
animator.withEndAction(new Runnable() { | |
@Override public void run() { | |
view.setVisibility(View.GONE); | |
} | |
}); | |
} else { | |
animator.withStartAction(new Runnable() { | |
@Override public void run() { | |
view.setVisibility(View.VISIBLE); | |
} | |
}); | |
} | |
return animator; | |
} | |
public static void animateTextViewColor(@NonNull TextView view, @ColorInt int newColor, | |
long duration, @NonNull TimeInterpolator interpolator) { | |
final @ColorInt int oldColor = view.getCurrentTextColor(); | |
final ObjectAnimator animator = ObjectAnimator.ofInt(view, "textColor", | |
oldColor, newColor); | |
animator.setEvaluator(new ArgbEvaluator()); | |
animator.setDuration(duration); | |
animator.setInterpolator(interpolator); | |
animator.start(); | |
} | |
public static void animateImageViewColor(@NonNull final ImageView view, @ColorInt int newColor, | |
long duration, @NonNull TimeInterpolator interpolator) { | |
final ValueAnimator animator = ValueAnimator.ofObject( | |
new ArgbEvaluator(), | |
MoreObjects.firstNonNull(view.getTag(), 0xFFFFFFFF), // Default to white if no tag set | |
newColor | |
); | |
animator.setDuration(duration); | |
animator.setInterpolator(interpolator); | |
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator animation) { | |
final int newColor = (int) animation.getAnimatedValue(); | |
view.setColorFilter(newColor); | |
view.setTag(newColor); | |
} | |
}); | |
animator.start(); | |
} | |
public static synchronized ColorMatrixColorFilter getGrayscaleColorFilter() { | |
if (null == greyscaleFilter) { | |
final ColorMatrix matrix = new ColorMatrix(); | |
matrix.setSaturation(0); | |
greyscaleFilter = new ColorMatrixColorFilter(matrix); | |
} | |
return greyscaleFilter; | |
} | |
/** | |
* Disables an image view and applies a monochrome color filter to it. | |
* @param view The view to disable. | |
*/ | |
public static void disableAndGrayOut(@NonNull ImageView view) { | |
view.setEnabled(false); | |
view.setColorFilter(ViewUtil.getGrayscaleColorFilter()); | |
} | |
/** | |
* Enables an image view and clears any color filter applied to it. | |
* @param view The view to enable. | |
*/ | |
public static void enableAndClearFilter(@NonNull ImageView view) { | |
view.setEnabled(true); | |
view.clearColorFilter(); | |
} | |
public static void setGrayAndDisable(@NonNull ImageView view, boolean disabled) { | |
if (disabled) { | |
disableAndGrayOut(view); | |
} else { | |
enableAndClearFilter(view); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment