View的工作原理(三)

一。Measure

View的Measure過程:

它會調用View的onMeasure方法,在onMeasure方法中會調用setMeasureDimension(width,height)方法設置view的寬高測量值,   而width和height的值在AT_MOST和EXACTLY模式下都等於specSize.(一般都是父容器剩餘空間大小)


所以如果我們自定義view時繼承View而不做任何改動,wrap_content和match_parent的表現效果是一樣的

我們如果要讓wrap_content生效,必須重寫onMeasure方法

TextView和ImageView的實現都針對wrap_content情形做了特殊處理。


ViewGroup的Measure過程:

viewGroup沒有實現onMeasure方法,

因爲不同的viewGroup的特點不同,如LinearLayout 和RelativeLayout,

只有具體的viewGroup自己實現。


二,layout

首先通過setFrame方法獲得View的四個頂點位置

然後調用onLayout方法確認其子元素的位置

由於不同的View和ViewGroup的特點不同,也要根據具體情況來實現它的onLayout方法


三,draw

執行步驟:繪製背景(background.draw(canvas))

                  繪製自己(onDraw)

                  繪製children(dispatchDraw)

                  繪製裝飾(onDrawScrollBars)

dispatchDraw方法會遍歷子元素的draw方法,以此一層一層完成view樹的繪製


由於view的measure,layout,draw過程和activity的onCreate,onStar,onResume並不同步

所以在activity的這些回調方法中調用view.getMeasuredHeight()來得到View的高度時,可能會因爲

View還沒有完成layout,使得得到的值爲0

那在什麼時候什麼地方調用View.getMeasuredHeight()可以確保返回值爲View的高度呢

方法如下:

1.onWindowFocusChanged(該方法會在activity的窗口得到焦點和失去焦點時均被調用一次

public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
}
}

2.view.post(runnable)

通過將一個runnable放在消息隊列的最後,那runnable的執行時間就在view完成初始化以後

view.post(new Runnable(){
@Override
public void run(){
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
}
}

3.ViewTreeObserver(在View樹的狀態發生改變或其中View的可見性發生改變時,onGlobalLayout方法將被回調)

ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout(){
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = view.getMeasuredWidth();
int height = view.getMeasureHeight();
}
}


參考:android開發藝術探索


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章