ViewGroup的测量及绘制

1、ViewGroup的测量
 
public abstract class

ViewGroup

extends View
implements ViewManager ViewParent
java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup

Class Overview

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.

对于ViewGroup来说除了完成自身的measure过程外,还要遍历去调用所有子元素的measure方法,各子元素再递归去执行这个过程。ViewGroup提供了一个measureChildren方法:

protected voidmeasureChildren (int widthMeasureSpec, int heightMeasureSpec)
 

Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding. We skip children that are in the GONE state The heavy lifting is done in getChildMeasureSpec.

Parameters
widthMeasureSpecThe width requirements for this view
heightMeasureSpecThe height requirements for this view
protected void measureChildren(int widthMeasureSpec,int heightMeasureSpec){
	final int size = mChildrenCount;
	final View[] children = mChildren;
	for(int i = 0;i < size; ++i){
		final View child = children[i];
		if((child.mViewFlags & VISIBILITY_MASK) != GONE){
			measureChild(child,widthMeasureSpec,heightMeasureSpec);
		}
	}
}

 

从上面的源码看,ViewGroup在measure时,会对每一个元素进行measure。

measureChild方法:

protected void measureChild(View child,int parentWidthMeasureSpec,int parentHeightMeasureSpec){
	final LayoutParams lp = child.getLayoutParams();
	final int childWidthMeasureSpec = getChildMeasureSpec(parentWidth - MeasureSpec,mPaddingLeft + mPaddingRight,lp.width);
	final int childHeightMeasureSpec = getChildMeasureSpec(parentHeight - MeasureSpec,mPaddingTop + mPaddingBottom,lp.height);
	child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}


measureChild的思想就是取出子元素的LayoutParams,再通过getChildMeasureSpec来获取子元素的MeasureSpec,接着直接将MeasureSpec传递给View的measure方法进行测量。

 

2、ViewGroup的绘制

 

ViewGroup通常情况下不需要绘制,如果不用指定ViewGroup的背景颜色,其onDraw()方法都不会被调用。ViewGroup会使用dispatchDraw()方法绘制其子View,过程同样是遍历所有子View,并调用子View的绘制方法来完成绘制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值