Android绘图机制(二) ViewGroup类

概述

ViewGroup是一个可以包含其他视图的视图。视图组是布局和视图容器的基类。该类中也定义ViewGroup.LayoutParams类作为布局参数的基类,可以查看ViewGroup.LayoutParams 获取更多布局属性。


开发指南

下面是一个继承ViewGroup实现的一个完全自定义视图,该视图是一个简单的FrameLayout,允许在视图内部左右两侧叠加子视图(蓝色字体标注的方法将在下篇文章详细介绍):

/**
 *布局管理器实例。这个一个功能相对全面的布局管理器,你可以在特定的场景中对其进行简化,并应用*这个布局管理器。
 */
@RemoteViews.RemoteView
public class CustomLayout extends ViewGroup {
    //被左侧子视图流使用的空间
    private int mLeftWidth;

    //被右侧子视图流使用的空间   
    private int mRightWidth;

    //下面的对象用于根据子视图窗体重心计算他们的值
    private final Rect mTmpContainerRect = new Rect();
    private final Rect mTmpChildRect = new Rect();


    public CustomLayout(Context context) {
        super(context);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //任何不滚动的布局管理器都应该像这样重写该方法
    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    //要求所有的子视图测量他们自己,并根据子视图的测量结果计算这个布局管理器的尺寸
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();

       //保持我们为了放置视图而在左右两侧使用的空间轨迹;我们需要记录成员变量的值,在后面布局过程中使用。
        mLeftWidth = 0;
        mRightWidth = 0;

        //测量过程的最后会计算这些尺寸值
        int maxHeight = 0;
        int maxWidth = 0;
        int childState = 0;

        //遍历所有子视图,测量他们的值,并通过他们的测量结果计算布局尺寸 
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                //测量子视图,将子视图的margin和padding边计算在内
               <span style="color:#3333ff;"> measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);</span>
                
                  
                //基于布局参数更新尺寸信息,被要求放置到左右两侧的子视图会进入放置过程流中
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                //自定义属性
                if (lp.position == LayoutParams.POSITION_LEFT) {
                    mLeftWidth += Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                } else if (lp.position == LayoutParams.POSITION_RIGHT) {
                    mRightWidth += Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                } else {
                    maxWidth = Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                }
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                childState = <span style="color:#3333ff;">combineMeasuredStates(childState, child.getMeasuredState());</span>
            }
        }

        //总宽度是内部所有子视图的宽度加上其他约束的宽度
        maxWidth += mLeftWidth + mRightWidth;

        //检查默认的最小高度和最小宽度,取最大值
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

       <span style="color:#3333ff;"> //报告最终的的测量结果
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));</span>
    }


    //在子视图中放置所有的子视图
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int count = getChildCount();

        //视图在布局中放置的左右外边
        int leftPos = getPaddingLeft();
        int rightPos = right - left - getPaddingRight();

        //视图流内部的中心区域
        final int middleLeft = leftPos + mLeftWidth;
        final int middleRight = rightPos - mRightWidth;

        //所操作的布局的顶边和底边
        final int parentTop = getPaddingTop();
        final int parentBottom = bottom - top - getPaddingBottom();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                               
                //测量宽度和测量高度
                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();

                //计算正在放置子视图的窗体
                if (lp.position == LayoutParams.POSITION_LEFT) {
                    mTmpContainerRect.left = leftPos + lp.leftMargin;
                    mTmpContainerRect.right = leftPos + width + lp.rightMargin;
                    leftPos = mTmpContainerRect.right;
                } else if (lp.position == LayoutParams.POSITION_RIGHT) {
                    mTmpContainerRect.right = rightPos - lp.rightMargin;
                    mTmpContainerRect.left = rightPos - width - lp.leftMargin;
                    rightPos = mTmpContainerRect.left;
                } else {
                    mTmpContainerRect.left = middleLeft + lp.leftMargin;
                    mTmpContainerRect.right = middleRight - lp.rightMargin;
                }
                mTmpContainerRect.top = parentTop + lp.topMargin;
                mTmpContainerRect.bottom = parentBottom - lp.bottomMargin;

              <span style="color:#3333ff;">  //通过子视图的重心值和尺寸决定其在容器内的最终布局
                Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);

                //放置子视图
                child.layout(mTmpChildRect.left, mTmpChildRect.top,
                        mTmpChildRect.right, mTmpChildRect.bottom);</span>
            }
        }
    }

    // -------------- --------------------------------------------------------
    //下面的实现部分是针对每个子视图的布局参数的,如果你不需要这些(比如说你写了一个布局管理器)不需要混合放置子视图,那么你可以删除这部分 


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new CustomLayout.LayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

    //每个子视图的布局信息
    public static class LayoutParams extends MarginLayoutParams {
        //与视图关联的布局参数中应用的重心Gravity
        public int gravity = Gravity.TOP | Gravity.START;

        public static int POSITION_MIDDLE = 0;
        public static int POSITION_LEFT = 1;
        public static int POSITION_RIGHT = 2;

        public int position = POSITION_MIDDLE;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
          <span style="color:#3333ff;">  // 从xml文件中获取布局参数的值,如果你不需要在xml布局文件中改变这些布局行为,可以不要这些代码
            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CustomLayoutLP);
            gravity = a.getInt(R.styleable.CustomLayoutLP_android_layout_gravity, gravity);
            position = a.getInt(R.styleable.CustomLayoutLP_layout_position, position);
            a.recycle();
            //(自定义属性,可以参考:</span><a target=_blank href="http://www.cnblogs.com/ufocdy/archive/2011/05/27/2060221.html" style="cursor: pointer; font-family: 微软雅黑; font-size: 14px; line-height: 1.5;">http://www.cnblogs.com/ufocdy/archive/2011/05/27/2060221.html</a>)
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }


你可能感兴趣的:(ViewGroup,android自定义控件,自定义布局,android绘图机制)