Created
September 19, 2019 11:12
-
-
Save robsn/bdd6221074ba393a84e4b1d65248f0e4 to your computer and use it in GitHub Desktop.
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
public final class DrawableCompat | |
{ | |
private DrawableCompat() | |
{ | |
} | |
/** | |
* Tints the given drawable with the given color. | |
* | |
* <br>For Android versions < Lollipop {@link Drawable#setColorFilter(int, PorterDuff.Mode)} will be used. | |
* <br>For Android versions >= Lollipop the drawables will be colored via {@link Drawable#setTint(int)} | |
* | |
* @param context The context. | |
* @param drawable The drawable to be tinted. | |
* @param colorResId The color resource to be applied. | |
*/ | |
@SuppressWarnings( "squid:CallToDeprecatedMethod" ) | |
public static void tintDrawableWithColorResId( final Context context, final Drawable drawable, @ColorRes final int colorResId ) | |
{ | |
final int tintColor = ContextCompat.getColor( context, colorResId ); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) | |
{ | |
drawable.setTint( tintColor ); | |
} | |
else | |
{ | |
drawable.setColorFilter( tintColor, SRC_ATOP ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment