简体   繁体   中英

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM