我正在尝试根据用户在偏好中选择的原色在可绘制对象上应用滤色器。这是我正在使用的一段代码。
getResources().getDrawable(R.drawable.ic_batman_1)
.setColorFilter(ColorHelper.getPrimaryColor(), PorterDuff.Mode.OVERLAY);
问题是,有时,这段代码不会改变可绘制的颜色过滤器。我已将此代码放置在我的活动(主活动)onCreate 和 onResume 方法中。
因此,一旦应用程序启动,我希望将此颜色滤镜应用到该可绘制对象上,但有时不会发生。我还注意到这个问题不会发生在高端手机(高速处理器,更多内存)上,而只会发生在低端手机上。
但是,如果我浏览任何其他活动并返回主要活动,则会应用滤色器。调试了代码,在使用正确的颜色参数启动时调用了 setColorFilter,但由于某种原因它没有被应用。任何形式的帮助表示赞赏。
请不要对这个问题投反对票,如果您认为这是愚蠢的问题,请发表评论,我会删除该问题。我正处于因提问而被禁止的边缘。
你尝试 Drawable.mutate();像这样的财产,
Drawable drawable = ContextCompat.getDrawable(context, resource).mutate();
drawable.setColorFilter(ColorHelper.getPrimaryColor(), PorterDuff.Mode. OVERLAY);
/**
* Make this drawable mutable. This operation cannot be reversed. A mutable
* drawable is guaranteed to not share its state with any other drawable.
* This is especially useful when you need to modify properties of drawables
* loaded from resources. By default, all drawables instances loaded from
* the same resource share a common state; if you modify the state of one
* instance, all the other instances will receive the same modification.
*
* Calling this method on a mutable Drawable will have no effect.
*
* @return This drawable.
* @see ConstantState
* @see #getConstantState()
*/
public @NonNull Drawable mutate() {
return this;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)