知识星球作业(第5周) - 关于view的知识

322 阅读6分钟

话题:关于View的知识

1、View的getWidth()和getMeasuredWidth()有什么区别吗?

2、如何在onCreate中拿到View的宽度和高度?

(PS: 以下代码基于7.0源码 )

第1题:View的getWidth() 和 getMeasuredWidth() 的区别

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

方法中返回的是 mMeasuredWidth , 它的默认值是0, 追踪发现在 setMeasuredDimensionRaw() 中被赋值:

private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
    mMeasuredWidth = measuredWidth;
    mMeasuredHeight = measuredHeight;

    mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}

而 setMeasuredDimensionRaw() 是在 setMeasuredDimension() 方法中被调用

protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
    boolean optical = isLayoutModeOptical(this);
    if (optical != isLayoutModeOptical(mParent)) {
        Insets insets = getOpticalInsets();
        int opticalWidth  = insets.left + insets.right;
        int opticalHeight = insets.top  + insets.bottom;
        measuredWidth  += optical ? opticalWidth  : -opticalWidth;
        measuredHeight += optical ? opticalHeight : -opticalHeight;
    }
    setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}

到这里就很明显了, 我们都知道 setMeasuredDimension 是在 onMeasure 方法中被调用用来保存测量的尺寸结果, 也就是说 mMeasuredWidth 是在 onMeasure() 方法中执行完成测量流程后并保存尺寸的时候被赋值, 所以 getMeasuredWidth() 返回的值就是 View 测量结果的宽度。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
            getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
  • getWidth()
public final int getWidth() {
    return mRight - mLeft;
}

mRight 和 mLeft 的默认值都是0, 追寻发现在 setFrame() 方法中被赋值 (其实在 offsetLeftAndRight() 和 setRight/Left()中也有被赋值, 不过这两个方法没有在 View 中被直接调用, 而且是被 public 修饰的, 说明我们可以自己直接调用 View 的这个方法来对 View 的位置进行操作)

protected boolean setFrame(int left, int top, int right, int bottom) {
	...
	mLeft = left;
	mTop = top;
	mRight = right;
	mBottom = bottom;
	...
}

setFrame() 在 layout() 中被调用, 这时已经开始 View 的布局流程,说明得到的值是一个最终尺寸值

public void layout(int l, int t, int r, int b) {
	...
	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);
	...
}

到这里还没有结束,我们需要看下来父类调用子元素的 layout 传入的四个顶点值是什么。但是由于在 View 中 onLayout() 方法是空实现, ViewGroup 的 onLayout() 是抽象方法, 所以就挑一个 ViewGroup 常用的子类看一下, 在刚哥的玉书中分析了 LinearLayout , 那我就选 FrameLayout 了:

#FrameLayout
protected void onLayout(boolean changed, int left, int top, int right, int bottom){
    layoutChildren(left, top, right, bottom, false);
}

onLayout 的参数直接传给 layoutChildren,继续走:

void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
    ...
    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();
            ...
            child.layout(childLeft, childTop, childLeft + width, childTop + height);
            ...

look look, 看到没, 这里调用子元素的 layout(int l, int t, int r, int b) 方法对子元素进行摆放, 之前看到 layout 方法中会对子元素的 mRight、mLeft、mTop、mBottom 赋值,即 mRight = r = l + width,而这里看到 width 的赋值是调用 child.getMeasuredWidth(), 所以不难得出来子元素的 getWidth() = mRight - mLeft = getMeasuredWidth()。

总结:

  • getMeasuredWidth() 得到的是 View 的测量尺寸,而这个值要在 View 测量流程完成后才能拿到, 否则值为0。测量尺寸我理解为View自己的期望尺寸。

  • getWidth() 得到的则是父容器根据子view的期望尺寸计算得出的最终尺寸,然后父容器对 View 进行摆放。一般情况下父类都是直接使用和这个期望尺寸,即最终值和测量尺寸值一般是相等的。 实际开发中一般在 onLayout() 中去获取控件的测量尺寸/最终尺寸。

  • 分析 LinearLayout 和 FrameLayout 可以看到 getWidth() 和 getMeasuredWidth() 的值是相等的,只是赋值时间不同,所以在系统 View 的默认实现中,以及开发中我们可以认为 getWidth() = getMeasuredWidth()。当然也存在两种情况会出现不相等:一种是某些极端情况系统需要多次执行measure流程,这时则除了最后一次measure,前几次的measure结果就可能存在不相等。另一种则是在 onLayout() 中调用 layout 时, 对传入的四个顶点值做了一些运算处理, 则这两个值也是不相等的,如下

