高级UI-流式布局-(行行代码有注释)

UI绘制流程

1, Measure

MeasureSpec:在Measure流程中,系统将View的LayoutParams根据父容器所施加的规则转换成对应的MeasureSpec,
在onMeasure中根据这个MeasureSpec来确定view的测量宽高
测量
三种测量模式

  • AT_MOST 至多
  • EXACTLY 精确的测量
  • UNSPECIFIED 不确定(需要动态测量)

AT_MOST : child view 最终的 大小不能超过父容器.给的 ------wrap_content

EXACTLY : 父容器已经测量出所需要的精确大小,这也是childview的最终大小
------match_parent,精确值

UNSPECIFIED: 不确定,源码内部使用
-------一般在ScrollView,ListView (滑动中使用 因为需要动态测量)

2、View的测量

onMeasure方法里面调用setMeasuredDimension()确定当前View的大小

3、ViewGroup的测量

3.1、遍历测量Child,可以通过下面三个方法来遍历测量Child
measureChildWithMargins
measureChild
measureChildren

3.2、setMeasuredDimension 确定当前ViewGroup的大小
4、假如去自定义View,ViewGroup,要如何做好Measure? 模板代码
实际上就是拷贝下源码就好

	1、View
		
		套路:最终调用setMeasuredDimession方法来保存自己的测量宽高
		final int specMode = MeasureSpec.getMode(measureSpec);
		final int specSize =  MeasureSpec.getSize(measureSpec);
		switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            /* Parent says we can be as big as we want. Just don't be larger
               than max size imposed on ourselves.
            */
            result = Math.min(desiredSize, maxSize);
            break;
        case MeasureSpec.AT_MOST:
            // Parent says we can be as big as we want, up to specSize.
            // Don't be larger than specSize, and don't be larger than
            // the max size imposed on ourselves.
            result = Math.min(Math.min(desiredSize, specSize), maxSize);
            break;
        case MeasureSpec.EXACTLY:
            // No choice. Do what we are told.
            result = specSize;
            break;
    }
    return result;
		
	2、ViewGroup
	套路:
	1、测量子view的规格大小
		measureChildWithMargins
		measureChild
		measureChildren
		
	2、通过子view的规格大小来确定自己的大小 setMeasuredDimession

2、Layout布局过程

	套路和我们Measure类似

流式布局

先说下思路:
当最后一个控件加入到当前行时,如果大于最大的宽度,那么换行.否则继续添加.

主要是两大块 onMeasure onLayout

onMeasure 遍历childview 并且测量

onLayout 需要摆放view 那么需要记录下来child的view
然后摆放

效果图
mark

上代码:

package androidrn.myflowlayout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuml
 * @explain 流式布局
 * @time 2017/12/26 20:32
 */

public class FlowLayout extends ViewGroup {



