美文网首页
View绘制流程(三) - onLayout

View绘制流程(三) - onLayout

作者: 世道无情 | 来源:发表于2019-02-12 14:26 被阅读0次

    1. onLayout


    measure完毕后,表示 view大小已经测量好了,然后就是 onLayout了,作用是确定 view的位置;

    performTraversals__>perforLayout
    host.layout__>view.onLayout__>viewgroup.onLayout

    2. 源码分析


    private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
                int desiredWindowHeight) {
        // 其实就是 view.layout
        host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
    }
    
    // view.layout
    public void layout(int l, int t, int r, int b) {
          // view.onLayout
          onLayout(changed, l, t, r, b);
    }
    
    // 子view的位置 需要 父view确定,所以需要看 ViewGroup的 onLayout
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {}
    
    // ViewGroup中的 onLayout是抽象方法,意味着每个子类都必须重写 onLayout
    @Override
    protected abstract void onLayout(boolean changed,int l, int t, int r, int b);
    

    上边源码分析:
    1>:在 performLayout 中 调用 view.layout(),在 该layout中,首先调用 setFrame 判断 view大小是否改变过,为了看有没有必要对当前 view 进行重绘;
    2>:在 view.layout() 中调用 view.onLayout(),此方法是一个空方法,因为 父view决定子view位置;
    3>:然后看 ViewGroup的 onLayout,是一个抽象方法,说明所有继承 ViewGroup的子类都必须 重写 onLayout,像LinearLayout、RelativeLayout都重写了;

    下边写一个示例代码,实现的就是:自定义一个ViewGroup,里边包含 子view,让 子view 显示出来就可以;

    3. 示例代码


    效果如下:


    图片.png
    1>:自定义 SimpleLayout,继承 ViewGroup,就是自定义 一个 ViewGroup:
    /**
     * ================================================
     * Email: 2185134304@qq.com
     * Created by Novate 2018/12/25 17:16
     * Version 1.0
     * Params:
     * Description:    自定义ViewGroup,用于包裹一个 子view,然后让 子view显示出来即可
     * ================================================
    */
    
    public class SimpleLayout extends ViewGroup {
        public SimpleLayout(Context context) {
            super(context);
        }
    
        public SimpleLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SimpleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
    
        /**
         * 1. 第一个调用onMeasure
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            // view绘制流程:因为首先调用onMeasure,然后调用onLayout,所以这里要在onMeasure判断:
            // SimpleLayout中是否包含子view,如果有就调用measureChild测量子view大小
            if (getChildCount() > 0){
                View childView = getChildAt(0);
                measureChild(childView , widthMeasureSpec , heightMeasureSpec);
            }
        }
    
    
        /**
         * 2. 第二个调用onLayout
         */
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            // 在onLayout中同样判断:
            // SimpleLayout中是否包含子view,如果有就调用 子view的 layout方法,
            // 确定 子view在SimpleLayout中的位置
            if (getChildCount() > 0){
                View childView = getChildAt(0);
                // 4个参数:左、上、右、下4个坐标  后两个参数就是在onMeasure中测量的宽高
    //            childView.layout(0 , 0 , childView.getMeasuredWidth() , 
    //            childView.getMeasuredHeight());
    
                // 这里修改后,图片ImageView大小就会变化
                childView.layout(0 , 0 , 200 , 200);
            }
        }
    
    2>:activity_simplelayout布局文件如下:
    <?xml version="1.0" encoding="utf-8"?>
    <com.novate.test.view.SimpleLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            />
    </com.novate.test.view.SimpleLayout>
    

    上边逻辑是:
    1>:view 绘制流程:先调用 onMeasure,然后调用 onLayout,这里需要在 onMeasure中判断该 SimpleLayout中是否包含子view,如果有,就调用 measureChild测量 子view大小;
    2>:在 onLayout中 同样需要判断,是否包含 子view,如果有 ,就调用 子view的 layout 确定它在 SimpleLayout中的位置;

    3>:getMeasureWidth在 onMeasure后就可以获取到,getMeasureWidth通过 setMeasuredDimension设置的;而getWidth在 onLayout后才可以获取到;

    相关文章

      网友评论

          本文标题:View绘制流程(三) - onLayout

          本文链接:https://www.haomeiwen.com/subject/rcnflqtx.html