I'm using a LayerDrawable to merge multiple Drawable. Now, I'd like to export my LayerDrawable to a file.
I've tried this way:
Bitmap b = ((BitmapDrawable)myLayerDrawable).getBitmap();
--> ClassCastException...
What can I do?
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With