gpt4 book ai didi

java - setColorFilter 有时在 android drawable 上不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:45:38 24 4
gpt4 key购买 nike

我正在尝试根据用户偏好中选择的原色在可绘制对象上应用滤色器。这是我正在使用的代码。

getResources().getDrawable(R.drawable.ic_batman_1)
.setColorFilter(ColorHelper.getPrimaryColor(), PorterDuff.Mode.OVERLAY);

问题是,有时,这段代码不会更改可绘制对象的颜色过滤器。我已将这段代码放在我的 Activity (主要 Activity )的 onCreate 和 onResume 方法中。

因此,一旦应用程序启动,我希望将此滤色器应用到该可绘制对象上,但有时它不会发生。我还注意到这个问题不会发生在高端手机(高速处理器、更多 RAM)上,而只会发生在低端手机上。

但是如果我浏览任何其他 Activity 并返回主要 Activity ,则会应用颜色过滤器。调试代码并在使用正确的颜色参数启动时调用 setColorFilter 但由于某种原因它没有被应用。感谢任何形式的帮助。

请不要对这个问题投反对票,如果你认为这是一个愚蠢的问题,请发表评论,我会把这个问题记下来。我正处于被禁止在 SO 上提问的边缘。

最佳答案

你试试 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;
}

关于java - setColorFilter 有时在 android drawable 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44984488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com