Android视图加载流程(5)之View的详细绘制流程Layout

流程图

Android视图加载流程(4)之View的详细绘制流程Measure

上一篇文章我们对View的测量(Measure)进行讲解了。接着我们开始聊布局(Layout),以下是我们熟悉的performTraversals方法。

private void performTraversals() {
    ......
    mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    ......
    //本文重点
    mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
    ......
    mView.draw(canvas);
    ......
} 

可以看见layout方法接收了4个参数,分别表示左、上、右、下。从上可以看出mView(DecorView)的左上都为0,右为width,下为height。

理解图 本文重点Layout

源码解读

Step1 ViewGroup

mView即DecorView(FrameLayout)是ViewGroup的子类,我们来看下ViewGroup的layout方法。

@Override
public final void layout(int l, int t, int r, int b) {
    ......
    super.layout(l, t, r, b);
    ......
}

ViewGroup的layout实际调用了父类(View)的layout方法。且layout由final修饰,所以ViewGroup的所有子类都不能重写改方法。

Step2 View

public void layout(int l, int t, int r, int b) {
    ......
    //实质都是调用setFrame方法把参数分别赋值给mLeft、mTop、mRight和mBottom这几个变量
    //判断View的位置是否发生过变化,以确定有没有必要对当前的View进行重新layout
    boolean changed = isLayoutModeOptical(mParent) ?
            setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
    //需要重新layout
    if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
        //回调onLayout
        onLayout(changed, l, t, r, b);
        ......
    }
    ......
}
//空方法
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}

layout完成后,随即调动用onLayout。这里与measure类似。View的onlayout是一个空方法。

Step3 ViewGroup

@Override
protected abstract void onLayout(boolean changed,
        int l, int t, int r, int b);

ViewGrpup的onLayout为抽象方法,也就意味着所有ViewGroup的子类都得实现onLayout方法。重写onLayout的目的就是安排其children在父View的具体位置。重写onLayout通常做法就是写一个for循环调用每一个子视图的layout(l,t,r,b)方法,通过传入不同的数据来排列所有的子视图。(这里可以参考onMeasure)

Step4 FrameLayout

我们以DecorView(FrameLayout)为例子来解析一下具体的onLayout是如何实现的?

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    layoutChildren(left, top, right, bottom, false /* no force left gravity */);
}
//具体的实现方法
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
    //获取子视图的个数
    final int count = getChildCount();
    
    //计算父视图的左、右、上下
    final int parentLeft = getPaddingLeftWithForeground();
    final int parentRight = right - left - getPaddingRightWithForeground();
    final int parentTop = getPaddingTopWithForeground();
    final int parentBottom = bottom - top - getPaddingBottomWithForeground();
    //开始遍历!
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            //获取子视图的布局参数
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            //获取子视图的宽高
            final int width = child.getMeasuredWidth();
            final int height = child.getMeasuredHeight();

            int childLeft;
            int childTop;
                
            int gravity = lp.gravity;
            if (gravity == -1) {
                gravity = DEFAULT_CHILD_GRAVITY;
            }

            final int layoutDirection = getLayoutDirection();
            final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
            final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
                //判断水平方向,给子视图的left赋值
            switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                    lp.leftMargin - lp.rightMargin;
                    break;
                case Gravity.RIGHT:
                    if (!forceLeftGravity) {
                        childLeft = parentRight - width - lp.rightMargin;
                        break;
                    }
                case Gravity.LEFT:
                default:
                    childLeft = parentLeft + lp.leftMargin;
            }
            //判断竖直方向,给子视图top赋值
            switch (verticalGravity) {
                case Gravity.TOP:
                    childTop = parentTop + lp.topMargin;
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                    lp.topMargin - lp.bottomMargin;
                    break;
                case Gravity.BOTTOM:
                    childTop = parentBottom - height - lp.bottomMargin;
                    break;
                default:
                    childTop = parentTop + lp.topMargin;
            }
            //左、上坐标已知,宽高已知,可得右、下坐标。排列子视图
            child.layout(childLeft, childTop, childLeft + width, childTop + height);
        }
    }
}

从上面对DecorView(FrameLayout)的onLaout分析可看出。DecorView通过获取子视图(measure)的宽高,且通过gravity计算出子视图对应的左、上坐标,从而得出右、下左标。从而实现一个子视图的布局。

当然各种ViewGroup有各种onLayout的实现,本文这里简单讲解FrameLayout。

额外 extra

我们以前遇到getWidth()、getHeight()和getMeasureWidth()、getMeasureHeight()傻傻不能分清楚,那他们的区别是什么呢?

public final int getMeasuredWidth() {
    return mMeasuredWidth & MEASURED_SIZE_MASK;
}

public final int getMeasuredHeight() {
    return mMeasuredHeight & MEASURED_SIZE_MASK;
}

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

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

public final int getLeft() {
    return mLeft;
}

public final int getRight() {
    return mRight;
}

public final int getTop() {
    return mTop;
}

public final int getBottom() {
    return mBottom;
}

getMeasureWith(),getMeasureHeight()我们的上文已经说过了,必须在onMeasure之后才有效。而getWith()和getHeight()要在onLayout之后才有效。

总结 Summary

通过以上几个步骤的分析,布局(Layout)的流程基本与Measure类似。通过循环调用layout排列各个子视图

  1. View.layout可重写,ViewGroup.layout不可重写。ViewGroup.onlayout为抽象方法,子类必须实现该方法。
  2. measure操作后得到的是measureWidth和measureHeight;layout操作后得到的是坐标left,top,right,bottom,当然这些值是相对于父View来说的
  3. getWith()和getHeight()方法必须在onLayout后调用才有值

Android视图加载流程(6)之View的详细绘制流程Draw


PS:本文整理自以下文章,若有发现问题请致邮 caoyanglee92@gmail.com

工匠若水 Android应用层View绘制流程与源码分析

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

推荐阅读更多精彩内容