浅谈自定义ViewGroup

什么是自定义viewGroup呢?

    其实我们经常会听到自定义view但是并没有听到过自定义的viewgroup,其实自定的viewgroup是自定义view的一个细分,我们把自定义view和自定义viewgroup细分.

自定义view就是说在没有现成的view。需要自己实现的时候,就使用自定义view,一般继承自view,比如surfaceview或者其他的view

自定义viewgroup一般是利用现有的组件根据特定的布局方式来组成新的组件,大多继承自viewgroup或各种layout

2. 自定义view的绘制流程



自己画的图很丑,将就着看把,其实我们可以看到我们必须要在测量之后再来布局这个viewgroup。然而在我们自定义viewgroup的时候其实最重要的时onmeasure和onlayout

在我们自定义view的时候最重要的时ondraw,其实我们在坐界面开发的过程中用得最多的时自定义viewgroup

3. View的层次结构(view树)


4. 开始

以flowlayout举例,首先我们需要测量view的大小,现在我们首先要知道子view的大小和宽高才能确定整个flowlayout的宽高,所以我们需要先循环找到所有子view来确定flowlayout 的宽高所以我们就有

int childCount = getChildCount();for (int i = 0; i < childCount; i++) {

View childView = getChildAt(i);    

childView.measure(widthMeasureSpec,heightMeasureSpec);

}

但是现在我们并不知道子view的宽高,所以现在需要确定子view的宽高,但是现在一般我们会存在三种情况

android:layout_width="wrap_content"

android:layout_width="match_parent"

android:layout_width="100dp"

第三种我们可以很明确的知道子view的宽度,但是前面两种我们并不知道,这时候我们需要用到layoutparmas,首先layoutparmas是什么呢?他是viewgroup的一个内部类,他内部实现了

public static final int MATCH_PARENT = -1;

public static final int WRAP_CONTENT = -2;

public int width;

也就是说它本身就代表了我们这三种情况,所以就有

LayoutParams layoutParams = childView.getLayoutParams();

layoutParams.width;

layoutParams.height;

我们就能得到这个子view的宽高,但是我们并不知道具体的宽高,然后我们在转换的时候还需要用到measurespec,那measurespec又是什么呢?它是view的一个内部类,它内部实现了三种mode(状态),UNSPECIFIED:不对view大小进行限制,EXACTLY:确切的大小,如100dp,AT_MOST:大小不可超过某数值,如:match_parent,最大不能超过父布局,在它内部以二进制的运算来得出结果,由于int为三十二位,用高两位来表述mode,低三十位来表示size,MODE_SHIFT=30的作用是移位

public static final int UNSPECIFIED = 0 << MODE_SHIFT;

public static final int EXACTLY     = 1 << MODE_SHIFT;

public static final int AT_MOST     = 2 << MODE_SHIFT;

private static final int MODE_SHIFT = 30;

然后我们就能得到measurespec的创建规则

然后在这个算法上我们就能找到getChildMeasureSpec这个方法,我们来看下

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 = View.sUseZeroUnspecifiedMeasureSpec ?0 : size;

        resultMode = MeasureSpec.UNSPECIFIED;

    }else if (childDimension == LayoutParams.WRAP_CONTENT) {

// Child wants to determine its own size.... find out how

// big it should be

        resultSize = View.sUseZeroUnspecifiedMeasureSpec ?0 : size;

        resultMode = MeasureSpec.UNSPECIFIED;

    }

break;

我们可以看到创建的规则从而得到我们相应的数值,所以我们可以得到以下代码

int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,paddingLeft + paddingRight,childLayoutParams.width);int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,paddingLeft + paddingRight,childLayoutParams.height);

我们来看看这个方法和参数

public static int getChildMeasureSpec(int spec, int padding, int childDimension)

第一个是父view的spec,第三个是子view的dimension,但是第二个参数会存疑,并不能确定到底是谁的padding,在这里其实是父类的padding,因为在我们计算子view的measure的时候其实最大是不能超过含有padding距离的父view的,所以这里传的是父view的padding那现在我们就能得到子view的measure

childView.measure(childWidthMeasureSpec,childHeightMeasureSpec);

接下来我们需要测量本view的宽高,但是会存在,当我们流式布局不固定,所以我们应该取某一行最长的作宽,所有行加起来作高,所以现在我们需要将一行所有的view和行高还有这行已经使用的size进行记录

List lineViews = new ArrayList<>();

int lineWidthUsed = 0;

int lineHeight = 0;

lineViews.add(childView);

lineWidthUsed = lineWidthUsed + childMeasureWidth + mHorizonalSpecing;lineHeight = Math.max(lineHeight,childMeasureHeight);

但是当我们已经使用的超过了一行的宽度,那么我们就需要

if (childMeasureWidth + lineWidthUsed + mHorizonalSpecing > selfWidth){    

parentNeededHeight = parentNeededHeight + lineHeight + mVerticalSpecing;   

 parentNeededWidth = Math.max(parentNeededWidth,lineWidthUsed + mHorizonalSpecing);    

lineViews = new ArrayList<>();   

 lineWidthUsed = 0;    

lineHeight = 0;

}

那最后我们就得到

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int realWidth = (widthMode == MeasureSpec.EXACTLY) ? selfWidth : parentNeededWidth;

int realHeight = (heightMode == MeasureSpec.EXACTLY) ? selfHeight : parentNeededHeight;

setMeasuredDimension(realWidth,realHeight);

那么我们接下来应该加载布局

int lineCount = allLines.size();

int curLeft = getPaddingLeft();

int curTop = getPaddingTop();

for (int i = 0; i < lineCount; i++) {

List lineView = allLines.get(i);

int lineHeight = lineHeights.get(i);

for (int i1 = 0; i1 < lineView.size(); i1++) {

View view = lineView.get(i1);

int right = curLeft + view.getMeasuredWidth();

int bottom = curTop + view.getMeasuredHeight();        

view.layout(curLeft, curTop,right,bottom);       

 curLeft = right + mHorizontalSpacing;   

}

curLeft = getPaddingLeft();   

curTop = curTop + lineHeight + mVerticalSpacing;

}

你可能感兴趣的:(浅谈自定义ViewGroup)