An analysis of the drawing process of Android UI (iv.) Draw process

Source: Internet
Author: User

Objective

Draw is the last step in the three steps of drawing a view. Like measure, layout, usually does not rewrite the draw itself, draw internal calls the OnDraw method, sub-Class View needs to rewrite OnDraw (Canvas) to complete the final drawing.

If you must rewrite draw (canvas), you need to call Super.draw (canvas) at the beginning of the method.

Draw Process

Draw internal specifically did what things, in the View.java source comments have done a very detailed introduction

        /*          * Draw Traversal performs several drawing steps which must is executed         * in the appropriate order:         *         *      1. Draw the background         *      2. If necessary, save the canvas ' layers to prepare for fading         *      3. Draw View ' s content         *      4. Draw Children         *      5. If necessary, draw the fading edges and restore layers         *      6. Draw Decorations (scrollbars for instance)         * /
    1. Draw background
    2. Keep the canvas's layers, if necessary, for fading effects
    3. Draw the content, this step calls the OnDraw method
    4. To draw a child view, you need to implement this method for ViewGroup
    5. Draw the edges of the fading and restore the layers, if needed
    6. Draw attachments (such as scroll bars)
A simple, custom view

Draw this process, there are not many things to talk about at the present stage. If you go deep into a specific SDK already implemented view inside to see the code, enough for me to eat a large pot of-just the most commonly used TextView, its ondraw is very complex.

Here, you implement a very simple custom view--display a line of text on a background color. Let's take a look at the finished page:

Isn't it very simple? The following is a custom Threebodyview.java

 Public classThreebodyiviewextendsView {PrivatePaint Mpaint; PrivateString MText;  PublicThreebodyiview (Context context, AttributeSet attrs) {Super(context, attrs); Mpaint=NewPaint (Paint.anti_alias_flag); MText= "Nothing to show ..."; } @Overrideprotected voidOnDraw (canvas canvas) {mpaint.setcolor (color.green);//Background ColorCanvas.drawrect (0, 0, GetWidth (), GetHeight (), mpaint);        Mpaint.setcolor (color.red); Mpaint.settextsize (100);//text color, sizeCanvas.drawtext (mText, 0, GetHeight ()/2, Mpaint); }     Public voidSetText (String s) {MText=s; Super. Invalidate (); }}

Threebodyview.java displays a line of text in a rectangular area and can dynamically change the text displayed by the SetText (String) method.

When used, it is used in the following way:

Fake_main_activity.xml

<?XML version= "1.0" encoding= "Utf-8"?><Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/fake_main_layout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"    >    <Com.leili.imhere.view.ThreeBodyiViewAndroid:id= "@+id/three_body_view"Android:layout_width= "300DP"Android:layout_height= "300DP" /></Framelayout>

Fakemainactivity.java

 Public class extends Activity {    private  threebodyiview Tbview;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {          Super. OnCreate (savedinstancestate);         Super . Setcontentview (r.layout.fake_main_activity);         Super . Findviewbyid (R.id.three_body_view);        Tbview.settext ("The world belongs to three bodies!") ");    }}

Summary

The process of draw is analyzed, and a weakening TextView is implemented in a simple demo,demo when explaining OnDraw.

The next article is the end of this series and will make a more complete custom view/viewgroup.

An analysis of the drawing process of Android UI (iv.) Draw process

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.