I need a piece of advice with setting opacity to a PictureDrawable
object since usual setAlpha()
is not implemented in PictureDrawable
.
My case is pretty similar to this one (I also get my drawables from SVGs in raw resources using a library). But I cannot use the solution because I would like to change the opacity dynamically (not by applying color filter at creation).
The context is animating multiple drawables when drawing on a SurfaceView
.
What could be a good workaround in this situation?
Thank you.
Credit goes to @pskink, I'll just elaborate it a little for further references.
This is what I do in onDraw(Canvas canvas)
method (in my case int opacity
changes iteratively from 0 to 255):
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
canvas.saveLayerAlpha(x, y, x + width, y + height, opacity,
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
drawable.setBounds(x, y, x + width, y + height);
drawable.draw(canvas);
canvas.restore();
This answer also provides good guidance on saveLayerAlpha()
usage.