标签:
本篇模拟三个角色:Android 架构师-小福、Android 控件开发工程师-小黑、 Android 开发工程师-小白,下面按照三个角色不同角度分析measure过程。
小福负责分享:
小黑负责分享:
java.lang.IllegalStateException: onMeasure() did not set the measured dimension by calling setMeasuredDimension()
public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource { /** * 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() { // 直接返回mMeasuredWidth与后者相与清理掉其他开关获取真是measure大小 return mMeasuredWidth & MEASURED_SIZE_MASK; } /** * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the * measured width and measured height. Failing to do so will trigger an * exception at measurement time.</p> * * @param measuredWidth The measured width of this view. May be a complex * bit mask as defined by {@link #MEASURED_SIZE_MASK} and * {@link #MEASURED_STATE_TOO_SMALL}. * @param measuredHeight The measured height of this view. May be a complex * bit mask as defined by {@link #MEASURED_SIZE_MASK} and * {@link #MEASURED_STATE_TOO_SMALL}. */ protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) { // 通常在onMeasure中调用,传入测量过的视图宽度与高度 mMeasuredWidth = measuredWidth; mMeasuredHeight = measuredHeight; mPrivateFlags |= MEASURED_DIMENSION_SET; } /** * Bits of {@link #getMeasuredWidthAndState()} and * {@link #getMeasuredWidthAndState()} that provide the actual measured size. */ // MeasureSpec中的Mode或占用int类型中前几位 public static final int MEASURED_SIZE_MASK = 0x00ffffff; }
public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource { /** * Return the width of the your view. * * @return The width of your view, in pixels. */ @ViewDebug.ExportedProperty(category = "layout") public final int getWidth() { // 视图的右侧减去左侧的值 return mRight - mLeft; } /** * Assign a size and position to this view. * * This is called from layout. * * @param left Left position, relative to parent * @param top Top position, relative to parent * @param right Right position, relative to parent * @param bottom Bottom position, relative to parent * @return true if the new size and position are different than the * previous ones * {@hide} */ protected boolean setFrame(int left, int top, int right, int bottom) { boolean changed = false; ...... // 四个值中任意一个发生改变就行 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { changed = true; ...... mLeft = left; mTop = top; mRight = right; mBottom = bottom; ...... } return changed; } public void layout(int l, int t, int r, int b) { ...... // 当前视图布局时执行,传入当前视图的上下左右边界值 boolean changed = setFrame(l, t, r, b); if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) { ...... // 上面执行完成后才会触发onLayout onLayout(changed, l, t, r, b); ...... } ...... mPrivateFlags &= ~FORCE_LAYOUT; } }
Android View measure (二) 自定义UI控件measure相关
标签:
原文地址:http://blog.csdn.net/androiddevelop/article/details/44263757