View的工作流程 源码分析

View的工作流程是指measure、layout、draw三大流程,即策略、布局、重绘。

一.Measure过程

1.view的Measure过程

1.在onMeasure调用setMeasuredDimension( )设置View的宽和高.

2.在setMeasuredDimension()中调用getDefaultSize()获取View的宽和高.

3.在getDefaultSize()方法中又会调用到getSuggestedMinimumWidth()或者getSuggestedMinimumHeight()获取到View宽和高

View的onMeasure方法()

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
            getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}

setMeasuredDimension()方法会设置View的宽和高,getDefaultSize()返回View测量后的大小(注:View的最终大小是在layout阶段确定的

 public static int getDefaultSize(int size, int measureSpec) {
    int result = size;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    switch (specMode) {
    case MeasureSpec.UNSPECIFIED:
        result = size;
        break;
    case MeasureSpec.AT_MOST:
    case MeasureSpec.EXACTLY:
        result = specSize;
        break;
    }
    return result;
}

getSuggestedMinimumWidth()方法

 protected int getSuggestedMinimumWidth() {
    return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}

如果View没有设置背景,那么View的宽度为mMinWidth,而mMinWidth对应于android:minWidth这个属性所指的的值,如果这个属性不指定,那么mMinWidth默认值为0;如果设置了背景则返回android:minWidth和背景的最小宽度这两者中的最大值,getSuggestedMinimumHeight()实现的原理也类似。

解决在布局中使用wrap_content,效果是match_parent的问题。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec , heightMeasureSpec);  
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);  
int widthSpceSize = MeasureSpec.getSize(widthMeasureSpec);  
int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec);  
int heightSpceSize=MeasureSpec.getSize(heightMeasureSpec);  

if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode==MeasureSpec.AT_MOST){  
    setMeasuredDimension(mWidth, mHeight);  
}else if(widthSpecMode==MeasureSpec.AT_MOST){  
    setMeasuredDimension(mWidth, heightSpceSize);  
}else if(heightSpecMode==MeasureSpec.AT_MOST){  
    setMeasuredDimension(widthSpceSize, mHeight);  
}  
} 

在上面的代码中,我们只需要给View指定一个默认的宽高(mWidth, mHeight),并在wrap_content时设置此宽/高即可。对于非wrap_content的情形,使用系统的测量值就行,指定一个默认的宽高(mWidth, mHeight)根据需要自行指定。

2.viewGroup的Measure过程

对于ViewGroup来说,除了完成自己的Measure过程,还回遍历去调用所有子元素的measure方法,各个子元素在递归的执行这个过程。

ViewGroup是一个抽象类,没有重写View的onMeasure方法,但是它提供了一个measureChildren方法。这是因为不同的ViewGroup子类有不同的布局特性,导致他们的测量细节各不相同,比如LinearLayout和RelativeLayout,因此ViewGroup没办法同一实现onMeasure方法。 measureChildren方法的流程:

取出子View的LayoutParams

通过getChildMeasureSpec方法来创建子元素的MeasureSpec

将MeasureSpec直接传递给View的measure方法来进行测量

   protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
    final int size = mChildrenCount;
    final View[] children = mChildren;
    for (int i = 0; i < size; ++i) {
        final View child = children[i];
        if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
        }
    }
 }

 protected void measureChild(View child, int parentWidthMeasureSpec,
        int parentHeightMeasureSpec) {
    final LayoutParams lp = child.getLayoutParams();

    final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
            mPaddingLeft + mPaddingRight, lp.width);
    final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
            mPaddingTop + mPaddingBottom, lp.height);

    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

可以查看一下LinearLayout 的onMeasure方法

Measure完成以后就可以在onLayout()通过getMeasuredWidth/Height获取到View的测量宽高,

二.Layout过程

Layout的作用是ViewGroup用来确定子元素的位置,当ViewGroup位置确定以后,他就会在onLayout中遍历所有子元素并调用其layout过程,在layout方法中onLayout方法又会被调用。layout方法确定view本身的位置,onlayout方法则确定所有子元素的位置。
View的layout源码

public void layout(int l, int t, int r, int b) {
    if ((mPrivateFlags3 & PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT) != 0) {
        onMeasure(mOldWidthMeasureSpec, mOldHeightMeasureSpec);
        mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
    }

    int oldL = mLeft;
    int oldT = mTop;
    int oldB = mBottom;
    int oldR = mRight;

    boolean changed = isLayoutModeOptical(mParent) ?
            setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);

    if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
        onLayout(changed, l, t, r, b);
        mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;

        ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnLayoutChangeListeners != null) {
            ArrayList<OnLayoutChangeListener> listenersCopy =
                    (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
            int numListeners = listenersCopy.size();
            for (int i = 0; i < numListeners; ++i) {
                listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
            }
        }
    }

    mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
    mPrivateFlags3 |= PFLAG3_IS_LAID_OUT;
}

layout方法的大致流程:

1.通过setFrame方法来设定View的四个顶点的位置,既初始化mLeft、mTop、mBottom、mRight这四个值,View的四个顶点位置确认了,那么View在父容器中的位置也就确认了。
2.调用onLayout方法,父容器确定子元素的位置,(同onMeasure,View 和ViewGroup 均没有真正实现onLayout,而是放在具体的子类如Linearlayout 中去实现)

View的测量宽/高和最终宽/高的区别?
这个问题可以具体为:View的getMeasuredWidth和getWidth;View的getMeasuredHeight和getHeight有什么区别

public final int getWidth() {
    return mRight - mLeft;
}

public final int getHeight() {
    return mBottom - mTop;
}

在日常开发中,我们认为这两个值相等。

三.draw过程

View的绘制过程遵循如下几步:

(1)绘制背景drawBackground(canvas)

(2)绘制自己onDraw

(3)绘制childrendispatchDraw遍历所有子View的draw方法

(4)绘制装饰onDrawScrollBars

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容