/**
* Tries to clone and simply color-filter the drawable. Uses {@link PorterDuff.Mode#SRC_ATOP}.
* <b>Note</b>: Use this when you don't know which drawable you have.
*
* @param drawable Which drawable to color
* @param color Which color to use
* @return A colored drawable ready for use
*/
@NonNull
public static Drawable colorUnknownDrawable(@NonNull final Drawable drawable, @ColorInt final int color) {
// check if this is a drawable wrapper, then do coloring by drawable wrapping
if (drawable instanceof DrawableWrapper || drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
final Drawable wrapResult = colorDrawableWrapped(drawable, color);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {
// there is a bug for JellyBean MR2 when this won't work, so.. set the tint filter manually
wrapResult.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
}
return wrapResult;
}
// wrapping failed, do a plain constant state clone
try {
final Drawable.ConstantState state = drawable.getConstantState();
if (state == null) {
// well done android.
throw new IllegalStateException("Constant state is unavailable");
}
final Drawable copy = drawable.getConstantState().newDrawable().mutate();
copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
return copy;
} catch (Exception ignored) {
return drawable;
}
}
Coloring.java 文件源码
java
阅读 33
收藏 0
点赞 0
评论 0
项目:silly-android
作者:
评论列表
文章目录