基于4.4 code:
View.java:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
.............................................................................
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
.....................................................................
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
..............................................................................
/**
* Like {@link #getMeasuredWidthAndState()}, but only returns the
* raw width component (that is the result is masked by
* {@link #MEASURED_SIZE_MASK}).
*
* @return The raw measured width of this view.
*/
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
之前光顾看基类ViewGroup了,结果发现 View的onMeasure中通过setMeasuredDimension()设置的View的mMeasuredWidth/Height 在 ViewGroup中根本
没有被用到,很是疑惑,感觉measure的意义没了,后来看了细化出来的FrameLayout等layout类,才发现,在这些真正的按照某种规则排布子View的类中
在onMeasure和onLayout中都是会用到子view的getMeasuredWidth/height()的,其实看到ViewGroup的onLayout是抽象函数时,应该有所察觉的。
而MeasuredWidth/Height 也基本只有在父View onMeasure/onLayout的情况下有用。
在子view的layout被调用时,会调到setFrame(l, t, r, b),这个函数才是真正的设置了View的尺寸,不一定就是之前measure时的MeasuredHeight。