现在的位置: 首页 > 综合 > 正文

android View与ViewGroup研究

2013年02月23日 ⁄ 综合 ⁄ 共 5362字 ⁄ 字号 评论关闭

View 有一个属性为 mParent(ViewParent型)
View 有一个属性为 mLayoutParams(ViewGroup.LayoutParams型)
无论是 mParent还是mLayoutParams 都是在系统在解析 XML 时自动进行初始化的.

ViewGroup 有一个 View[] mChildren 数组,用来保存自己的孩子;ViewGroup实现了ViewManager接口,可以增加/删除 孩子;ViewGroup实现了ViewParent接口可以执行针对特定child View的一些操作.

无论是View还是ViewGroup,其重点都在 layout(确定大小和位置) 方法和 draw(确定如何画) 方法上.

每一个View都具有一个Rect区域,它的坐标是相对于其父亲的.
对于一个View应该首先进行 measure(int widthMeasureSpec, int heightMeasureSpec) (用于决定大小),然后再进行layout() (用于决定位置)

layout(int l, int t, int r, int b) 内部调用 protected void onLayout(boolean changed, int left, int top, int right, int bottom)

draw(Canvas) 内部调用 onDraw(Canvas),而 onDraw 由继承View的类实现.

draw(Canvas) 的作用: 把一个View呈递到一个Canvas上,在呈递之前layout应该被设置好,draw的过程如下:

/*
 * Draw traversal performs several drawing steps which must be executed
 * in the appropriate order:
 *
 *      1. Draw the background
 *      2. If necessary, save the canvas' layers to prepare for fading
 *      3. Draw view's content
 *      4. Draw children
 *      5. If necessary, draw the fading edges and restore layers
 *      6. Draw decorations (scrollbars for instance)
 */

下面是 View.draw方法中的一些摘抄

// Step 3, draw the content
if (!dirtyOpaque) onDraw(canvas);

// Step 4, draw the children
dispatchDraw(canvas);

// Step 6, draw decorations (scrollbars)
onDrawScrollBars(canvas);

ViewGroup中的 dispatchDraw(canvas)的实现如下:

protected void dispatchDraw(Canvas canvas) {
	final int count = mChildrenCount;
	final View[] children = mChildren;
	int flags = mGroupFlags;


	if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
		for (int i = 0; i < count; i++) {
			final View child = children[i];
			if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
				more |= drawChild(canvas, child, drawingTime);
			}
		}
	} else {
		for (int i = 0; i < count; i++) {
			final View child = children[getChildDrawingOrder(count, i)];
			if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
				more |= drawChild(canvas, child, drawingTime);
			}
		}
	}
}

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
	return child.draw(canvas, this, drawingTime);
}

layout(int l, int t, int r, int b) 内部调用 protected void onLayout(boolean changed, int left, int top, int right, int bottom)

一个View和一个ViewGroup不一样的地方在于:
View.measure及layout是针对自己的,而ViewGroup.measure及layout是针对自己及孩子的.
View类中的方法 public final void measure(int widthMeasureSpec, int heightMeasureSpec),那么此方法是被谁调用,且参数是如何传递的呢? 应该是被ViewGroup的measureChild

ViewGroup通过measureChild, 根据View的ViewGroup.LayoutParams, 产生一个 MeasureSpec,并把这个 MeasureSpec 传给 View.measure()
下面为ViewGroup.measureChild

protected void measureChild(View child, int parentWidthMeasureSpec,
		int parentHeightMeasureSpec) {
	final LayoutParams lp = child.getLayoutParams();

	final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
			mPaddingLeft + mPaddingRight, lp.width);
	final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
			mPaddingTop + mPaddingBottom, lp.height);

	child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:
            if (childDimension >= 0) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:
            if (childDimension >= 0) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= 0) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = 0;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = 0;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
		//把这个结果传递给子View的 measure 方法
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }

一个定制View的onMeasure方法:

@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec), 
				measureHeight(heightMeasureSpec));
	}
	
	private int measureWidth(int widthMeasureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(widthMeasureSpec);
		int specSize = MeasureSpec.getSize(widthMeasureSpec);
		if(MeasureSpec.EXACTLY == specMode) {
			result = specSize;
		} else {
			result = (int)mPaint.measureText(mText) + getPaddingLeft() 
						+ getPaddingRight();
			if(MeasureSpec.AT_MOST == specMode) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}
	
	private int measureHeight(int heightMeasureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(heightMeasureSpec);
		int specSize = MeasureSpec.getSize(heightMeasureSpec);
		mAscent = (int)mPaint.ascent();
		
		if(MeasureSpec.EXACTLY == specMode) {
			result = specSize;
		} else {
			result = -mAscent + (int)mPaint.descent() + getPaddingTop() 
						+ getPaddingBottom();
			if(MeasureSpec.AT_MOST == specMode) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

抱歉!评论已关闭.