ViewGroup的测量及绘制
ViewGroup
extends Viewimplements ViewManager ViewParent
| java.lang.Object | ||
| ???? | android.view.View | |
| ? | ???? | android.view.ViewGroup |
Class Overview
A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.
對于ViewGroup來說除了完成自身的measure過程外,還要遍歷去調用所有子元素的measure方法,各子元素再遞歸去執行這個過程。ViewGroup提供了一個measureChildren方法:
protected voidmeasureChildren (int widthMeasureSpec, int heightMeasureSpec)
Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding. We skip children that are in the GONE state The heavy lifting is done in getChildMeasureSpec.
Parameters
| The width requirements for this view |
| The height requirements for this view |
?
從上面的源碼看,ViewGroup在measure時,會對每一個元素進行measure。
measureChild方法:
protected void measureChild(View child,int parentWidthMeasureSpec,int parentHeightMeasureSpec){final LayoutParams lp = child.getLayoutParams();final int childWidthMeasureSpec = getChildMeasureSpec(parentWidth - MeasureSpec,mPaddingLeft + mPaddingRight,lp.width);final int childHeightMeasureSpec = getChildMeasureSpec(parentHeight - MeasureSpec,mPaddingTop + mPaddingBottom,lp.height);child.measure(childWidthMeasureSpec, childHeightMeasureSpec); }
measureChild的思想就是取出子元素的LayoutParams,再通過getChildMeasureSpec來獲取子元素的MeasureSpec,接著直接將MeasureSpec傳遞給View的measure方法進行測量。
?
2、ViewGroup的繪制
?
ViewGroup通常情況下不需要繪制,如果不用指定ViewGroup的背景顏色,其onDraw()方法都不會被調用。ViewGroup會使用dispatchDraw()方法繪制其子View,過程同樣是遍歷所有子View,并調用子View的繪制方法來完成繪制。
總結
以上是生活随笔為你收集整理的ViewGroup的测量及绘制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消防二次备案需要什么资料(消防二次备案)
- 下一篇: View的事件分发机制简述