A little understanding of canvas and bitmap

Source: Internet
Author: User

In Android's API description for canvas, it begins with the following description:

To draw something, your 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).

First translate this sentence,

When you want to draw something, you need 4 basic components, a bitmap to store pixels, and a canvas to receive draw calls (the result of draw is to draw the pixels to the previous bitmap),
A source, which is what you want to draw (such as a rectangle, path, text, or another bitmap), and finally a brush (describing the color and style you want to paint, etc.)

Inadvertently see this description, my understanding of canvas and bitmap suddenly has a simple feeling.

Bitmap
First of all, imagine that the images we see on the screen are made up of a single pixel, so where do these pixels come from? From the above, we will know the answer, these pixels are stored in
Bitmap, we can assume that bitmap is a display object that is used to display directly on the window, which is a final product.
We know that through bitmapfactory, we can create a bitmap from the resources (Resource), InputStream, and files, and give it to the control to use.

    publicstaticreadBitmapFromResourceint resId) {        new BitmapFactory.Options();        op.inPreferredConfig = Bitmap.Config.ARGB_8888;        false;        false;        InputStream is = context.getResources().openRawResource(resId);        returnnull, op);    }

Canvas
So how do you understand canvas?

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);            new Canvas(bitmap);

The above is a piece of code to create a canvas, and we know that to create a canvas, you must first create a bitmap object and pass it as a parameter to the canvas.
So, can we just create a canvas and then not pass the bitmap object to it?
No!
Why?
Canvas is meant to be a canvas, but in Android it's just a medium that can be called by canvas, drawrect,drawcircle, and so on, but in fact, when these things finally show up,
are pixels, but only bitmap can save pixels, and canvas does not, so when you create a canvas, you must pass a bitmap that is used to carry the contents of the picture.
As you can understand, there is now a blank paper (BITMAP) in front of us, but we cannot point the brush directly above, we must first put a canvas on this piece of paper, and then we are on this canvas
Start point Point ... Finally, we can see that all of our shapes are through the canvas to the paper, at this time, the blank paper is already a picture, and we lift off the canvas, we can direct this picture to others to see.
At this point, the canvas has no effect, its function is only to undertake our brush operation, and then the corresponding location of the pixels stored in the corresponding bitmap.

    canvas.drawPath(mPath, mHighlightPaint);    return bitmap;

I do not know, so that people can understand the relationship between canvas and bitmap?
To put it simply, we have to do the painting, we need to do it through canvas, but every canvas has to have a corresponding bitmap to carry the content, and what we do on the canvas will eventually be written in bitmap.
And the final display to the user to see the content, but also through the bitmap to show.
The above also talked about two things, a drawing primitive, a paint, it is better to understand, it is not here to say more.

drawable
And we often encounter a drawable class, what is this thing, and what is the relationship with bitmap?

A drawable is a general abstraction for "something so can be drawn."
Drawable are objects that can be drawn onto a canvas.

Therefore, bitmap is a kind of drawable, so there is bitmapdrawable, color is a kind of drawable, so there is colordrawable. The shape is a drawable, so there is a shapedrawable.
Personally, it is a similar interface and so on, but in essence it is also an abstract class, inheriting the drawable, it shows that this thing is can be drawn.

A little understanding of canvas and bitmap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.