CanvasBitmap and Canvas

Source: Internet
Author: User

CanvasBitmap and Canvas

In the Android Canvas API description, it is described as follows at the beginning:

To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw CILS (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 ).

Translate this sentence first,

When you want to draw something, you need four basic components and a Bitmap to store pixels, a Canvas receives the call of draw (the result of draw is to draw pixels to the Bitmap mentioned above ),
One source is what you want to draw (such as a rectangle, path, text, or another Bitmap), and the last is a paint brush (describe the color and style you want to draw)

When I accidentally saw this description, my understanding of Canvas and Bitmap suddenly felt so simple.

Bitmap
First, imagine that the images we see on the screen are composed of pixels. Where do these pixels come from? From the above sentence, we will know the answer. These pixels are stored in
Bitmap is a display object that is directly displayed in a window. It is a final product.
We know that through BitmapFactory, we can create a Bitmap from resources, InputStream, and files and hand it over to the control.

    public static Bitmap readBitmapFromResource(Context context, int resId) {        BitmapFactory.Options op = new BitmapFactory.Options();        op.inPreferredConfig = Bitmap.Config.ARGB_8888;        op.inDither = false;        op.inScaled = false;        InputStream is = context.getResources().openRawResource(resId);        return BitmapFactory.decodeStream(is, null, op);    }

Canvas
So how to understand Canvas?

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

The above is a piece of code for creating a Canvas. 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 only create a Canvas without passing Bitmap objects to it?
No!
Why?
Canvas is intended to be a Canvas, but in Android, it is just a media. You can use Canvas to call drawRect, drawCircle, and so on. But in fact, when these items are finally displayed,
All are pixels, but only Bitmap can save pixels, while Canvas does not. Therefore, when creating a Canvas, a Bitmap must be passed to carry the painting content.
It can be understood that there is a blank paper (Bitmap) in front of us, but we cannot point the paint brush directly to it. We must put a Canvas on the paper first ), and then we are on this canvas.
Start Point... Finally, we can see that all of our images are applied to the paper through the canvas. At this time, the blank paper is already a painting, and we have removed the canvas, you can directly show this picture to others.
At this time, Canvas has no function, and its function is only to take the paint brush operation, and then store the pixels at the corresponding position to the corresponding Bitmap.

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

I don't know. Can you understand the relationship between Canvas and Bitmap?
To put it simply, we need to implement painting through Canvas, but each Canvas must have a corresponding Bitmap to carry content, the operations we perform on the Canvas will eventually be written to Bitmap.
The content finally presented to users is also presented through Bitmap.
Two things are mentioned above, one drawing primitive and one paint, which is easy to understand. I will not talk about it here.

Drawable
We often encounter a Drawable class. What is this thing, and what is its relationship with Bitmap?

A Drawable is a general role action for "something that can be drawn ."
Drawable is an object that can be drawn to the canvas.

Therefore, Bitmap is a Drawable, so BitmapDrawable is available, and the color is a Drawable, so ColorDrawable. shape is a Drawable, so ShapeDrawable is available.
In my opinion, it is something like an interface, and in essence it is also an abstract class that inherits Drawable, it indicates that this stuff can be drawn.

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.