标签:颜色 image fine 声明 protected and hose 并且 内部实现
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
/**
* Measure specification规格 mode: The parent has not imposed强迫、要求 any constraint约束
* on the child. It can be whatever size it wants.可以是任意你想要的大小,如ListView的高度
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;//未指定尺寸,适用于AdapterView
/**
* Measure specification mode: The parent has determined确定的 an exact size for the child.
* The child is going to be given指定 those bounds regardless不管怎样 of how big it wants to be.
*/
public static final int EXACTLY = 1 << MODE_SHIFT;//精确尺寸,包括FILL_PARENT和具体的值
/**
* Measure specification mode: The child can be as large as it wants up to the specified指定的 size.
*/
public static final int AT_MOST = 2 << MODE_SHIFT;//父控件允许的最大尺寸,WRAP_CONTENT
public static int makeMeasureSpec(int size, int mode) {
if (sUseBrokenMakeMeasureSpec) return size + mode;
else return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
//获取Parent size与padding差值(也就是Parent剩余大小),若差值小于0直接返回0
int size = Math.max(0, specSize - padding);
else if (childDimension == LayoutParams.MATCH_PARENT) {//如果设置为MATCH_PARENT=-1
// Child wants to be our size. So be it. 设置child的size为size,mode为EXACTLY
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
//获取Parent size与padding差值(也就是Parent剩余大小),若差值小于0直接返回0
int size = Math.max(0, specSize - padding);
else if (childDimension == LayoutParams.WRAP_CONTENT) {//如果设置为WRAP_CONTENT=-2
//设置child的size为size,mode为AT_MOST
// Child wants to determine its own size. It can‘t be bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
/**
* Utility用于 to return a default size. Uses the supplied size if the MeasureSpec
* imposed no constraints没有强加的约束. Will get larger if allowed by the MeasureSpec.
* @param size Default size for this view
* @param measureSpec Constraints imposed by the parent
* @return The size this view should be.
*/
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
/**
* Returns the suggested minimum最小的 height that the view should use. This returns the
* maximum最大值 of the view‘s minimum height and the background‘s minimum height
* When being used in {@link #onMeasure(int, int)}, the caller should still
* ensure确保 the returned height is within在要求之内 the requirements of the parent.
*/
protected int getSuggestedMinimumHeight() {
return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(resolveSize(50, widthMeasureSpec), resolveSize(50, heightMeasureSpec));
}
public static int resolveSize(int size, int measureSpec) {
return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
}
/**
* Utility用于 to reconcile调节 a desired期望 size and state, with constraints imposed限制
* by a MeasureSpec. Will take the desired size, unless a different size
* is imposed by the constraints. The returned value is a compound符合的 integer,
* with the resolved分解 size in the {@link #MEASURED_SIZE_MASK} bits and
* optionally随意的 the bit {@link #MEASURED_STATE_TOO_SMALL} set if the
* resulting size is smaller than the size the view wants to be.
*
* @param size How big the view wants to be.你期望View是多大(仅仅是一个参考值)
* @param measureSpec Constraints imposed by the parent.父类强加的限制
* @param childMeasuredState Size information bit mask for the view‘s children.
* @return Size information bit mask as defined by
* MEASURED_SIZE_MASK and MEASURED_STATE_TOO_SMALL.
*/
public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
final int specMode = MeasureSpec.getMode(measureSpec);
final int specSize = MeasureSpec.getSize(measureSpec);
final int result;
switch (specMode) {
case MeasureSpec.AT_MOST:
if (specSize < size) result = specSize | MEASURED_STATE_TOO_SMALL;
else result = size;
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
case MeasureSpec.UNSPECIFIED:
default:
result = size;
}
return result | (childMeasuredState & MEASURED_STATE_MASK);
}
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
//这些值都是在父View的【measure】过程中确定的}
public final int getWidth() {
return mRight - mLeft;
//mRight、mLeft这些值都是在父View的【layout】过程中确定的}
public final int getLeft() {
return mLeft;//mLeft这些值都是在父View的【layout】过程中确定的
}
Recycles重新使用、回收 the TypedArray, to be re-used重新使用 by a later caller.
After calling this function you must not ever touch操作 the typed array again.
public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) {
final int len = attrs.length;
final TypedArray array = TypedArray.obtain(Resources.this, len);
// .....
return array;
}
final TypedArray attrs = res.mTypedArrayPool.acquire();
标签:颜色 image fine 声明 protected and hose 并且 内部实现
原文地址:http://www.cnblogs.com/baiqiantao/p/4745b8ff752e1da4d8db1187e2b36247.html