The article directories

  1. Picture understanding model
  2. The key to my enlightenment was the official saveLayer document:
  3. doubt
    1. Canvas why do I think it is called auxiliary/helper coordinate system?
    2. How does canvas make drawing easy?
    3. SaveLayerAlpha (… int alpha..) What about transparency?
    4. The difference between the restoreToCount and restore methods
    5. Why does view ondraw display objects without using the Restore Layer?
    6. Why was the value of the first saveLayer 1? Instead of 0?
  4. Tips
  5. Reference&Thanks:

The working principle of Canvas is based on my subjective inference

If there is an error please point out thank you ~

Picture understanding model

The key to my enlightenment was the official saveLayer document:

This behaves the same as save(), but in addition it allocates and redirects drawing to an offscreen bitmap.All drawing calls are directed to a newly allocated offscreen bitmap. Only when the balancing call to restore() is made, is that offscreen buffer drawn back to the current target of the Canvas (either the screen, it's target Bitmap, or the previous layer).

A newly created bitmap represents Layer. All subsequent operations (including drawingXXX) are directed to this new off-screen bitmap. The off-screen draw cache is only drawn to current when restored Target of the Canvas(bt/offscreen)

doubt

Canvas why do I think it is called auxiliary/helper coordinate system?

Let’s start with an example:

new Cavas(bitmap); Cavas. Translate (0, 10); cavas.drawLine(...) / / horizontal cavas. Translate (0, 10); cavas.drawLine(...) / / horizontal

The result is: The bitmap does not change with the cavas.translate shift, but the draw after the canvas translate shift shows that the draw method is the canvas reference system, the canvas reference system is rorate, translate, Clip and other attributes; After the adjustment of these attributes, it is mapped to the Bitmap coordinate system for drawing, and the image drawn on the Bitmap has nothing to do with the canvas (adjusting the Canvas will not change the bitmap image).

Conclusion: Canvas is like the auxiliary coordinate system. The assistant draw method draws to the final bitmap, and the rat painter will keep rotating the canvas and flipping the canvas to draw lines, mainly for more comfort (for us, it is simpler and easier to understand).

How does canvas make drawing easy?

new Cavas(bitmap); cavas.drawLine(...) / / horizontal cavas. Rorate (90 °, x, y); cavas.drawLine(...) / / horizontal

How do you draw a Line if you don’t have a canvas and you compute the cosine of 90 degrees and the sine of 90 degrees and you compute the coordinate points and then you draw the Line easy, right

SaveLayerAlpha (… int alpha..) What about transparency?

Bitmap will have a method (presumably) called setAlpha(int alpha) that causes the transparency of all drawXXX series images (drawAlpha) to end up =alpha/255*drawAlpha;

The difference between the restoreToCount and restore methods

RestoreToCount (int saveCount): encapsulates restore (saveCount must >=1)

For example: Save four times then restore three times; save(); 1save(); 2save(); 3save(); 4//save stack 1->2->3->4->5(now)restore(); To 4 restore (); To 3 restore (); Go to 2//save stack 1->2 is also equivalent to using restoreToCount(2) directly

Why does view ondraw display objects without using the Restore Layer?

The source code will probably end up using Canvas.restoreToCount (1)

Be sure to restore the off-screen cache or the Layer drawing will not display!

So we need to get into the habit of restore so that the code is not wrong anywhere, right

Why was the value of the first saveLayer 1? Instead of 0?

Because canvas. Save () returns the value of the previous layer. Moreover, Canvas provides Layer support. By default, it can be regarded as only one Layer.

Tips

  • The layer layer must go out of the stack to “draw” the images drawn in this layer onto the top layer or Canvas
  • Tips: If you want the Canvas to display the correct image, you don’t have to leave the stack at will. For example, saveLayer4 times is now 1->2->3->4->5 in the stack

    RestoreToCount (2) is not displayed! Restore (); To 4 restore (); To 3 restore (); So the image must be restored (1) on the second Layer(bitmap) because the first Layer(bt) references the bt of our new Canvas(bt);
  • SaveLayer can specify the size. Because if you create one that is the same size as the original, you may get OOM. Taking the screen size as an example, if one pixel requires 8bit storage space, the number of bits used by 1024768 machines is 1024768*8= 6.2m!

    public int saveLayer(RectF bounds, Paint paint, int saveFlags)

Reference&Thanks:

Blog.csdn.net/harvic88092…