android: getBackground.setAlpha() 更改具有相同背景图像的所有活动的 alpha

android: getBackground.setAlpha() changes alpha of all activities with same background-image

例如,如果我将以下行添加到我的第二个 activity,背景图像的 alpha 值也会在我的第一个 activity 中改变,因为我使用了相同的图像。

findViewById(R.id.main).getBackground().setAlpha(100);

但我不想这样。这两个活动应具有不同的 alpha 值,但仍具有与背景图像相同的可绘制对象。我怎样才能做到这一点?

您应该在设置 alpha 之前调用 mutate() 创建一个单独的实例。根据文档:

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.