---
title: "Drawable对象的mutate()方法为什么不起作用？"
author: "LoveZhou"
date: "2011-04-01T23:11:25Z"
tags: ["Android", "OS"]
categories: ["移动开发"]
slug: "drawable-mutate"
draft: false
lastmod: "2011-04-01T23:11:25Z"
id: 983597
url: "https://lovezhou.iteye.com/blog/983597"
---

要在两个ImageView中显示用一张图片，而使它们的透明度不一样，利用Drawable对象的setAlpha()方法可以改变透明度，但是由于是同一张图片，底层只有一个Drawable对象，所以改变透明度的时候，所有显示的图片的透明度均会被更改，调用mutate()方法，可以使Drawable对象生成不同的constantstate对象，修改时就不会影响其它drawable对象的状态，但是调用后，不起作用，请会的朋友帮忙解答一下，先行谢过！
测试代码

```java
package com.zhou.activity;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class DrawableActivity extends Activity {
    ImageView myImageView;
    ImageView myImageView2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myImageView = (ImageView) this.findViewById(R.id.myImageView);
        myImageView2 = (ImageView) this.findViewById(R.id.myImageView2);
        
        //取得图片
        Resources res = this.getResources();
        Drawable drawable = res.getDrawable(R.drawable.link);
    
        //不透明
        drawable.mutate().setAlpha(255);
        myImageView.setImageDrawable(drawable);
        //透明
        drawable.mutate().setAlpha(55);
        myImageView2.setImageDrawable(drawable);
    }
} 
```