How to cache surfaceview in a bitmap? - android

In my android app there are two SurfaceViews. One SurfaceView is drawn upon when user touches it. I want to copy and replicate the content of this SurfaceView on the other SurfaceView. Probably this can be done by caching the content of the first SurfaceView in a bitmap and then drawing the bitmap on the second SurfaceView. But how to cache the first SurfaceView ?
There are a few similar questions on this forum but they really do not work out for me.

This: surfaceview.getDrawingCache(); will return the bitmap out of the SurfaceView and then you can draw it to the second SurfaceView: canvas.drawBitmap(bitmap, 0, 0, null);.
Another way is to copy the canvas, not the surface view, like this:
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
This means, that if you'll draw something on the canvas, it will be actually drawn on the bitmap.

Related

How to make a background image fill the canvas?

Hello everyone I am new to android and I just have a quick question. I am making an animation using Bitmaps and drawing them to the canvas using the onDraw(). I am trying to draw a background to the canvas that I made in photoshop. I already uploaded it as a drawable and then decoded it to a Bitmap then added it to the onDraw method. It draws, but I doesn't fill the entire canvas like I want it to. Any suggestions and or fixes? Thank you so much, I looked around and saw something about converting the bitmap to an image view but didn't really understand all to well.
Use drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) where the rect for source is the entire bitmap (from 0,0 to width, height) and dest is the entire canvas. This will stretch the bitmap to fill the canvas. Be warned this can cause the aspect ratio to be off and/or issues with fuzziness or blockiness.

Draw on a large canvas, scroll it, save it, reload it ... works, but only the viewport part is saved

My program can draw 2D shapes on a canvas. The result can be scrolled. , When I save the result, then ONLY the image in the viewport is saved.
I use a View and draw on it's canvas.
Saving the drawing/updated result is:
setDrawingCacheEnabled(true);
buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap( this.getDrawingCache());
setDrawingCacheEnabled( false);
// the bitmap I can save into a file
bm.compress( Bitmap.CompressFormat.PNG, 90, new FileOutputStream( new File( filepath)));
When I use a large view/canvas, then I get errors that my drawingCache is not big enough. When I use a regualar view/canvas size, then nearly all drawings outside the viewport are clipped.
==> Can I draw on a bitmap and have that bitmap immediately drawn on the view's canvas?
So, after some drawing, the initial bitmap is updated with the newly added drawing?
Happily, there is a simple solution to this question.
Create a large bitmap with your own size;
Create your own cachedCanvas = new Canvas( largeBitmap);
Draw first on the cachedCanvas'.
In onDraw() do: canvas.drawBitmap( cachedBitmap, 0, 0, null);
Saving your own largeBitmap is easy!

Canvas to 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.

Android Moving Bitmap in canvas

I have an activity with an imageView which has a canvas, and under it a button. when I click the button a new bitmap gets drawn into the canvas with text ..
How do I make the drawn bitmap moveable like I drag it into any place I want in the canvas ?
Canvas c = new Canvas(WorkBitmap);
c.drawText("Bitmap", 360, 520, null);
c.drawBitmap(ball, x, y, null);
I saw some tutorials on SurfaceView and I did them, thye worked fine. But I want the bitmap in my canvas to move, in the tutorials its the whole screen a surfaceView canvas ..
You need to use the SurfaceView widget that is available in the XML
But if you want the image to be move-able around all the screen you could use SurfaceView
Check this Youtube video, It's very informative (watch all the SurfaceView playlist to benefit)

Android save canvas screen as bitmap

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.

Categories

Resources