为什么在所有地方都应用可绘制的彩色滤光片? [英] Why drawable color filter is applied in all places?
本文介绍了为什么在所有地方都应用可绘制的彩色滤光片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序的一部分中,我需要将可绘制的R.drawable.blah
过滤为白色(最初是红色),所以我有以下方法:
In a section of my app I need my drawable R.drawable.blah
to be filtered to white color (originally is red), so I have this method:
public final static Drawable getFilteredDrawable(Context context, @DrawableRes int drawable, @ColorRes int color) {
Drawable d = ContextCompat.getDrawable(context, drawable);
d.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN);
return d;
}
我以这种方式使用它:
DrawableUtil.getFilteredDrawable(this, R.drawable.blah, android.R.color.white);
问题在于,现在可绘制对象在整个应用中变为白色,即使不应用过滤器也是如此.我希望可绘制对象仅在应用程序的这一部分是白色的,但是它在我使用它的每个位置.
Problem is that now the drawable becomes white in the whole app, even not applying the filter. I want the drawable to be white just in this section of the app, but it is in each place I use it, instead.
我该如何解决?
推荐答案
请改用此方法,以确保您使用的是可绘制对象的副本
use this method instead, to be sure you're using a copy of your drawable
public final static Drawable getFilteredDrawable(Context context, @DrawableRes int drawable, @ColorRes int color) {
Drawable d = ContextCompat.getDrawable(context, drawable).getConstantState().newDrawable().mutate(); //so we are sure we are using a copy of the original drawable
d.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN);
return d;
}
这篇关于为什么在所有地方都应用可绘制的彩色滤光片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文