Drawable对象的mutate()方法为什么不起作用?

本文介绍如何使用Drawable对象的setAlpha()方法调整ImageView中图片的透明度,并通过mutate()方法实现不同透明度的图片显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值