Is there a way to do it? I d like to save the canvas screen as a Bitmap object, and then draw it again, to the screen.
You can construct a Canvas passing in a Bitmap onto which the canvas will draw. See http://developer.android.com/reference/android/graphics/Canvas.html#Canvas(android.graphics.Bitmap)
You need to construct a Bitmap appropriately sized to the view into which you want to draw.
Just guessing here, but if you are doing this because you want to draw off screen then draw the bitmap on all at once you might also look at SurfaceHolder/SurfaceView.
Related
How can I save the image on a canvas to bitmap?
There are several related posts, but I didn't find the one works for me. To make it clear, here is my problem
What's on my canvas:
An bitmap image which was drawn to the canvas using canvas.drawBitmap(oldBitmap, x, y, null);
Some paths drawn overlay to this image on the same canvas using canvas.drawPath.
Both the image and the paths are draw on a customized SurfaceView. And both they are draw to the canvas in the onDraw function. Or more specific, the implementation is in the following link:
Android: custom draw on an given image (with SurfaceView)
Now I need to save the image on canvas (the oldBitmap and paths I drew) to a newBitmap. Could someone tell me how to do that?
I'm newbie with canvas.
I'm trying to draw child bitmap on parent bitmap using canvas.drawbitmap(childbitmap,matrix,point) method. I'm getting number of bitmaps using loop and trying to overlay image on all that bitmaps But somehow the output which I got haven't original child image. It looks like white image. so I'm able to seen child image on parent one but don't know why it looks like this?
Let me put my code over here with output image.
Canvas mCanvas = new Canvas(bitmap);
Paint p = new Paint();
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.ic_launcher);
mCanvas.drawBitmap(icon,0,0,p);
This above code will run with loop and it will give multiple "bitmap" object on which I have to draw canvas. So now I'm taking launcher icon as child bitmap.while checking final output it shows like below image :
Check the white image instead of launcher icon.So what's wrong with my code?
Waiting for your best suggestion ASAP.
You are drawing your bitmap on top with the current canvas. So you have to call mCanvas.drawBitmap a second time to draw the foreground bitmap.
My program is a simple flood-fill game application for android, namely, the user can paint and draw something on the canvas. Now I want to provide a sharing option to the users. I guess that I must begin with copying the canvas to a bitmap object. I could not find a satisfactory answer because it is generally suggested "to create a new canvas, then..." but I got a canvas like that,
Canvas canvas = holder.lockCanvas();
then I use it. So, how can I copy my current canvas to a bitmap object?
Thanks
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.
I'm using a SurfaceView and canvas. Over time The canvas draws the same bitmap image but at different position. So every time the position of the bitmap moves, any drawings on the bitmap has to be be moved as well. So is there a way to just have the drawings on the bitmap image stay so that when the image moves(when canvas redraws the bitmap at a different location on screen), any drawings on the image moves with it as well?
You can setup a Bitmap to be the backing buffer for your canvas so that drawing to the canvas is, effective, drawing to that Bitmap. This way you can keep it around and do whatever you want with it.
I have a widget which looks like this:
Every cone is a "touchable element". Now I want to put a bitmap texture above each cone. However, bitmap images are all rectangular, so a bitmap texture above one cone would interfere with the bitmap texture above another cone.
I'm wondering what is the best solution to this approach. Should I just create an image which fits (as a rectangle) exactly above the cone and make the non used areas transparent?
A second question is, how do bitmap textures work with stretching? Because this whole circle draws itself to fit the whole screen size, while bitmap textures are pretty much one size.
Offhand I can't think of a better way to draw bitmaps over those cones than your own suggestion of using transparent zones.
However, I can help with your second question as stretching bitmaps is not hard. You've got a few options in the Canvas class. For example:
canvas.save();
canvas.scale(xRatio, yRatio);
canvas.drawBitmap(....);
canvas.restore();
You can also use a Matrix, using matrix.postScale(xRatio, yRatio), to then generate either a larger bitmap and draw it normally, or pass the matrix in to your canvas.drawBitmap(....) command to make it scale while it draws.
All of these methods assume you have access to the drawing canvas itself. If you are using a view, you can subclass it and override the onDraw(Canvas canvas) method to grab the canvas before it starts drawing it. If you're using a SurfaceHolder, then you should already know how to get the canvas.
Edit: I forgot the third method I was going to describe. You can use the canvas.drawBitmap(bitmap, srcRect, dstRect, paint) to also make the canvas scale the bitmap to fit the destination rectangle. In short, there are lots of methods to do this - pick the one that's easiest based on your application!