onmeasure Example Analysis

Source: Internet
Author: User

This article transferred from: http://blog.csdn.net/u012604322/article/details/17097105

The above two views are not given in the Android API but a widget view that is used in call answering and alarm clock--glowpadview.java

We use the source code to see how the size of this view is controlled by Onmeasure.

@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        Final intMinimumwidth =getsuggestedminimumwidth (); Final intMinimumheight =getsuggestedminimumheight (); intComputedwidth =resolvemeasured (Widthmeasurespec, minimumwidth); intComputedheight =resolvemeasured (Heightmeasurespec, minimumheight); Setmeasureddimension (Computedwidth, computedHeight); }
    @Override    protectedint  getsuggestedminimumwidth () {        // View should is large enough to contain the background + handle         and // target drawable on either edge.        return (int) (Math.max (Mouterring.getwidth (), 2 * Mouterradius) + mmaxtargetwidth);    }

The mouterring is a specified circle (drawn by shape, so the width and height, equivalent to the radius of the circle), Mouterradius the radius of the largest dashed circle, and mmaxtargetwidth as the widths of the zzz picture or other image in the figure. This part of the value is the size of the properties that have been specified by the developer themselves in their own view

    Private intResolvemeasured (intMeasurespec,intdesired) {        intresult = 0; intSpecsize =measurespec.getsize (MEASURESPEC); Switch(Measurespec.getmode (Measurespec)) { CaseMeasureSpec.UNSPECIFIED:result=desired;  Break;  CaseMeasureSpec.AT_MOST:result=math.min (specsize, desired);  Break;  Casemeasurespec.exactly:default: Result=specsize; }        returnresult; }

From the width analysis, the viewgroup that hosts this view may have two situations, one (a) providing more space than the value we give above, and two (B) being small (we will not allow this to happen in development, but the logic of design still has to take this into account). Then it is to see the view of the Layout_width, one is wrap_content, corresponding to At_most,a to get the value of the given value, b value is the value of the parent view, although we do not want this, but the parent view only gives the space, we have to do so.

The second is the match_parent, which corresponds to the value of only one parent view, which is also in line with the requirements.

A good design should take into account the use of a variety of situations, and a reusable framework design is more so, before the design needs to anticipate a variety of possible applications.

onmeasure Example Analysis

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.