2

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?

Community
  • 1
  • 1
Rafael
  • 6,339
  • 5
  • 22
  • 22

2 Answers2

3

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.

The Vee
  • 11,420
  • 5
  • 27
  • 60
  • See also my related question and the discussion in the comments: http://stackoverflow.com/questions/13655917/how-do-i-tint-this-picturedrawable – The Vee Jul 18 '13 at 09:51
1

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.

Simon Meyer
  • 1,946
  • 12
  • 23