FrameLayout源碼淺析

FrameLayout是一個(gè)ViewGroup。在ViewGroup最重要的兩步方法是測(cè)量和布局:onMeasure()、onLayout()方法。所以這里只分析FrameLayout的onMeasure()、onLayout()方法。

FrameLayout的特征:
FrameLayout的所有子View一般都是在容器的左上方,除非子View有設(shè)置layout_gravity這個(gè)屬性。

FrameLayout的onMeasure()源碼:
流程:FrameLayout的onMeasure(),會(huì)先遍歷所有子View,并為每個(gè)子View確定MeasureSpec,計(jì)算出子view中最大的寬度和高度,并把寬或高是match_parent的子view放進(jìn)一個(gè)List集合中。把子view的最大寬高度+padding,然后調(diào)用setMeasuredDimension()方法,根據(jù)frameLayout的MeasureSpec和最大寬高度確認(rèn)FrameLayout的大小。之后把剛剛match_parent的子view集合遍歷,根據(jù)已經(jīng)確定了FrameLayout的大小再重新計(jì)算設(shè)置這個(gè)子View的MeasureSpec

private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1);
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      int count = getChildCount();
      //是否大小已經(jīng)確定,不需要子view的和大小來確定
      final boolean measureMatchParentChildren =
              MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
              MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
      //父類可以會(huì)多次調(diào)用onMeasure,為了避免數(shù)據(jù)疊加,每次先清空數(shù)據(jù)
      mMatchParentChildren.clear();

      int maxHeight = 0;
      int maxWidth = 0;
      int childState = 0;

      //測(cè)量所有子View,并記錄最大的寬高
      for (int i = 0; i < count; i++) {
          final View child = getChildAt(i);
          if (mMeasureAllChildren || child.getVisibility() != GONE) {
              //這里是設(shè)置子view的measureSpec,并調(diào)用子view的onmeasure方法,與getChildMeasureSpec()不同的是,這個(gè)measureChildMargin()方法把margin也去除了。
              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());
              //
              if (measureMatchParentChildren) {
                  if (lp.width == LayoutParams.MATCH_PARENT ||
                          lp.height == LayoutParams.MATCH_PARENT) {
                      mMatchParentChildren.add(child);
                  }
              }
          }
      }

      //最大寬高加上padding
      // Account for padding too
      maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
      maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();

      // Check against our minimum height and width
      maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
      maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

      // Check against our foreground's minimum height and width
      final Drawable drawable = getForeground();
      if (drawable != null) {
          maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
          maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
      }

      //主要處理viewGroup的at_most、unspecified兩種模式的大小,at_most:大小不能超過父viewGroup默認(rèn)大小,unspecified默認(rèn)是子view所需的大小
      setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
              resolveSizeAndState(maxHeight, heightMeasureSpec,
                      childState << MEASURED_HEIGHT_STATE_SHIFT));

      //將所有子view的寬高的是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);
              }

              //給match_parent的子view重新設(shè)置大小
              //這里又調(diào)用了子View的measure()方法,在上面的確定子view的measureSpec已經(jīng)調(diào)用了一次,所以子view可能會(huì)被父view調(diào)用多次onMeasure(),要記得數(shù)據(jù)清楚。
              child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
          }
      }
  }

FrameLayout的match_parent的子view會(huì)被調(diào)用兩次onMeasure,所以再onMeasure()要注意數(shù)據(jù)的清楚,F(xiàn)rameLayout這里onMeasure也是每調(diào)用一次就會(huì)清除List的集合。大概實(shí)現(xiàn)思路是:先記錄子View的最大寬高->確定FrameLayout自己的大小->重新設(shè)置width、height是match_parent的大小。

FrameLayout的onLayout()源碼:
onLayout布局再FrameLayout的大小確定之后,因?yàn)槠渥覸iew不用涉及到換行等問題,一般子view如果不設(shè)置gravity的屬性,默認(rèn)是在FrameLayout左上顯示。所以這里的布局,只需要判斷gravity的屬性,然后根據(jù)屬性值來動(dòng)態(tài)確認(rèn)left和top的位置

//frameLayout所有的子View都是從容器左上方開始,除了一個(gè)gravity
  @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;

              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;
              }

              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);
          }
      }
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容