How to copy one canvas into another canvas? - android

I have drawn some rects into a canvas and I want to copy those rects into another canvas.The commonly discussed solution of using bitmaps will not work because I didn't use a bitmap in this canvas, no images only those rects.
How do I do that?

Have you tried saving the attributes given to the rectangles upon drawing to the first canvas, and then redrawing them on the second canvas based on these attributes? Perhaps you could try creating a Rect-object for the first canvas and passing it to Canvas.drawRect() on the second canvas?

You can create a bitmap using a canvas and you draw on that bitmap.
bitmap = Bitmap.createBitmap(50,50,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// drawn some rects into canvas
Now you can use the bitmap to draw on any canvas you want;
Canvas canvas1 = new Canvas(bitmap);
Canvas canvas2 = new Canvas(bitmap);
Or if you have a canvas already:
canvas.drawBitmap(bitmap, 0, 0, paint);
This way you draw rects only once and not on each canvas.

Related

What is the relationship between canvas and matrix in Android?

I read this canvas overview :
The Canvas class holds the "draw" calls. To draw something, you need 4
basic components: A Bitmap to hold the pixels, a Canvas to host the
draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
Path, text, Bitmap), and a paint (to describe the colors and styles
for the drawing).
Can anyone explain the canvas more clearly?
And I am confused about the relationship between the canvas and matrix. Does the canvas takes the transformations from the matrix?
And I want to know if below function affect on the canvas?
canvas.drawBitmap(bitmap, matrix, paint);
In other words, is the canvas matrix different from the bitmap matrix?
I asked this, because when I'm using canvas.drawBitmap and then using canvas.concat() and then drawing any object, this object takes same transformations on canvas, so I think the canvas and bitmap have the same matrix!!
They are different. When using the canvas to draw a bitmap providing a matrix, internally, the provided matrix are concatenated to the current canvas matrix.
In other words, calling canvas.drawBitmap(rectBitmap, matrix, paint); has the same effect of:
canvas.save();
canvas.concat(matrix);
canvas.drawBitmap(rectBitmap, 0, 0, paint);
canvas.restore();
This explain why your object is taking the same transformations because you are calling canvas.concat(matrix); and after that drawing the object.

Android: Canvas drawBitmap?

I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This is what I've tried:
drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);
What am I doing wrong?
From developer.android.com:
Parameters
bitmap The bitmap to be drawn
src May be null. The subset of the bitmap to be drawn
dst The rectangle that the bitmap will be scaled/translated to fit into
paint May be null. The paint used to draw the bitmap
What is missing in my code?
Thanks!
You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)
So it should look like this:
drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);

Android creating a circle bitmap without drawing it on canvas

My question is: is there any way to create a circle and then make it bitmap and not to display it on canvas??For example make a black circle then convert it to a bitmap but not using the canvas.setBitmap(Bitmap bitmap).Thanks in advance.
You can easily get a bitmap object using Bitmap static method (Bitmap.create) and then use that object to draw anything on it by instantiating a canvas object with bitmapObject.
your code should look something like this:
Bitmap bitmap = Bitmap.create(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//draw on your canvas.

Best approach to fast copy bitmaps in code on Android?

Background: Using Canvas, Paint and Path objects I draw several geometries on the canvas, mostly polygons and circles. They fill most of the Android screen.
Question: With Mathematica I can 'fast-copy' Graphics using Translate ( in x and y direction ), after which the resulting image is automatically zoomed out such that all copies are visible. ( For example. Draw a square that fills the entire screen, copy it using (2,2) and four squares appear. ) The premise is that copying is a faster operation. - Is a similar operation possible on Android?
There's nothing as convenient as that, but to achieve the effect you can draw directly to a Bitmap and re-use it - scaling and translating it yourself.
public void onDraw(Canvas canvas) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas bmpCanvas = new Canvas(bmp);
// draw into bmpCanvas
// ...
// draw bitmap using
// public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
canvas.drawBitmap(bmp, ...);

what is the relation between Canvas and Bitmap?

What is the relation between Canvas and Bitmap?
Bitmap drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(),
bmp1.getHeight(), bmp1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bmp1, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SCREEN));
canvas.drawBitmap(bmp2, 0, 0, paint);
compositeImageView.setImageBitmap(drawingBitmap);
I don't understand this code.Why the drawingBitmap is the composition of bmp1 and bmp2?
Basically, the Canvas is backed by a Bitmap, so when you draw anything using the canvas, the canvas will draw into the Bitmap it was created with. So, when you draw those two bitmaps using the canvas, it's going to composite the bitmaps together and the result will be stored in drawingBitmap, as it's backing the canvas.
Anh's analogy is correct-ish, though probably confusing (and over-simplifying, which I'm also doing above) – as I mentioned in a comment, you can think of the Canvas as the pen, the Paint as a configuration of that pen (e.g., replaceable ink or something - whatever you can fit into the idea of a configurable pen), and the Bitmap as the paper you draw onto. The analogy becomes confusing if you focus too much on the accepted meaning of the words.
Let's think canvas as a pen, and drawingBitmap as a paper. You use your pen to draw something on your paper, and you get what you draw.
Technically, you can construct Canvas object from Bitmap to draw others bitmaps on it.
Canvas is the place or medium where perfroms/executes the operation of drawing, and Bitmap is responsible for storing the pixel of the picture you draw.

Categories

Resources