    /**
     * 用来保存每行的view的列表
     */
    private List<List<View>> mViewLinesList = new ArrayList<>();
    /**
     * 用来保存行高的列表
     */
    private List<Integer> mLineHeights = new ArrayList<>();

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 用来支持margin
     *
     * @param attrs
     * @return
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);

    }

    /**
     * 负责设置子控件的测量模式和大小 根据所有子控件设置自己的宽和高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获得它的父容器为它设置的测量模式和大小
        int iWidthMode = MeasureSpec.getMode(widthMeasureSpec);//宽度的测量模式
        int iHeithgMode = MeasureSpec.getMode(heightMeasureSpec);//高度的测量模式
        //如果是EXACTLY 那么就是获取的值
        int iWidthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);//  比如at_most中最多不能超过的值
        int iHeightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);

//        如果是warp_content情况下,记录宽和高
        int measureWidth = 0;//最终宽度
        int measureHeight = 0;//最终的高度
        int iCurrentLineW = 0;//每行的宽度
        int iCurrentLineH = 0;//每行的高度


        //如果测量模式是EXACTLY 也就是宽高设置成match_parent
        if (iWidthMode == MeasureSpec.EXACTLY && iHeithgMode == MeasureSpec.EXACTLY) {
            //那么获取的宽度默认值就是建议的值
            measureWidth = iWidthSpaceSize;
            measureHeight = iHeightSpaceSize;
        } else {
            //开始测量
            int childWidth;
            int childHeight;
            //用来存储每行的子view
            List<View> viewList = new ArrayList<>();
            //获取孩子的个数
            int childCount = getChildCount();

            //遍历所有child view
            for (int i = 0; i < childCount; i++) {
                //获取孩子View
                View childView = getChildAt(i);

                //测量每一个child的宽和高 参数把 默认的宽高传入进去
                measureChild(childView, widthMeasureSpec, heightMeasureSpec);
                // 得到child的layoutParams
                MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
                // 当前子空间实际占据的宽度
                childWidth = childView.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
                // 当前子空间实际占据的高度
                childHeight = childView.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
                /**
                 * 如果加入当前child,超出最大宽度,则得到目前最大宽度给width,累加 然后换行
                 */
                if (iCurrentLineW + childWidth > iWidthSpaceSize) {
                    //1 记录当前行的信息

                    //宽度比较 获取最宽的宽度 用测量的宽度和当前的宽度进行比较
                    measureWidth = Math.max(measureWidth, iCurrentLineW);
                    //高度累加
                    measureHeight +=  iCurrentLineH;


                    //2,将当前viewLine添加至总的mViewLinesList,并将高度添加至总的高度list
                    mViewLinesList.add(viewList);
                    mLineHeights.add(iCurrentLineH);

                    //1,换行,记录新的一行的信息
                    //重新赋值新的一行的宽高
                    iCurrentLineW = childWidth;
                    iCurrentLineH = childHeight;

                    // 新建一行,那么viewList 也需要重新new一个
                    viewList = new ArrayList<View>();
                    viewList.add(childView);

                } else {
                    //1、行内宽度的叠加、高度比较
                    //如果没有大于父容器的宽度 那么宽度不断累加
                    iCurrentLineW +=  childWidth;
                    //高度比较 获取最高的高度
                    iCurrentLineH = Math.max(iCurrentLineH,childHeight );
                    // 2、添加至当前行的viewList中
                    viewList.add(childView);
                }
                /*****3、如果正好是最后一行需要换行**********/
                //如果正好是最后一行需要换行
                if (i == childCount - 1) {
                    //1 记录当前行的信息

                    //宽度比较 获取最宽的宽度 用测量的宽度和当前的宽度进行比较
                    measureWidth = Math.max(measureWidth, iCurrentLineW);
                    //高度累加
                    measureHeight +=  iCurrentLineH;


                    //2,将当前viewLine添加至总的mViewLinesList,并将高度添加至总的高度list
                    mViewLinesList.add(viewList);
                    mLineHeights.add(iCurrentLineH);


                }
            }


        }

        //最后需要调用这个 存储测量宽度和测量高度
        setMeasuredDimension(measureWidth, measureHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        /**遍历摆放view**/
        int left, right, top, bottom;
        int curTop = 0;
        int curLeft = 0;
        //共有多少行
        int lineCount = mViewLinesList.size();
        //垂直方向遍历行
        for (int i = 0; i < lineCount; i++) {
            //这是第i行
            List<View> viewLine = mViewLinesList.get(i);
            //第i行有多少个childview
            int lineViewSize = viewLine.size();
            //遍历第i行
            for (int j = 0; j < lineViewSize; j++) {
                //获取当前行的chlidview
                View childView = viewLine.get(j);
                //获取当前childview的LayoutParams
                MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
                //下面根据layoutparams 获取左上右下的距离 最后再调用chlidview.layout
                left = curLeft + layoutParams.leftMargin;
                top = curTop + layoutParams.topMargin;
                //右边的距离是当前距离左边的距离加上控件的宽度
                right = left + childView.getMeasuredWidth();
                //高度同理
                bottom = top + childView.getMeasuredHeight();
                //开始布局
                childView.layout(left, top, right, bottom);
                //currentLeft 叠加 布局一次currentLeft 需要向右移动
                curLeft += childView.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;

            }
            //换行后currentLeft置为0
            curLeft = 0;
            //换行后高度需要累加
            curTop += mLineHeights.get(i);
        }
        //布局完成后清空
        mViewLinesList.clear();
        mLineHeights.clear();


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值