如何使同一资源中的多个可绘制对象唯一?

How do I make multiple drawables from the same resource unique?

在我的 Android 应用程序中,我试图以编程方式为一个可绘制资源创建多种颜色变化。 (我对拼字游戏中的每个字母都有一个独特的资源,并且在给定时间棋盘上随机有 X 个相同的字母块)

现在我的 Draw 方法对每个唯一资源调用相同的 ID:

int id = context.getResources().getIdentifier(this.tileLetter.toString(), "drawable", mypackagename);

Drawable t = context.getResources().getDrawable(id);
t.setAlpha(num);
t.draw(mycanvasname);

我的问题是,当我尝试在同一个 class 的不同对象上调用 setAlpha 时,更改显示在 canvas 上所有具有相同字母的图块上。 (再次,考虑尝试仅更改棋盘上一个独特的图块 'A' 的 alpha,而让其他 'A' 保持不变)。

更新 在 t.setAlpha(num) 之前调用 t.mutate() 就成功了。

如果您创建 Drawable 的克隆并根据需要修改每个克隆会怎样?请在此处查看 Flavio 的回答:Android: Cloning a drawable in order to make a StateListDrawable with filters