7

I am using svgandroid to generate PictureDrawables from SVG raw resources. However, I can't seem to be able to apply a color filter on a drawable created this way.

The old code using PNGs

icon = getResources().getDrawable(R.drawable.ic_braille);
icon.setColorFilter(0x88880000, Mode.MULTIPLY);
((ImageView)v.findViewById(R.id.icon)).setImageDrawable(icon);

works, but

icon = SVGParser.getSVGFromResource(getResources(), R.raw.ic_braille).createPictureDrawable();
icon.setColorFilter(0x88880000, Mode.MULTIPLY);
((ImageView)v.findViewById(R.id.icon)).setImageDrawable(icon);

does not. I have tried applying the color filter on the Drawable, on the ImageView (after setting the drawable to it), through XML, even on the Drawable after setting it as the drawable of the ImageView, either is OK for the PNG but neither works for the PictureDrawable. Replacing setImageDrawable by setBackgroundDrawable, as suggested by some, does not render the drawable at all. I am running the code on Androids 1.6, 2.3, 4.0, no difference. I have checked the source of the SVG library, it does not touch color filters in any point. What am I doing wrong? Is tinting unavailable for some kinds of drawables?

The Vee
  • 11,420
  • 5
  • 27
  • 60
  • There is a possibility of what you have said "unavailable for some kinds of drawables" – Ali Imran Dec 01 '12 at 03:51
  • 2
    OK, I found this in PictureDrawable.java just now: `@Override public void setColorFilter(ColorFilter colorFilter) {}` Pretty stupid :-( I'd really like to see my PictureDrawables tinted. Hope there's another way. – The Vee Dec 01 '12 at 03:56
  • 1
    Right now, I am working this around using a custom Drawable that keeps a cache bitmap updated on the change of the bounding rectangle and paints it using a Paint with the color filter. Ugly as hell, because Canvas does not report its resulting size in pixels when transformations are applied. – The Vee Dec 01 '12 at 11:04
  • 2
    Solved the above way but subclassing `ImageView` instead, this seems clean enough. I think `PictureDrawable` should throw an `UnimplementedOperationException` upon calls to `setColorFilter` or `setAlpha`, that could save people like me a hour or two. – The Vee Dec 17 '12 at 20:10
  • You accepted the answer for svg-android. Can you provide an APK file of that library? – Seraphim's Mar 14 '13 at 13:20
  • I can't but you can extract the source and add it to your project, or create and compile a new library project. It's just 6 source files. – The Vee Mar 16 '13 at 14:05

1 Answers1

2

Use this fork of svg-android: https://github.com/japgolly/svg-android

It supports ColorFilters.

Golly
  • 1,319
  • 8
  • 18
  • Can you explain how to use that fork? It's seems to be a Maven project. Can someone provide the APK file? – Seraphim's Mar 14 '13 at 13:19
  • As far as I see, it only support ColorFilters when parsing? I still cannot apply different or changing ColorFilters to the Picture/PictureDrawable/ImageView? – sstn Sep 13 '13 at 12:33