Rafael
Rafael

Reputation: 6709

Grayscale PictureDrawable

To grayscale a drawable I do:

ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(200);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
drawable.setColorFilter(filter);

as Justin said. It works fine.

I'm using svg-android library. So I'm trying:

PictureDrawable pictureDrawable = svg.createPictureDrawable();
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(200);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
pictureDrawable.setColorFilter(filter);
imageView.setImageDrawable(pictureDrawable);

But nothing happens... What is wrong?

Upvotes: 2

Views: 613

Answers (2)

Simon Meyer
Simon Meyer

Reputation: 2044

I just found this page by googling. In case anyone is still interested i'm using some workaround for the same problem:

Picture pic = svg.getPicture();
Bitmap bm = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(bm);
c.drawPicture(pic, new Rect(0,0,pic.getWidth(), pic.getHeight()));
c.save();
BitmapDrawable drawable = new BitmapDrawable(getResources(), bm);
drawable.setColorFilter(getBlackAndWhiteFilter());
imageView.setImageDrawable(drawable);

instead of using pic.getWidth() and pic.getWidth() you can of course use your preferred imagesize - so the bitmap doesnt get drawn too big since that might cost some performance.

Upvotes: 1

The Vee
The Vee

Reputation: 11580

I'm afraid I found the cause in Android source codes:

Drawable.java

public abstract void setColorFilter(ColorFilter cf);

PictureDrawable.java

@Override
public void setColorFilter(ColorFilter colorFilter) {}

Simply put, any call to setColorFilter is useless for PictureDrawables.

Upvotes: 3

Related Questions