Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 558
bebosh Avatar asked Dec 23 '14 10:12

bebosh


People also ask

What is the use of canvas in android?

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).

What is canvas and Bitmap in Android?

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.

What does canvas translate do?

Actually, translate moves the entire coordinate system. This has the desired effect of also moving the thing you are drawing. I draw the tree first at (0,0) . Then I translate the origin of the coordinate system to some other spot on the canvas.


1 Answers

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.

like image 132
tato.rodrigo Avatar answered Oct 16 '22 14:10

tato.rodrigo