LayerDrawable to bitmap

Have you tried drawing the Drawable to a Bitmap Canvas? I think the call order would go something like:

Bitmap b = Bitmap.createBitmap(int width, int height, Bitmap.Config config);
myLayerDrawable.draw(new Canvas(b));

Then you can write the Bitmap object to an output stream.


Thank both users have answered before me (@Kyle P and @Anjum Shrimali). Inspired by their answers ... This worked for my case fine:

final int width = myLayerDrawable.getIntrinsicWidth();
final int height = myLayerDrawable.getIntrinsicHeight();

final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
myLayerDrawable.setBounds(0, 0, width, height);
myLayerDrawable.draw(new Canvas(bitmap));