# Drawable的mutate方法——多ImageView.setAlpha失效问题解决

imageView.setImageResource(resId);
imageView.setAlpha(alpha);


/**

* 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

* 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 Drawable mutate() {

return this;

}


mutate方法让drawable可变的，保证了与其他drawable的状态隔离，该操作不可逆。解决方案如下：

Drawable drawable = getResources().getDrawable(resId);
imageView.setImageDrawable(drawable.mutate());
imageView.getDrawable().setAlpha(255*alpha);