在一个我正在开发的应用程序中,我试图以编程方式创建一个ImageButton,它是所选ImageButton的副本,但是图像以不同的方式着色,比如说红色。

如果我使用PowerDuff.Mode.MULTIPLY:

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

然后,即使原始的ImageButton共享相同的drawable,其颜色也会变为红色。有没有一种方法可以只在clonebutton上应用过滤器,而不使用两个不同的drawables?例如,是否可以通过某种方式在不编辑drawable的情况下在clonebutton的顶部放置一个着色层?

更新
我将drawable设置为可变的:
Drawable d = swipebutton.getDrawable();
d.mutate();
d.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
swipebutton.setImageDrawable(d);

这样可以防止我的clonebutton将其drawable的状态共享给其他views

最佳答案

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

08-18 20:18