自定义ViewAndroid学习笔记 - UI

自定义ViewGroup

2019-04-25  本文已影响0人  CoderYuZ

本文主要是自己学习的笔记,想学习的建议直接去看HenCoder视频教学地址

自定义ViewGroup的工作其实就是测量子View大小,测量自己大小,摆放子View。主要是重写onMeasure()和onLayout()

重写onMeasure主要是三个步骤:
  1. 调用每个子View的measure(),让子View自我测量
  2. 根据子View给出的尺寸,得出子View的位置并保存他们的位置和尺寸(因为现在是测量阶段,在onLayout中才会传给子View)
  3. 根据子VIew的位置和尺寸计算出自己的尺寸,并用setMeasuredDimension()保存

第1步是最复杂的,onMeasure方法的两个参数是父View对子View的尺寸限制,这两个参数需要算出来,这俩个参数是通过开发者的要求(也就是XML文件中laytou_开头的属性)和父VIew的可用空间结合计算出来的。计算规则有点复杂,对于每个View尺寸限制的计算,主要是两个属性laytou_width和laytou_height,其他laytou_开头的标签,比如laytou_toLeftOf等等都有影响,开发者在XML中使用了什么标签,就需要在onMeasure中作出相应的计算。主要代码如下,注释很详细:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // usedWidth用来计算ViewGroup自身可用空间是,根据子View的数量逐渐变大。下面代码并没写出具体计算
        int usedWidth = 0;
        // 子View的MeasureSpec,以下只用宽度举例,假设横向摆放
        int childWidthMeasureSpec = 0;
        int childHeightMeasureSpec = 0;
        //ViewGroup自身MeasureSpec
        int selfWithSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int selfWithSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        
        //循环每个子View, 并调用子View的measure方法,让子View自我测量
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            LayoutParams layoutParams =  childView.getLayoutParams();
            switch (layoutParams.width){
                case LayoutParams.MATCH_PARENT:
                    if (selfWithSpecMode == MeasureSpec.AT_MOST || selfWithSpecMode == MeasureSpec.EXACTLY){
                        // 如果自身SpecMode是AT_MOST或者EXACTLY,子View是MATCH_PARENT,那么子View大小是确定的
                        childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(selfWithSpecSize - usedWidth, MeasureSpec.EXACTLY);
                    } else {
                        // 如果自身SpecMode是UNSPECIFIED,子View是MATCH_PARENT,那么没人能限制子View的大小了,会无限大。直接把UNSPECIFIED传下去。子View会设置自己想要的尺寸。
                        // 因为mode为UNSPECIFIED,所以selfWithSpecSize我们可以认为没什么用处,传0也可以。 (其实有用,有兴趣的话去看源码)
                        childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(selfWithSpecSize, MeasureSpec.UNSPECIFIED);
                    }
                    break;
                case LayoutParams.WRAP_CONTENT:
                    if (selfWithSpecMode == MeasureSpec.AT_MOST || selfWithSpecMode == MeasureSpec.EXACTLY){
                        // 如果自身SpecMode是AT_MOST或者EXACTLY,子View是WRAP_CONTENT,那么子View最大宽度是确定的
                        childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(selfWithSpecSize - usedWidth, MeasureSpec.AT_MOST);
                    } else {
                        // 如果自身SpecMode是UNSPECIFIED,子View是WRAP_CONTENT,那么随子View去了
                        // 因为mode为UNSPECIFIED,所以selfWithSpecSize我们可以认为没什么用处,传0也可以。 (其实有用,有兴趣的话去看源码)
                        childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(selfWithSpecSize, MeasureSpec.UNSPECIFIED);
                    }
                    break;
                default:
                    // 子View的宽高为固定值,开发者设置的优先级最高,没什么解释的
                    childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(layoutParams.width, MeasureSpec.EXACTLY);
                    break;
            }
            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }

第2步中有两点说明:

第3步就是根据子View的边界算出自己的尺寸

重写onLayout只需要做一件事:

重写子View的onLayout方法,把之前在onMeasure里保存下来的它们的位置和尺寸作为参数传进去,让它们把自己的位置和尺寸保存下来,并进行自我布局。代码:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            // 数组是在onMeasure中计算并储存的,把位置和尺寸转换成左、上、右、下就可以了
            childView.layout(childLeft[i], childTop[i], childRight[i], childBottom[i]);

        }
    }

总结

感谢HenCoder

上一篇 下一篇

猜你喜欢

热点阅读