FrameLayout源码分析

简介

FrameLayout是层级布局,即所有的子View都是从底层向上层开始,默认不指定margin或者layout_gravity的话,每个子view的坐标起始坐标都是Framelayout的起始坐标

public class FrameLayout extends ViewGroup {
    ...
}

FrameLayout就是直接继承自ViewGroup的

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int count = getChildCount();
    //判断FrameLayout 宽 高 是否同时设置了Match_parent或者 设置了指定大小的宽高
    // measureMatchParentChildren 为 true,则表示没有设置了Match_parent或者 设置了指定大小的宽高
       final boolean measureMatchParentChildren =
               MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
               MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
       mMatchParentChildren.clear();

       int maxHeight = 0;
       int maxWidth = 0;
       int childState = 0;
    //遍历所有子View计算出所有的子View中的最大宽度 maxWidth 和最大高度 maxHeigh
       for (int i = 0; i < count; i++) {
           final View child = getChildAt(i);
           if (mMeasureAllChildren || child.getVisibility() != GONE) {
               //计算子View的宽高,包含了子View的左右上下Margin,直接调用ViewGroup中的方法
               measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
               final LayoutParams lp = (LayoutParams) child.getLayoutParams();
               maxWidth = Math.max(maxWidth,
                       child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
               maxHeight = Math.max(maxHeight,
                       child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
               childState = combineMeasuredStates(childState, child.getMeasuredState());
               // FrameLayout没有设置固定大小的宽高或Match_parent高宽,则收集所有设置了Match_parent的子View 
               if (measureMatchParentChildren) {
                   if (lp.width == LayoutParams.MATCH_PARENT ||
                           lp.height == LayoutParams.MATCH_PARENT) {
                       mMatchParentChildren.add(child);
                   }
               }
           }
       }
       // 加上padding
       maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
       maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();

       maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
       maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

       final Drawable drawable = getForeground();
       if (drawable != null) {
           maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
           maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
       }
    //设置自身的宽高
       setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
               resolveSizeAndState(maxHeight, heightMeasureSpec,
                       childState << MEASURED_HEIGHT_STATE_SHIFT));
    ...
   }

先看前部分,根据FrameLayout 宽 高 是否同时设置了Match_parent或者 设置了指定大小来设置一个Boolean值,然后遍历所有的子View,计算出最大宽度和最大高度,同时调用ViewGroup中的方法进行子View的测量;如果FrameLayout没有设置为match_parent或者固定的值,则会存储是match_parent的所有子View ,接着就是加上padding值,然后设置自己的宽高。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       ...
       // 如果FrameLayout没有同时设置固定大小或者Match_parent宽高,且有子View设置了Match_parent宽高,则重新测量 设置了Match_Parent宽高的子View
       count = mMatchParentChildren.size();
       if (count > 1) {
           for (int i = 0; i < count; i++) {
               final View child = mMatchParentChildren.get(i);
               final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

               final int childWidthMeasureSpec;
               if (lp.width == LayoutParams.MATCH_PARENT) {
                   final int width = Math.max(0, getMeasuredWidth()
                           - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
                           - lp.leftMargin - lp.rightMargin);
                   childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                           width, MeasureSpec.EXACTLY);
               } else {
                   childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                           getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
                           lp.leftMargin + lp.rightMargin,
                           lp.width);
               }

               final int childHeightMeasureSpec;
               if (lp.height == LayoutParams.MATCH_PARENT) {
                   final int height = Math.max(0, getMeasuredHeight()
                           - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
                           - lp.topMargin - lp.bottomMargin);
                   childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                           height, MeasureSpec.EXACTLY);
               } else {
                   childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                           getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
                           lp.topMargin + lp.bottomMargin,
                           lp.height);
               }
            
               child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
           }
       }
}

后面部分就是判断是否需要重新测量,如果FrameLayout没有同时设置固定大小或者Match_parent宽高,且有子View设置了Match_parent宽高,则重新测量 设置了Match_Parent宽高的子View

onLayout

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();
    //遍历子View
       for (int i = 0; i < count; i++) {
           final View child = getChildAt(i);
           //同样,View的可见性不为GONE才会计算在内
           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;
            // 根据水平方向Gravity,计算View的左右,坐标
               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;
               }
            // 根据垂直方向Gravity,计算View的上下,坐标
               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);
           }
       }
   }

onLayout方法主要就是对子View进行layout
在对子View进行layout的过程中,主要就是通过遍历所有的子View,然后根据水平和垂直两个方向计算view具体的坐标,最后通过子View本身的layout方法进行放置。因为每个view在不同的层级,所以只需要计算每个View的Layout_gravity,默认的Layout_gravity 为Gravity_left | GravityTop, 在根据Gravity计算时,分别通过水平方向的Gravity计算出 左右 坐标,然后根据垂直方向的Gravity计算出上下坐标。

onDraw

FrameLayout没有自己实现onDraw方法

FrameLayout、LinearLayout和RelativeLayout的性能对比

当RelativeLayout和LinearLayout作为ViewGroup表达相同的布局的时候,谁的绘制更快一些,性能相对更好一些?
通过网上的很多实验结果我们得之,两者绘制同样的界面时layout和draw的过程时间消耗相差无几,关键在于measure过程

  • LinearLayout:
    在没有权重的情况下,就只会单纯的遍历一个方向,遍历一次所有的View;如果View设置了权重 ,那么在第一次遍历的时候这个View是不会进行测量的,在第二次测量(专门用于测量权weight重的);所以无权重一次遍历,有权重两次遍历。

  • RelativeLayout:因为依赖关系,所以在进行排序后,分别会对水平、垂直方向进行遍历,所以两次遍历。

  • FrameLayout:某种情况上来说,FrameLayout也可能导致二次测量,不过FrameLayout的二次测量就只针对View为match_parent的了。

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

推荐阅读更多精彩内容