1

I'm using svg-android library to deal with SVG's in my app. Long story short, this lib generates a (Vectorial) PictureDrawable from a SVG.

Unfortunately, Android can't drawPicture() into a canvas using Hardware Acceleration, so to achieve acceleration I have to transform the Picture inside the PictureDrawable into a Bitmap.

The problem is, if the drawable host size changes after the bitmap has been created, I have to resample to the new resolution.

I have overriden:

@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    if (Conf.LOG_ON) Log.d(TAG, "OnBoundsChange "+bounds);
    createBitmapWithNewBounds();
}

It works when I manually call setBounds, but when the drawable host size changes, onBoundsChange() is not automatically called (and I don't know if it should be).

So is there any way for the drawable to detect its host size has changed so:

  • I can trigger a manual call to setBounds() ?
  • onBoundsChange() is automatically called?
rupps
  • 9,712
  • 4
  • 55
  • 95

1 Answers1

0

Have you tried overriding protected boolean onStateChanged(int[] state) ?

I've taken the liberty of including the source code and accompanying documentation here: /** * Override this in your subclass to change appearance if you recognize the * specified state. * * @return Returns true if the state change has caused the appearance of * the Drawable to change (that is, it needs to be drawn), else false * if it looks the same and there is no need to redraw it since its * last state. */ protected boolean onStateChange(int[] state) { return false; }

Based on the source of Drawable, the default implementation is to indicate that the state has not changed, as you can see by the return value. Since drawable are not extending Views, this must be their analog of asking for a layout(). Therefore, I think if you simply override the onStateChanged() to return true, you should see the call trickle down to onBoundsChanged() . Obviously, a naive return true is not the right solution, as it would depend on the incoming int[] state , but that should get you on the right track.
Hope this helps.

yrizk
  • 394
  • 2
  • 8