How to export a View as Bitmap
In last days in my Card library, I tried to export a View as a Bitmap.
A simple way to export the view is to use following code:
Pay attention where you will use this code.
If you use this in
In these cases you have to use something like this before drawing the bitmap:
An example:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVz40VdhtuZbRhw63nY0PVsKLUxpPZcl7-BJoWUBh_Qw1c86jQvpafIX85bPSqIwMqO11nuYU8oHMghpMuY7-TOZEbWWsQb1Oa6HLIQy1uOrFn1f7OXTN4SQzeiLiCqqxi2AW4pV60-qE1/s320/export_full.png)
A simple way to export the view is to use following code:
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas);
Pay attention where you will use this code.
If you use this in
onCreate()
method, the View didn't go through a layout, so its size is null (0x0).In these cases you have to use something like this before drawing the bitmap:
view.measure(widthSpec, heightSpec); view.layout(left, top, right, bottom);
An example:
int spec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED); view.measure(spec,spec); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas);
Comments
Post a Comment