Android canvas and drawing-canvas and drawables (1)

Source: Internet
Author: User

Note: This article translated from: http://developer.android.com/guide/topics/graphics/2d-graphics.html

The android framework API provides a set of 2D plotting APIs that can render custom images on a canvas and modify existing view objects, to customize their appearance and visual effects. When drawing a 2D image, you usually need to use one of the following two methods:

1. Draw a graph or animation to a view object in the layout. In this way, the process of drawing hierarchical view data is usually handled by the System-you only need to define the drawing to be drawn into the view object.

2. Draw the image directly on a canvas object (canvas object ). In this method, you must call the ondraw () method of the corresponding class (pass the image to the canvas object), or call a draw... () Method (such as drawpicture ()). In this process, you can also control any animation.

Method 1 is the best choice when you want to draw a simple image that does not require dynamic changes and does not require game performance to a view object. For example, to display a static image or a predefined animation in a static application, you should use method 1 to draw the image into a view object.

When the application needs to redraw itself frequently, it is a good choice to use method 2 to draw the image into the canvas. Applications such as video games should draw graphics on their own canvas objects. However, there are more ways to complete the drawing task:

1. In the same thread as the UI activity, to create a custom view object component in the layout, you must first call the invalidate () method and then process the ondraw () callback method;

2. A surfaceview object is managed in an independent thread, and a thread is used to execute the task of drawing the image to the canvas object (the invalidate () method is not required ).

Draw a drawing using a canvas object (draw with a canvas)

When you want to write professional graphics or graphics animation control applications, you should use the canvas object to draw with all your efforts. Canvas uses a virtual plane to draw an image on the actual surface-it holds all call Methods Starting with draw. A canvas object is actually used to draw an underlying bitmap, which is placed in a window.

In the draw event of the ondraw () callback method, a canvas object is provided, and you only need to hand over the content to be drawn to the canvas object. When processing a surfaceview object, you can also obtain a canvas object from the surfaceholder. lockcanvas () method. However, if you want to create a new canvas object, you must define a bitmap object on the canvas object that is actually being drawn. For a canvas object, this bitmap object is always required. You should create a new canvas object as follows:

Bitmap B
= Bitmap. createbitmap (100,
100,
Bitmap. config. argb_8888 );
Canvas C
= New
Canvas (B );

Now we can draw on the defined bitmap object. After drawing a drawing on a canvas object, you can use canvas. drawbitmap (bitmap ,...) To draw the bitmap object to another canvas object. It is recommended that you use the canvas object provided by the view. ondraw () method or sufaceholder. lockcanvas () method to complete final graphic rendering.

The canvas class has a set of drawing methods that you can use, such as drawbitmap (...) , Drawrect (...) , Drawtext (...) . You can also use other draw () method classes. For example, you may want to place some drawable objects on the canvas object. The drawable class has a draw () method with a canvas object as the parameter.

Related Article

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.