converting a canvas into bitmap image in android
Advice depends upon what you are trying to do.
If you are concerned that your controls take a long time to draw, and you want to draw to a bitmap so you can blit the bitmap rather than re-drawing via a canvas, then you don't want to be double-guessing the platform - controls automatically cache their drawing to temporary bitmaps, and these can even be fetched from the control using getDrawingCache()
If you want to draw using a canvas to a bitmap, the usual recipe is:
- Create a bitmap of the correct size using
Bitmap.createBitmap()
- Create a canvas instance pointing that this bitmap using
Canvas(Bitmap)
constructor - Draw to the canvas
- Use the bitmap
How to draw on canvas and convert to bitmap?
Found this article may help: http://www.informit.com/articles/article.aspx?p=2143148&seqNum=2
draw.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bitmap imageBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(imageBitmap);
float scale = getResources().getDisplayMetrics().density;
Paint p = new Paint();
p.setColor(Color.BLUE);
p.setTextSize(24*scale);
canvas.drawText("Hello", imageView.getWidth()/2, imageView.getHeight()/2, p);
imageView.setImageBitmap(imageBitmap);
}
});
Android Canvas to Bitmap
Try it this way...
- Create a bitmap
of the correct size using Bitmap.createBitmap()
- Create a canvas instance
pointing that this bitmap using Canvas(Bitmap)
constructor
- Draw to the canvas
- Use the bitmap
Canvas to Bitmap
As suggested Maulik.J, I looked at converting a canvas into bitmap image in android again.
I could not understand from a bit close expression of this link. But, then I saw below text taken from http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas . So, I solved this problem with the help of Maulik.J and the following text:
The Bitmap is always required for a Canvas. You can set up a new Canvas like this:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Now your Canvas will draw onto the defined Bitmap. After drawing upon it with the Canvas, you can then carry your Bitmap to another Canvas with one of the Canvas.drawBitmap(Bitmap,...) methods. It's recommended that you ultimately draw your final graphics through a Canvas offered to you by View.onDraw() or SurfaceHolder.lockCanvas()
I created a second Canvas object having a bitmap which has same dimensions with my real canvas, I drew all my illustrations on this second temporary canvas. Then, I drew its bitmap on my real canvas.
Thanks.
How to convert canvas to bitmap in android?
Your bitmap is black because you didn't draw anything on it.
To have some drawing in the bitmap you must indeed create it with this call :
bitmap = Bitmap.createBitmap(getDrawingCache());
Then you have a stackoverflow exception simply because :
getDrawingCache()
callonDraw(Canvas)
onDraw(Canvas)
callgetDrawnMessage(Canvas canvas)
getDrawnMessage(Canvas canvas)
callgetDrawingCache()
getDrawingCache()
callonDraw(Canvas)
- ...
So you real problem is the StackOverflow exception.
Solution : don't call getDrawnMessage(Canvas canvas)
in the onDraw(Canvas)
method.
Related Topics
Bad Class File Magic or Version
How to Show Dependencies Tree in Android Studio
How to Mock Location on Device
Android Service Stops When App Is Closed
Android Linearlayout:Add Border with Shadow Around a Linearlayout
Programmatically Select Item Listview in Android
Webview Load Website When Online, Load Local File When Offline
How to Make an Android App Return to the Last Open Activity When Relaunched
Android Customized Button; Changing Text Color
What Is Different Between Mainactivity.This VS Getapplicationcontext()
Passing Arraylist of Objects Between Activities
Android - Detect Phone Unlock Event, Not Screen On
Get Content Uri from File Path in Android
Android Canvas: Drawing Too Large Bitmap
Call Activity Method from Adapter
How to Send a Sms Using Smsmanager in Dual Sim Mobile
Android Emulator Doesn't Take Keyboard Input - Sdk Tools Rev 20