Android draws a small knowledge point of view

Source: Internet
Author: User

"Onmeasure"

A custom control that inherits view directly needs to override the Onmeasure method and set its own size when wrap_content, otherwise using wrap_content in the layout is equivalent to match_parent.

@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); intWidthsize =measurespec.getsize (WIDTHMEASURESPEC); intHeightsize =measurespec.getsize (HEIGHTMEASURESPEC); intWidth= -;//Adjust the default value size as required        intheight= -;//Adjust the default value size as required        if(Widthmode = = Measurespec.at_most && Heightmode = =measurespec.at_most)        {setmeasureddimension (width, height); }Else if(Widthmode = =measurespec.at_most)        {setmeasureddimension (width, heightsize); }Else if(Heightmode = = Measurespec.at_most){
Setmeasureddimension (widthsize, height);
}
}

LinearLayout calls through Measure Onmeasure

ViewGroup is an abstract class, there is an abstract method OnLayout; Onmeasure method is not overridden, but a measurechildren is provided to measure each child element

ViewGroup's measurement process onmeasure is generally overridden in its subclasses, such as Onmeasure in LinearLayout, which first determines the linear direction and then traverses the measurement (performed on each child element

Measurechildbeforelayout () method, which internally calls the child element's measure () method to measure its own size) child element, the child element is measured, linearlayout will measure its own size

Setmeasureddimension (Resolvesizeandstate (MaxWidth, Widthmeasurespec, childstate), heightsizeandstate);

View is not an abstract class

The exact width and height of the view may not be available in the activity's lifecycle Oncreat,onresume, because the measure process of the view and the activity's life cycle are not synchronized.

WORKAROUND: 1. Execute View.getmeasuredwidth () in the onwindowfocuschanged of the view

"OnLayout"//linearlayout through layout call OnLayout

The layout method determines the location of the view itself, and onlayout iterates through the child elements (through Setchildframe) to invoke the layout method of the child elements to determine the position of the child elements.

"OnDraw"

Call the following four methods by using the Draw method

1. Draw the background background.draw (canvas).

2. Draw your own OnDraw (canvas).

3. Draw Children (Dispatchdraw (canvas)).

4. Draw Decorations (Ondrawscrollbars (canvas)).

The rendering of the view is implemented through Dispatchdraw, where the dispatchdraw iterates through the draw method of all the child elements, so that the draw event can be passed down one layer at a

The viewroot corresponds to the Viewrootimpl class, which is the link between WindowManager and Deco view (framelayout).

Android draws a small knowledge point of view

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.