protected void onLayout(boolean changed, int left, int top, int right, int bottom){	
	...
    child.layout(childLeft, childTop, childLeft + width + 100, childTop + height + 100);
    ...
}
// 或重写 layout 方法
public void layout(int l, int t, int r, int b) {
	super.layout(l, t, r + 100, b + 100);
}

第2题:如何在onCreate中拿到View的宽度和高度

如果直接在 onCreate 中调用 getMeasuredWidth/Height() 是不能正确获取它的尺寸值的, 而且同样在 onResume 和 onStart 中都是不准确的,因为你无法保证此时 View 的测量过程已经完成了,如果没有完成,得到的值则为0。

1. Activity/View 的 onWindowFocusChanged(boolean hasFocus) onWindowFocusChanged 表示 View 已经初始化完毕了, 这时获取它的宽/高是没问题的。 这个方法是当 Activity/View 得到焦点和失去焦点时都会调用一次, 在 Activity 中对应 onResume 和 onPause ,如果频繁的进行 onResume 和 onPause, 则 onWindowFocusChanged 也会被频繁的调用。

public void onWindowFocusChanged(boolean hasFocus) {
	super.onWindowFocusChanged(hasFocus);
	if(hasFocus){
		int width = view.getMeasuredWidth();
		int height = view.getMeasuredHeight();
	}
}

2. view.post(runnable): 通过 post 将一个 runnable 消息投递到消息队列的底部,然后等待 Looper 调用此 runnable 的时候,View 已经初始化好了

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
	 view.post(new Runnable(){
		 @Override
		 public void run(){
			int width = view.getMeasuredWidth();
			int height = view.getMeasuredHeight(); 
		 }
	 });
}

3. ViewTreeObserver ViewTreeObserver 的众多回调可以完成这个需求, 例如使用 OnGlobalLayoutListener 这个接口, 当 view 树的状态改变或者 view 树内部 view 的可见性改变, 都会回调 onGlobalLayout 方法。

// 方法1:增加整体布局监听
ViewTreeObserver vto = view.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override 
    public void onGlobalLayout() {
	    view.getViewTreeObserver().removeGlobalOnLayoutListener(this);     
	    int height = view.getMeasuredHeight(); 
	    int width = view.getMeasuredWidth(); 
    } 
});

// 方法2:增加组件绘制之前的监听
ViewTreeObserver vto =view.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
   @Override
    public boolean onPreDraw() {
       int height = view.getMeasuredHeight();
       int width = view.getMeasuredWidth();    
   }
});

4. view.measure(int widthMeasureSpec, int heightMeasureSpec) 这是通过手动触发对 View 进行 measure 来得到 View 的宽/高的方法。需要根据 View 的 LayoutParams 情况来分别处理:

  • match_parent: 无法测量宽/高,根据前面分析的 View 测量过程,此时构造它的 MeasureSpec 需要知道父容器的剩余控件,而此时我们无法获取,则理论上讲无法测出 View 的大小。

  • 具体的数值(dp / px): 比如宽高都是200, 直接通过 MeasureSpec.makeMeasureSpec 手动构造它的宽和高尺寸, 然后传入 view.measure 方法触发测量 :

int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);
  • wrap_content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1 << 30 - 1, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(1 << 30 - 1, View.MeasureSpec.AT_MOST);
view.measure(widthMeasureSpec, heightMeasureSpec);

1 << 30 - 1 就是30位 int 值的最大值, 也就是30个1。前面介绍 MeasureSpec 时说到 View 的尺寸用30位的int值表示,此时我们是用 View 理论上能支持的最大值去构造 MeasureSpec ,相当于给 View 一个足够的范围空间去完成自己的测量并保存自己的测量结果, 是可行的。

  • 还有两个错误用法: 违背了系统的内部实现规范, 因为无法通过错误的 MeasureSpec 去得到合法的 SpecMode, 导致测量过程有错。
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(-1 , View.MeasureSpec.UNSPECIFIED
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(- 1, View.MeasureSpec.UNSPECIFIE
view.measure(widthMeasureSpec, heightMeasureSpec);

// 这个我自己在7.0版本的编译环境下已经编译不通过了,在 makeMeasureSpec 
// 方法的第一个参数需要传入 0 ~ 1073741823 范围的值, -1 不合法。
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
// measure 方法参数不合法

最后帮刚哥做个宣传,抓紧加入星球大家一起学习吧~