What is the relationship between Canvas and Bitmap?

What is the relationship 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 do not understand this code. Why is a Bitmap drawing a composition of bmp1 and bmp2?

+4
source share
2 answers

Basically, Canvas supported by Bitmap , so when you draw something using the canvas, the canvas will draw in the Bitmap with which it was created. So, when you draw these two bitmaps using a canvas, it is going to combine the bitmaps together and the result will be saved in drawingBitmap as it supports the canvas.

The analogy is similar to the correct one, although it is probably confusing (and overly simplifying, which I also do above) - as I mentioned in the commentary, you can think of Canvas as a pen, Paint as a configuration of this pen (for example, replaceable ink or something- something else that you can fit into the idea of ​​a custom pen) and Bitmap as the paper you draw on. The analogy becomes confusing if you pay too much attention to the accepted meaning of words.

+9
source

Think of canvas as a pen and drawingBitmap as a document. You use your pen to draw something on your paper, and you will get what you draw. Technically, you can build a canvas object from Bitmap to draw other bitmaps on it.

+3
source

All Articles