• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            xiaoguozi's Blog
            Pay it forword - 我并不覺(jué)的自豪,我所嘗試的事情都失敗了······習(xí)慣原本生活的人不容易改變,就算現(xiàn)狀很糟,他們也很難改變,在過(guò)程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛(ài)傳出去,很困難,也無(wú)法預(yù)料,人們需要更細(xì)心的觀察別人,要隨時(shí)注意才能保護(hù)別人,因?yàn)樗麄兾幢刂雷约阂裁础ぁぁぁぁ?/span>

            今天學(xué)習(xí)android自定義組件:docs/guide/topics/ui/custom-components.html

            其中有兩個(gè)對(duì)布局界面影響很的方法,onDraw(),和onMeasure().

            onDraw()比較好理解.onMeasure()就比較難理解一些,也更復(fù)雜些 ,引用文檔中的說(shuō)法就是:

            onMeasure() is a little more involved.
            其實(shí)還有另一個(gè)方面的原因就是我對(duì)這個(gè)單詞measure不是很知道,然后果了下詞典,就放了下心,確實(shí)是測(cè)量的意思.

            實(shí)現(xiàn)onMeasure()方法基本需要完成下面三個(gè)方面的事情(最終結(jié)果是你自己寫(xiě)相應(yīng)代碼得出測(cè)量值并調(diào)用view的一個(gè)方法進(jìn)行設(shè)置,告訴給你的view安排位置大小的父容器你要多大的空間.):

            1.傳遞進(jìn)來(lái)的參數(shù),widthMeasureSpec,和heightMeasureSpec是你對(duì)你應(yīng)該得出來(lái)的測(cè)量值的限制.

             

            The overidden onMeasure() method is called with width and height measure specifications(widthMeasureSpec and heightMeasureSpec parameters,both are integer codes representing dimensions) which should be treated as requirements for the restrictions on the width and height measurements you should produce.

            2. 你在onMeasure計(jì)算出來(lái)設(shè)置的width和height將被用來(lái)渲染組件.應(yīng)當(dāng)盡量在傳遞進(jìn)來(lái)的width和height 聲明之間.

            雖然你也可以選擇你設(shè)置的尺寸超過(guò)傳遞進(jìn)來(lái)的聲明.但是這樣的話,父容器可以選擇,如clipping,scrolling,或者拋出異常,或者(也許是用新的聲明參數(shù))再次調(diào)用onMeasure()

            Your component's onMeasure() method should calculate a measurement width and height which will be required to render the component.it should try to stay within the specified passed in.although it can choose to exceed them(in this case,the parent can choose what to do,including clipping,scrolling,throwing an excption,or asking the onMeasure to try again,perhaps with different measurement specifications).

            3.一但width和height計(jì)算好了,就應(yīng)該調(diào)用View.setMeasuredDimension(int width,int height)方法,否則將導(dǎo)致拋出異常.
            Once the width and height are calculated,the setMeasureDimension(int width,int height) method must be called with the calculated measurements.Failure to do this will result in an exceptiion being thrown
               

            在Android提提供的一個(gè)自定義View示例中(在API demos 中的 view/LabelView)可以看到一個(gè)重寫(xiě)onMeasure()方法的

            實(shí)例,也比較好理解.

            01/**
            02 * @see android.view.View#measure(int, int)
            03 */
            04@Override
            05protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            06    setMeasuredDimension(measureWidth(widthMeasureSpec),
            07            measureHeight(heightMeasureSpec));
            08}
            09 
            10/**
            11 * Determines the width of this view
            12 * @param measureSpec A measureSpec packed into an int
            13 * @return The width of the view, honoring constraints from measureSpec
            14 */
            15private int measureWidth(int measureSpec) {
            16    int result = 0;
            17    int specMode = MeasureSpec.getMode(measureSpec);
            18    int specSize = MeasureSpec.getSize(measureSpec);
            19 
            20    if (specMode == MeasureSpec.EXACTLY) {
            21        // We were told how big to be
            22        result = specSize;
            23    } else {
            24        // Measure the text
            25        result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
            26                + getPaddingRight();
            27        if (specMode == MeasureSpec.AT_MOST) {
            28            // Respect AT_MOST value if that was what is called for by measureSpec
            29            result = Math.min(result, specSize);
            30        }
            31    }
            32 
            33    return result;
            34}

             

            直接看measureWidth()

            首先看到的是參數(shù),分別代表寬度和高度的MeasureSpec

            android2.2文檔中對(duì)于MeasureSpec中的說(shuō)明是:

            一個(gè)MeasureSpec封裝了從父容器傳遞給子容器的布局需求.

            每一個(gè)MeasureSpec代表了一個(gè)寬度,或者高度的說(shuō)明.

            一個(gè)MeasureSpec是一個(gè)大小跟模式的組合值.一共有三種模式.

            A MeasureSpec encapsulates the layout requirements passed from parent to child Each MeasureSpec represents a requirement for either the width or the height.A MeasureSpec is compsized of a size and a mode.There are three possible modes:

             (1)UPSPECIFIED :父容器對(duì)于子容器沒(méi)有任何限制,子容器想要多大就多大.
            UNSPECIFIED The parent has not imposed any constraint on the child.It can be whatever size it wants

             (2) EXACTLY

             父容器已經(jīng)為子容器設(shè)置了尺寸,子容器應(yīng)當(dāng)服從這些邊界,不論子容器想要多大的空間.

            EXACTLY The parent has determined and exact size for the child.The child is going to be given those bounds regardless of how big it wants to be.

            (3) AT_MOST

             子容器可以是聲明大小內(nèi)的任意大小.

            AT_MOST The child can be as large as it wants up to the specified size

            MeasureSpec是View類(lèi)下的靜態(tài)公開(kāi)類(lèi),MeasureSpec中的值作為一個(gè)整型是為了減少對(duì)象的分配開(kāi)支.此類(lèi)用于

            將size和mode打包或者解包為一個(gè)整型.

            MeasureSpecs are implemented as ints to reduce object allocation.This class is provided to pack and unpack the size,mode tuple into the int

            我比較好奇的是怎么樣將兩個(gè)值打包到一個(gè)int中,又如何解包.

            MeasureSpec類(lèi)代碼如下 :(注釋已經(jīng)被我刪除了,因?yàn)樵谏厦嬲f(shuō)明了.)

            01public static class MeasureSpec {
            02    private static final int MODE_SHIFT = 30;
            03    private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
            04 
            05    public static final int UNSPECIFIED = 0 << MODE_SHIFT;
            06    public static final int EXACTLY     = 1 << MODE_SHIFT;
            07    public static final int AT_MOST     = 2 << MODE_SHIFT;
            08 
            09    public static int makeMeasureSpec(int size, int mode) {
            10        return size + mode;
            11    }
            12    public static int getMode(int measureSpec) {
            13        return (measureSpec & MODE_MASK);
            14    }
            15    public static int getSize(int measureSpec) {
            16        return (measureSpec & ~MODE_MASK);
            17    }  }

            我無(wú)聊的將他們的十進(jìn)制值打印出來(lái)了:

            mode_shift=30,mode_mask=-1073741824,UNSPECIFIED=0,EXACTLY=1073741824,AT_MOST=-2147483648

            然后覺(jué)得也應(yīng)該將他們的二進(jìn)制值打印出來(lái),如下:

            mode_shift=11110, // 30

            mode_mask=11000000000000000000000000000000,

            UNSPECIFIED=0, 

            EXACTLY=1000000000000000000000000000000, 

            AT_MOST=10000000000000000000000000000000

             

            1MODE_MASK  = 0x3 << MODE_SHIFT //也就是說(shuō)MODE_MASK是由11左移30位得到的.因?yàn)镴ava用補(bǔ)碼表示數(shù)值.最后得到的值最高位是1所以就是負(fù)數(shù)了
            1 
            對(duì)于上面的數(shù)值我們應(yīng)該這樣想,不要把0x3看成3而要看成二進(jìn)制的11,

            而把MODE_SHIFF就看成30.那為什么是二進(jìn)制 的11呢?

            呢,因?yàn)橹挥腥髂J?如果有四種模式就是111了因?yàn)?11三個(gè)位才可以有四種組合對(duì)吧.

            我們這樣來(lái)看,

            UNSPECIFIED=00000000000000000000000000000000, 

                  EXACTLY=01000000000000000000000000000000, 

                AT_MOST=10000000000000000000000000000000

            也就是說(shuō),0,1,2

            對(duì)應(yīng)   00,01,10

            當(dāng)跟11想與時(shí)  00 &11 還是得到 00,11&01 -> 01,10&

            我覺(jué)得到了這個(gè)份上相信,看我博客的也都理解了.

             return (measureSpec & ~MODE_MASK);應(yīng)該是 return (measureSpec & (~MODE_MASK));

            posted on 2012-03-27 23:59 小果子 閱讀(19808) 評(píng)論(4)  編輯 收藏 引用 所屬分類(lèi): Android & Ios

            FeedBack:
            # re: android中onMeasure初看,深入理解布局之一!(轉(zhuǎn))
            2012-07-18 13:37 | 小果子
            @張?chǎng)?br>是的。  回復(fù)  更多評(píng)論
              
            # re: android中onMeasure初看,深入理解布局之一!(轉(zhuǎn))
            2012-10-08 18:31 | hello
            # re: android中onMeasure初看,深入理解布局之一!(轉(zhuǎn))
            2014-02-10 09:57 | JACK_WANG
            @張?chǎng)?br>measureSpec 的高16位表示mode,低16位表示size。  回復(fù)  更多評(píng)論
              
            欧美日韩中文字幕久久久不卡 | 久久精品国产亚洲精品| 亚洲综合日韩久久成人AV| 久久夜色撩人精品国产小说| 91精品国产91久久久久福利| 久久99精品久久只有精品| 久久午夜无码鲁丝片| 久久久久久亚洲AV无码专区| 久久久久高潮毛片免费全部播放| 久久精品视频一| 一本色道久久综合狠狠躁| 97久久国产综合精品女不卡| 99久久99久久精品国产片果冻| 亚洲欧洲中文日韩久久AV乱码| 亚洲精品国产第一综合99久久| 久久久久亚洲AV无码观看| 国产精品美女久久福利网站| 99精品国产99久久久久久97| 狠狠色丁香久久婷婷综合五月 | 久久亚洲春色中文字幕久久久| 国产精品久久久久国产A级| 久久综合综合久久97色| 91久久精品电影| 久久久一本精品99久久精品88| 久久久久久精品免费看SSS| 韩国免费A级毛片久久| 久久久久久久综合日本| 久久亚洲私人国产精品| 国产国产成人久久精品| 怡红院日本一道日本久久 | 日韩精品久久久久久久电影蜜臀| 欧美午夜精品久久久久免费视| 久久99国产精品久久99果冻传媒| 久久久综合香蕉尹人综合网| 欧美噜噜久久久XXX| 国产视频久久| 国产精品免费福利久久| 久久香综合精品久久伊人| 亚洲狠狠综合久久| 久久久久人妻一区精品性色av| 久久99精品免费一区二区|