Drawable.setColorFilter()在Android 2.1上无法工作

5
Drawable d = new BitmapDrawable(BitmapFactory.decodeResource(
    getResources(), R.drawable.ic_watch));
d.setColorFilter(new LightingColorFilter(color, lightenColor));
imageView.setImageDrawable(d);

在Android 2.2(模拟器)和2.3(N1)上,setColorFilter()正常工作。为什么在2.1上(在模拟器上测试)它不起作用?是另一个Android的错误吗?
1个回答

9

您需要将 Bitmap 变为可变的。

// make a mutable Bitmap
Bitmap immutableBitmap = BitmapFactory.decodeResource(getResources(), 
    R.drawable.ic_watch);
Bitmap mutableBitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);

// you have two bitmaps in memory, so clean up the mess a bit
immutableBitmap.recycle(); immutableBitmap=null;

Drawable d = new BitmapDrawable(mutableBitmap);

// mutate it
d.setColorFilter(new LightingColorFilter(color, lightenColor));

imageView.setImageDrawable(d);

你可以在这里看到这个问题的出现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接