1、 View的getWidth()和getMeasuredWidth()有什么区别吗? 我们先看一下 getMeasuredWidth()的源码
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
查找一下mMeasuredWidth 在那里赋的值
private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
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);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
追踪到最后是调用完onMeasure方法进行赋的值,也就是说如果调用getMeasuredWidth() 获取宽度,必须要等measure方法走完才行
再来分析一下getWidth(),来看一下源码
public final int getWidth() {
return mRight - mLeft;
}
找一下mRight -和mLeft在那里赋的值
protected boolean setFrame(int left, int top, int right, int bottom) {
....
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
....
}
在源码可以看到在这个方法赋的值,这个方法是在layout调用的也就是说,getWidth()必须要layout完之后才能获取到值
而 onMeasure 和layout是怎么调用的呢,简单捋一下view的绘制流程
每一个view的绘制流程大致包括,measure,layout,draw,view的绘制是从ViewRootImpl的performtraversal()方法开始的这个方法里面依次调用了 measure , layout, draw ,在measure中调用了onMeasure
总结一下,getWidth()在layout走完之后才有值getMeasuredWidth()在measure走完之后才有值
2、如何在onCreate中拿到View的宽度和高度?
由于view的测量 measure 和Activity的生命周期是不一样的,很有可能在onCreate里面获取宽高,view还没有测量完成,获取不正确
下面有几种方法去正确的获取view的宽高
(1)onWindowFocusChanged(boolean hasFocus):
这个方法表示view已经初始化完毕,此时宽高已经初始化好了,这个方法会在得到焦点,和失去焦点分别调用一次
(2)view.post(Runnable):
通过post可以将一个Runnable投放到消息队列尾部,意思是将任务添加到消息队列中,保证在UI线程执行。从本质上说,它还是依赖于以Handler、Looper、MessageQueue、Message为基础的异步消息处理机制。相对于新建Handler进行处理更加便捷。下面举一个常用的例子,比如在onCreate方法中获取某个view的宽高,而直接View#getWidth获取到的值是0。要知道View显示到界面上需要经历onMeasure、onLayout和onDraw三个过程,而View的宽高是在onLayout阶段才能最终确定的,而在Activity#onCreate中并不能保证View已经执行到了onLayout方法,也就是说Activity的声明周期与View的绘制流程并不是一一绑定。那为什么调用post方法就能起作用呢?首先MessageQueue是按顺序处理消息的,而在setContentView()后队列中会包含一条询问是否完成布局的消息,而我们的任务通过View#post方法被添加到队列尾部,保证了在layout结束以后才执行。
(3) ViewTreeObserver
使用ViewTreeObserver的addOnGlobalLayoutListener就可以获取,意思是当view树的状态发生改变,或者可见发生改变时回调,伴随view树的状态多次发生改变,会多次回调,记得移除监听
(4)measure
private void initMeasure() {
int measureWidth = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
int measureheight = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
mButton3.measure(measureWidth, measureheight);
Log.d("mmmmeasure", "width=" + mButton3.getMeasuredWidth() + "/");
}
这个有点问题,这个测出来的跟其他的宽度不同
12-19 09:10:09.462 12850-12850/com.example.jh.rxhapp D/mmmwith: width=0/
12-19 09:10:09.463 12850-12850/com.example.jh.rxhapp D/mmmmeasure: width=324/
12-19 09:10:09.585 12850-12850/com.example.jh.rxhapp D/mmmviewTreeObserver: width=1080/
12-19 09:10:09.599 12850-12850/com.example.jh.rxhapp D/mmmviewpost: width=1080/
12-19 09:10:09.746 12850-12850/com.example.jh.rxhapp D/mmmwithonFocusChanged: width=1080/