Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable.setColorFilter() not working on Android 2.1

Tags:

android

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

On Android 2.2 (emulator) and 2.3 (N1) setColorFilter() works fine. Why doesn't it work on 2.1 (tested on emulator)? Another Android bug?

like image 783
fhucho Avatar asked Mar 31 '11 12:03

fhucho


1 Answers

You need to make your Bitmap mutable.

// 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);

You can see this problem cropping up over here, too.

like image 57
kanzure Avatar answered Nov 03 '22 03:11

kanzure