public static Drawable getTintDrawableByColor2( Context context, @DrawableRes int drawableResId, @ColorInt int color) { Drawable drawable = AndroidUtils.getDrawable(context, drawableResId); if (DrawableUtils.canSafelyMutateDrawable(drawable)) { drawable = drawable.mutate(); } PorterDuffColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); drawable.setColorFilter(filter); return drawable; }
public static Drawable getTintDrawableByColor( Context context, @DrawableRes int drawableResId, @ColorInt int color) { Drawable drawable = AndroidUtils.getDrawable(context, drawableResId); if (DrawableUtils.canSafelyMutateDrawable(drawable)) { drawable = drawable.mutate(); } Drawable drawableCompat = DrawableCompat.wrap(drawable); DrawableCompat.setTint(drawableCompat, color); return drawableCompat; }
public static boolean tintDrawableUsingColorFilter(@NonNull Context context, @DrawableRes final int resId, @NonNull Drawable drawable) { PorterDuff.Mode tintMode = DEFAULT_MODE; boolean colorAttrSet = false; int colorAttr = 0; int alpha = -1; if (arrayContains(COLORFILTER_TINT_COLOR_CONTROL_NORMAL, resId)) { colorAttr = R.attr.colorControlNormal; colorAttrSet = true; } else if (arrayContains(COLORFILTER_COLOR_CONTROL_ACTIVATED, resId)) { colorAttr = R.attr.colorControlActivated; colorAttrSet = true; } else if (arrayContains(COLORFILTER_COLOR_BACKGROUND_MULTIPLY, resId)) { colorAttr = android.R.attr.colorBackground; colorAttrSet = true; tintMode = PorterDuff.Mode.MULTIPLY; } else if (resId == R.drawable.abc_list_divider_mtrl_alpha) { colorAttr = android.R.attr.colorForeground; colorAttrSet = true; alpha = Math.round(0.16f * 255); } if (colorAttrSet) { if (DrawableUtils.canSafelyMutateDrawable(drawable)) { drawable = drawable.mutate(); } final int color = getThemeAttrColor(context, colorAttr); drawable.setColorFilter(getPorterDuffColorFilter(color, tintMode)); if (alpha != -1) { drawable.setAlpha(alpha); } return true; } return false; }