天天看點

Android自定義控件onMeasure、onLayout介紹 一、onMeasure()方法 二、onLayout       onLayout(boolean changed, int left, int top,int right,int bottom);

Android中自定義View時經常會需要重寫View的onMeasure(),onLayout()方法。

onMeasure()主要是用來測量控件的大小位置,而onLayout()主要用來布局控件,繪制控件的位置面。

分别介紹下這兩個方法。

一、onMeasure()方法

onMeasure(int widthMeasureSpec,int heightMeasureSpec)

1、調用時間:當控件的父元素放置該控件時,用于告訴父元素該控件需要的大小。

2、傳入參數:widthMeasureSpec,heightMeasureSpec。這兩個傳入參數由高32位和低16位組成,高32位儲存的值叫specMode,可以通過MeasureSpec.getMode()擷取;低16位為specSize可以由MeasureSpec.getSize()擷取。這兩個值是由ViewGroup中的layout_width,layout_height和padding以及View自身的layout_margin共同決定。權值weight也是尤其需要考慮的因素,有它的存在情況可能會稍微複雜點。

specMode可以取三個值:MeasureSpec.EXACTLY ,MeasureSpec.AT_MOST,MeasureSpec.UNSPECIFIED;specMode與layout_的對應關系如下:

match_parent- MeasureSpec.EXACTLY:當layout_為match_parent或者為某一具體值的時候specMode為EXACTLY代表精确的值;

wrap_content- MeasureSpec.AT_MOST:表示能獲得的最大尺寸;

當無法确定尺寸的時候則是 MeasureSpec.UNSPECIFIED,這時候specSize會為最小值(即0),也可以設定一個預設的最小值;

3、可以在onMeasure()中來計算控件的尺寸,然後根據setMeasuredDimension(mWidth,mHeight);方法來告訴父控件此控件需要的尺寸,onMeasure()方法中必須調用此方法。

4、值得注意的是:

1)specSize和傳入setMeasuredDimension()方法中的值的機關都是px(dp*density就是px)。

2)match_parent并不是填充整個父容器,而是在不覆寫已經加入父容器的控件的情況下填充父容器。

例如:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
    height = 0;
    switch (heightSpecMode) {
        case MeasureSpec.AT_MOST: // wrap_content時,子最大可以達到的指定大小
            height = (int) dp2px(DEFAULT_HEIGHT_DP);
            break;
        case MeasureSpec.EXACTLY: // 指定View的高寬或者match_parent時,模式為EXACTLY
        case MeasureSpec.UNSPECIFIED: // 父不沒有對子施加任何限制
            height = heightSpecSize;
            break;
    }
    setMeasuredDimension(widthSpecSize, height);
}      

當有多個子控件時:

//我這裡每行隻放一個View,
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentWidthSize = 0;
    int paretnHeightSize = 0;
    for (int i = 1; i < getChildCount(); i++) {
        View childView = getChildAt(i);
        // 設定子view寬、高
        childView.measure(
                MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY));
        parentWidthSize = childView.getMeasuredWidth();
        paretnHeightSize += childView.getMeasuredHeight();
    }
    setMeasuredDimension(parentWidthSize, paretnHeightSize);
}      

二、onLayout  

    onLayout(boolean changed, int left, int top,int right,int bottom);

    父容器的onLayout()調用子類的onLayout()來确定子view在viewGroup中的位置,如:onLayout(10,10,100,100)表示子容器在父容器中(10,10)位置顯示,長、寬都是90。結合onMeasure()方法使用可以确定子view的布局。

其中的

l是childView在這個自定義Viewgroup中距離左邊的距離,

t是距離上邊的距離,

r是自定義Viewgroup中多寬,

b是自定義Viewgroup中多高。

可以這樣了解,把這個子View看作一個矩形,那麼就可以把l、t看作是矩形左上角的坐标,r、b看作是右下角的坐标,這樣比較好了解

  當隻有一個子控件的時候:

@Override

protected

void

onLayout(

boolean

changed, 

int

l, 

int

t, 

int

r, 

int

b) {

View childView = getChildAt(i);

int

Width = childView.getMeasuredWidth();

int

height = childView.getMeasuredHeight();

childView.layout(

l

t

, childWidth , height );

}

  當有多個子控件的時候:

@Override

protected

void

onLayout(

boolean

changed, 

int

l, 

int

t, 

int

r, 

int

b) {

for

(

int

i = 

1

; i < getChildCount(); i++) {

View childView = getChildAt(i);

int

Width = childView.getMeasuredWidth();

int

height = childView.getMeasuredHeight();

childView.layout(

, Width , height );

}

}

當然,也可以不用for循環來布局子控件,如在繪制彈性上下拉控件時:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (!isLayout) {
        // 這裡是第一次進來的時候做一些初始化
        mHeadView = new LoadingLayout(mContext, getChildAt(0), true);
        pullableView = getChildAt(1);
        mFootView = new LoadingLayout(mContext, getChildAt(2), false);
        isLayout = true;
        refreshDist = ((ViewGroup) mHeadView.rootView).getChildAt(0)
                .getMeasuredHeight();
        loadmoreDist = ((ViewGroup) mFootView.rootView).getChildAt(0)
                .getMeasuredHeight();
    }
    // 改變子控件的布局,這裡直接用(pullDownY + pullUpY)作為偏移量,這樣就可以不對目前狀态作區分
    mHeadView.rootView.layout(0,
            (int) (pullDownY + pullUpY) - mHeadView.rootView.getMeasuredHeight(),
            mHeadView.rootView.getMeasuredWidth(), (int) (pullDownY + pullUpY));
    pullableView.layout(0, (int) (pullDownY + pullUpY),
            pullableView.getMeasuredWidth(), (int) (pullDownY + pullUpY)
                    + pullableView.getMeasuredHeight());
    mFootView.rootView.layout(0,
            (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight(),
            mFootView.rootView.getMeasuredWidth(),
            (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight()
                    + mFootView.rootView.getMeasuredHeight());
}      
pullableView是我們的彈性控件,可以是listview或者scrollview;      
mFootView是底部控件;      
mHeadView是頭部控件;      
通過不斷的改變pullDownY或者pullUpY,然後不斷的重繪視圖,來達到彈性效果。