ViewPager源码分析(1):onMeasure、onLayout
我的简书同步发布:ViewPager源码分析(1):onMeasure、onLayout
转载请注明出处:【huachao1001的专栏:http://blog.csdn.net/huachao1001】
在网上看了一些面经,感觉ViewPager被问到的概率还是蛮大的,于是决定去好好研究ViewPager源码,一步一步去琢磨ViewPager的实现,并写到博文里来~。
我们知道,ViewPager是一个ViewGroup,而我们平时自己自定义ViewGroup时,除了至少写两个构造函数以外,onMeasure和onLayout这两个函数基本上是必须要去写的。今天先把onMeasure和onLayout两个函数仔细研究研究~
onMeasure
ViewPager将子View分为两种,一种是Decor View 用于装饰ViewPager,它可能需要占用一些空间;另一种是普通的子View,也就是我们横滑时显示的各个View。onMeasure首先是对Decor View进行测量,然后再对普通的子View进行测量。详细的注释如下:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //根据布局文件,设置尺寸信息,默认大小为0 setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); final int measuredWidth = getMeasuredWidth(); final int maxGutterSize = measuredWidth / 10; //设置mGutterSize的值,后面再讲mGutterSize mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize); // ViewPager的显示区域只能显示对于一个View //childWidthSize和childHeightSize为一个View的可用宽高大小 //即去除了ViewPager内边距后的宽高 int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight(); int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); //1.先对Decor View进行测量 //下面这个循环是只针对Decor View的,即用于装饰ViewPager的View int size = getChildCount(); for (int i = 0; i < size; ++i) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); //1.1 如果该View是Decor View,即用于装饰ViewPager的View if (lp != null && lp.isDecor) { //1.2 获取Decor View的在水平方向和竖直方向上的Gravity final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK; final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK; //1.3 默认Dedor View模式对应的宽高是wrap_content int widthMode = MeasureSpec.AT_MOST; int heightMode = MeasureSpec.AT_MOST; //1.4 记录Decor View是在垂直方向上还是在水平方向上占用空间 boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM; boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT; //1.5 consumeHorizontal:如果是在垂直方向上占用空间, // 那么水平方向就是match_parent,即EXACTLY //而垂直方向上具体占用多少空间,还得由Decor View决定 //consumeHorizontal也是同理 if (consumeVertical) { widthMode = MeasureSpec.EXACTLY; } else if (consumeHorizontal) { heightMode = MeasureSpec.EXACTLY; } //1.6 宽高大小,初始化为ViewPager可视区域中子View可用空间 int widthSize = childWidthSize; int heightSize = childHeightSize; //1.7 如果宽度不是wrap_content,那么width的测量模式就是EXACTLY //如果宽度既不是wrap_content又不是match_parent,那么说明是用户 //在布局文件写的具体的尺寸,直接将widthSize设置为这个具体尺寸 if (lp.width != LayoutParams.WRAP_CONTENT) { widthMode = MeasureSpec.EXACTLY; if (lp.width != LayoutParams.FILL_PARENT) { widthSize = lp.width; } } //1.8 同1.7 if (lp.height != LayoutParams.WRAP_CONTENT) { heightMode = MeasureSpec.EXACTLY; if (lp.height != LayoutParams.FILL_PARENT) { heightSize = lp.height; } } //1.9 合成Decor View的宽高specification(包含尺寸和模式的整数) final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode); final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode); //1.10 对DecorView进行测量 child.measure(widthSpec, heightSpec); //1.11 如果Decor View占用了ViewPager的垂直方向的空间 //需要将子View的竖直方向可用的空间减去DecorView的高度, //同理,水平方向上也做同样的处理 if (consumeVertical) { childHeightSize -= child.getMeasuredHeight(); } else if (consumeHorizontal) { childWidthSize -= child.getMeasuredWidth(); } } } } //2.子View默认宽高的specification(包含尺寸和模式的整数) //(PS:mChildWidthMeasureSpec并没有再次用到,个人感觉有点多余) mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY); mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY); //3.确保我们需要显示的fragment已经被我们创建好了 //populate()比较复杂,后面再详细介绍 mInLayout = true; populate(); mInLayout = false; // 4.再对子View进行测量 size = getChildCount(); for (int i = 0; i < size; ++i) { final View child = getChildAt(i); //4.1 visibility为GONE的无需测量 if (child.getVisibility() != GONE) { if (DEBUG) Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec); //4.2 获取子View的LayoutParams final LayoutParams lp = (LayoutParams) child.getLayoutParams(); //4.3 只针对子View而不对Decor View测量 if (lp == null || !lp.isDecor) { //4.4 LayoutParams的widthFactor是取值为[0,1]的浮点数, // 用于表示子View占ViewPager显示区域中子View可用宽度的比例, // 即(childWidthSize * lp.widthFactor)表示当前子View的实际宽度 final int widthSpec = MeasureSpec.makeMeasureSpec( (int) (childWidthSize * lp.widthFactor), MeasureSpec.EXACTLY); //4.5 对当前子View进行测量 child.measure(widthSpec, mChildHeightMeasureSpec); } } } }
onLayout
我们知道ViewPager的子View是水平摆放的,所以在onLayout中,大部分工作的就是计算childLeft,即子View的左边位置,而顶部位置基本上是一样的,从以下代码中可以体现的出来:
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { //1.以下局部变量很简单,不再解释 final int count = getChildCount(); int width = r - l; int height = b - t; int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); int paddingRight = getPaddingRight(); int paddingBottom = getPaddingBottom(); final int scrollX = getScrollX(); //2.Decor View 数量 int decorCount = 0; //3.首先对Decor View进行layout,再对普通子View进行layout, // 之所以先对Decor View布局,是为了让普通子View能有合适的偏移 //下面循环主要是针对Decor View for (int i = 0; i < count; i++) { final View child = getChildAt(i); //3.1 visibility不为GONE才layout if (child.getVisibility() != GONE) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); //3.2 左边和顶部的边距初始化为0 int childLeft = 0; int childTop = 0; if (lp.isDecor) {//3.3 只针对Decor View //3.4 获取水平或垂直方向上的Gravity final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK; final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK; //3.5 根据水平方向上的Gravity,确定childLeft以及paddingRight switch (hgrav) { default://没有设置水平方向Gravity时(左中右),childLeft就取paddingLeft childLeft = paddingLeft; break; case Gravity.LEFT://水平方向Gravity为left,Decor View往最左边靠 childLeft = paddingLeft; paddingLeft += child.getMeasuredWidth(); break; case Gravity.CENTER_HORIZONTAL://将Decor View居中摆放 childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft); break; case Gravity.RIGHT://将Decor View往最右边靠 childLeft = width - paddingRight - child.getMeasuredWidth(); paddingRight += child.getMeasuredWidth(); break; } //3.6 与3.5同理 switch (vgrav) { default: childTop = paddingTop; break; case Gravity.TOP: childTop = paddingTop; paddingTop += child.getMeasuredHeight(); break; case Gravity.CENTER_VERTICAL: childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop); break; case Gravity.BOTTOM: childTop = height - paddingBottom - child.getMeasuredHeight(); paddingBottom += child.getMeasuredHeight(); break; } //3.7 上面计算的childLeft是相对ViewPager的左边计算的, //还需要加上x方向已经滑动的距离scrollX childLeft += scrollX; //3.8 对Decor View布局 child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight()); //3.9 将Decor View数量+1 decorCount++; } } } //4.普通子View的宽度 final int childWidth = width - paddingLeft - paddingRight; // Page views. Do this once we have the right padding offsets from above. //5.下面针对普通子View布局,在此之前我们已经得到正确的偏移量了 for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); //5.1 ItemInfo 是ViewPager静态内部类, // 它保存了普通子View的position、offset等信息,是对普通子View的一个抽象描述 ItemInfo ii; //5.2 infoForChild通过传入View查询对应的ItemInfo对象 if (!lp.isDecor && (ii = infoForChild(child)) != null) { //计算当前子View的左边偏移量 int loff = (int) (childWidth * ii.offset); //将左边距+左边偏移量得到最终子View左边位置 int childLeft = paddingLeft + loff; int childTop = paddingTop; //5.3 如果当前子View需要进行测量(measure),当这个子View是在Layout期间新添加新的, // 那么这个子View需要进行测量,即needsMeasure为true if (lp.needsMeasure) { //5.4 标记已经测量过了 lp.needsMeasure = false; //5.5 下面过程跟onMeasure类似 final int widthSpec = MeasureSpec.makeMeasureSpec( (int) (childWidth * lp.widthFactor), MeasureSpec.EXACTLY); final int heightSpec = MeasureSpec.makeMeasureSpec( (int) (height - paddingTop - paddingBottom), MeasureSpec.EXACTLY); child.measure(widthSpec, heightSpec); } if (DEBUG) Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + "," + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight()); //5.6 对普通子View进行layout child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight()); } } } //6. 将部分局部变量保存到实例变量中 mTopPageBounds = paddingTop; mBottomPageBounds = height - paddingBottom; mDecorChildCount = decorCount; //7. 如果是第一次layout,则将ViewPager滑动到第一个子View的位置 if (mFirstLayout) { scrollToItem(mCurItem, false, 0, false); } //8. 标记已经布局过了,即不再是第一次布局了 mFirstLayout = false; }
好了,最基本的onMeasure和onLayout已经分析完了,现在还不能滑动,接下来的文章将去分析一下ViewPager的滑动~