加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 暂时改变可绘制的颜色

发布时间:2020-12-15 02:52:06 所属栏目:Java 来源:网络整理
导读:在我开发的一个应用程序中,我正在尝试以编程方式创建一个 ImageButton,它是所选ImageButton的副本,但图像以不同的方式着色,让我们说是红色. 如果我使用PowerDuff.Mode.MULTIPLY: clonebutton.getDrawable().setColorFilter(0xFFFF0000,Mode.MULTIPLY); 然后
在我开发的一个应用程序中,我正在尝试以编程方式创建一个 ImageButton,它是所选ImageButton的副本,但图像以不同的方式着色,让我们说是红色.

如果我使用PowerDuff.Mode.MULTIPLY:

clonebutton.getDrawable().setColorFilter(0xFFFF0000,Mode.MULTIPLY);

然后,即使原始的ImageButton将其颜色更改为红色,因为它们共享相同的drawable.有没有办法只在克隆按钮上应用过滤器而不使用两个不同的drawable?例如,是否可以在某种程度上将colorize图层放在clonebutton的顶部而不编辑drawable?

更新
我将drawable设置为可变:

Drawable d = swipebutton.getDrawable();
d.mutate();
d.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
swipebutton.setImageDrawable(d);

这可以防止我的clonebutton将其drawable的状态共享给其他视图.

解决方法

Drawable buttonBackground = clonebutton.getDrawable();
buttonBackground = buttonBackground.mutate();
buttonBackground.setColorFilter(0xFFFF0000,Mode.MULTIPLY);

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读