Android canvas and drawing-canvas and drawables (2)

Source: Internet
Author: User

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

Drawing on a view object

If the applicationProgramThere is no need for a large amount of graphic processing or a high frame rate (such as a chess game, Snake game, or another slow animation application), you should consider creating a custom view component, and use the view of this component. the canvas parameter of the ondraw () method to draw the image. The biggest convenience of this is that the android framework provides a predefined canvas object, which is used to place the call to draw images.

Inherit the View class (or its subclass) and define the ondraw () callback method. The system calls this method to complete the drawing request of the view object. This is also the place where the canvas object is used to execute all drawing calls. This canvas object is passed in by the ondraw () callback method.

The android Framework calls the ondraw () method only when necessary. Each time the application is requested to complete the drawing task, the view object must be invalidated by calling the invalidate () method. This indicates that the image can be drawn on the view object. Then, the android system calls the ondraw () side of the view object (although this callback method is not guaranteed to be called immediately ).

In the ondraw () method of the custom view component, use the given canvas object to draw all the images (for example, canvas. Draw... () Method or pass the canvas object as a parameter to the draw () method of another class ). Once the ondraw () method is executed, the android framework uses this canvas object to draw a bitmap object with systematic processing.

Note: to send an invalid request in a thread rather than in the main activity thread, you must call postinvalidate ().

For more information on inheriting the View class, see create custom components (Http://developer.android.com/guide/topics/ui/custom-components.html)

For the sample program, see Snake game.CodeFile:/samples/snake /.

Drawing on a surfaceview object

A surfaceview object is a subclass of a special view class. It provides a special graphic painting plane in the view Hierarchy Tree. The main purpose of this graphic painting surface is to provide an auxiliary thread for the application so that the application does not need to wait to complete the painting of the view Hierarchy Tree of the system. On the contrary, the auxiliary thread that references the surfaceview object can draw itself on the canvas object at its own pace.

Create a subclass that inherits the surfaceview class. This subclass should also implement the surfaceholder. Callback class, which is an interface that can notify the underlying surface class of information. For example, create, change, or destroy a surface. These events are crucial to understanding when a graph can be drawn, whether it needs to be adjusted based on new surface properties, and when to terminate the graph painting and killing certain tasks. Inside the surfaceview subclass, it is also a good place to define the auxiliary Thread class. It will execute all the operations for drawing the image to the canvas object.

Do not directly process the surface object. You should use the surfaceholder object to process it. Therefore, when the surfaceview subclass is initialized, you must call the getholder () method to obtain the surfaceholder object. The addcallback () method should be called to notify the surfaceholder object, which can receive the surfaceholder callback object (surfaceholder. Callback), and then rewrite each surfaceholder. callbacke method in the surfaceview subclass.

In order to draw a graph to the canvas object of the Surface object in the auxiliary thread, the surfacehondler object and the canvas object obtained using the lockcanvas () method must be passed to the auxiliary thread. Now we can use the given surfaceholder object and canvas object to start drawing the image. Once the canvas object is used to complete the drawing task, we need to call the unlockcanvasandpost () method, pass the drawing to this method using a canvas object. Now the surface object will leave the canvas object for drawing the image. Each time you want to re-draw the image, you must perform the lock and unlock process.

Note: Each canvas object obtained through the surfaceholder object will be retained in its previous state. To correctly process the image, you must re-draw the entire surface object. For example, you can clear the color previously filled in the canvas object with the drawcolor () method, or the background image set with the drawbitmap () method. Otherwise, you will see the trajectory of the previously executed graph.

for the sample application, see the Lunar Lander game, which is in the SDK sample Folder: /samples/lunarlander /.

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.