美文网首页android自定义view
Android的View绘制流程

Android的View绘制流程

作者: ComeAsExpected | 来源:发表于2018-08-01 19:05 被阅读242次

    View的工作流程主要是指measure、layout、draw这三大流程,即测量、布局、绘制,其中measure确定View的测量宽高,layout确定View的最终宽高和四个顶点的位置,draw则将View绘制到屏幕上。


    measure过程:measure方法是为了确定View的大小,父容器会提供宽度和高度参数的约束信息。该方法是一个final类型的方法,意味着子类不能重写它。真正的测量工作是在View的onMeasure方法中进行,因此关注onMeasure方法的实现即可。

    测量情况
    • MeasureSpec.UNSPECIFIED:父容器不对View有任何限制,要多大给多大。这种情况一般用于系统内部,表示一种测量的状态。
    • MeasureSpec.EXACTLY:父容器已经决定了View的精确大小,不管View想要多大都是给固定大小。它对应于LayoutParams中的match_parent和具体数值这两种模式。
    • MeasureSpec.AT_MOST:父容器指定了一个可用大小,View的大小不能超过这个值。它对应于LayoutParams中的wrap_content。
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                    getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
        }
    
    public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
    
        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.EXACTLY:
            result = specSize;
            break;
        }
        return result;
    }
    
    protected int getSuggestedMinimumWidth() {
        //mMinWidth对应于android:minWidth这个属性所值定的值,如果不指定,则默认为0
        //有背景图片的情况下,mBackground.getMinimumWidth()返回的是Drawable的原始宽度,这个值可能为0(比如ShapeDrawable没有原始宽高,就会返回0)
        return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
    

    onMeasure方法最后一定要执行setMeasuredDimension(int measuredWidth, int measuredHeight),否则 measure 方法会抛出异常 IllegalStateException。View的宽高计算:EXACTLY和AT_MOST情况下就是measureSpec的specSize(View测量后的大小);UNSPECIFIED情况下,它的大小是getSuggestedMinimumWidth()返回值。

    从getDefaultSize方法的实现来看,EXACTLY和AT_MOST情况下,View的宽高由specSize决定,由此得出结论:直接继承View的自定义控件需要重写onMeasure方法并设置wrap_content时的自身大小,否则在布局中使用wrap_content相当于使用match_parent。原因:如果View在布局中使用wrap_content,那么它的specMode是AT_MOST模式,在这种模式下,它的宽高为specSize;这种情况下,View的specSize是parentSize,而parentSize是父容器中目前可以使用的大小,即父容器当前剩余空间大小,因此效果与使用match_parent一致。

    ViewGroup的measure过程: 除了完成自己的measure过程外,还要遍历去调用所有子元素的measure方法,各个子元素再递归去执行这个过程。ViewGroup是一个抽象类,因为不同的ViewGroup子类有不同的布局特性,所以没有重写onMeasure方法,而是由具体子类重写,但是它提供了measureChildren和measureChildWithMargins方法,它们会调用每个子View的measure方法。

    需要注意的是,在某些极端情况下,系统可能需要多次measure才能确定最终的测量宽高,在这种情形下,在onMeasure方法中拿到的测量宽高可能是不准确的。一个比较好的习惯是在onLayout方法中去获取View的测量宽高。


    layout过程

    Layout的作用是ViewGroup用来确定子元素的位置,当ViewGroup的位置被确定后,它在onLayout中会遍历所有子元素并调用其layout方法,在layout方法中又会调用onLayout方法。

    layout方法的大致流程:首先会通过setFrame方法来设定View的四个顶点的位置,即初始化mLeft、mRight、mTop和mBottom,View的四个顶点一旦确定,那么View在父容器中的位置也就确定了;接着会调用onLayout方法,这个方法的用途是父容器确定子元素的位置,因为它的具体实现和具体的布局有关,所以View和ViewGroup都没有真正实现它,而是由子类重写。

    注意:ViewGroup的layout方法是final类型的,它会调用onLayout方法(ViewGroup中是抽象方法,子类必须重写),所以子类根据布局需要重写onLayout方法。View的layout方法是public类型的,但是最终的位置确定是在onLayout方法(View中是public的空方法)中进行的,所以重写onLayout方法即可。


    draw过程

    Draw过程就是将View绘制到屏幕上,有如下几步:

    1. 绘制背景 drawBackground(canvas)
    2. If necessary, save the canvas' layers to prepare for fading
    3. 绘制自己的内容 onDraw(canvas)
    4. 绘制子元素 dispatchDraw(canvas) :遍历调用所有子元素的draw方法
    5. If necessary, draw the fading edges and restore layers
    6. 绘制装饰 onDrawForeground(canvas)

    接下来看View的一个方法:

    /**
    * If this view doesn't do any drawing on its own, set this flag to
    * allow further optimizations. By default, this flag is not set on
    * View, but could be set on some View subclasses such as ViewGroup.
    *
    * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
    * you should clear this flag.
    *
    * @param willNotDraw whether or not this View draw on its own
    */
    public void setWillNotDraw(boolean willNotDraw) {
    setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
    }

    从setWillNotDraw这个方法的注释可看出,如果一个View不需要绘制任何内容,那么设置这个标记为为true以后,系统会进行相应的优化。默认情况下,View没有起用这个优化标记位,但是ViewGroup会默认启用。这个标记位对实际开发的意义是:当我们的自定义控件继承于ViewGroup并且本身不具备绘制功能时,就可以开始这个标记位便于系统进行优化。如果明确知道一个ViewGroup需要通过onDraw来绘制内容,就需要显式的关闭WILL_NOT_DRAW 这个标记位。


    自定义View

    自定义View一般需要重写的方法

    注意事项:

    1. 让View支持wrap_content(原因在onMeasure过程中讲过)
    2. 如果有必要,让View支持padding
    3. 尽量不要在View中使用Handler,易造成内存泄漏,可以用post系列的方法
    4. View中有线程或动画,需要及时停止,参考View#onDetachedFromWindow
    5. View有滑动嵌套情形时,处理好滑动冲突

    相关问答

    1. 在Activity启动的时候获取某个View的宽高
      答:在onCreate、onStart、onResume中均无法获取到某个View的正确宽高,因为View的measure过程和Activity的生命周期方法并不是同步执行的,无法保证Activity执行了onCreate、onStart、onResume时某个View已经测量完毕,获取到的宽高可能为0。可以通过下列四种方法来解决这个问题:
    • Activity重写onWindowFocusChanged
      onWindowFocusChanged这个方法的含义是:View已经初始化完毕,宽高已经准备好了,所以这时候获取的宽高是没问题的。这个方法会被调用多次,当Activity窗口得到焦点和失去焦点的时候都会被调用。
    • view.post(runnable)
      通过post可以讲一个runnable投递到消息队列的尾部,然后等待Looper调用此runnable的时候,View已经初始化好了。
    • ViewTreeObserver
      使用ViewTreeObserver的众多回调可以完成这个功能,比如OnGlobalLayoutListener接口:当View树的状态发生改变或者View树内部的View的可见性发生改变时,onGlobalLayout方法将会被回调,这是获取View宽高的一个好时机。注意该方法会被调用多次。
    • view.measure(int widthMeasureSpec, int heightMeasureSpec)
      通过手动对View进行measure来得到View的宽高,个人不推荐该方法,有局限性,而且容易出错,就不介绍了。
    public class TestActivity extends Activity {
    
        @BindView(R.id.btn)
        TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            ButterKnife.bind(this);
    
            textView.post(new Runnable() {
                @Override
                public void run() {
                    int width = textView.getMeasuredWidth();
                    int height = textView.getMeasuredHeight();
                    Log.d("==========", "onCreate view.post  width = " + width + " ; height = " + height);
                }
            });
    
            ViewTreeObserver viewTreeObserver = textView.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //特别注意:此处不能直接使用viewTreeObserver.removeGlobalOnLayoutListener(this); 会有如下异常:
                    //java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
                    textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    int width = textView.getMeasuredWidth();
                    int height = textView.getMeasuredHeight();
                    Log.d("==========", "onCreate viewTreeObserver.addOnGlobalLayoutListener  width = " + width + " ; height = " + height);
                }
            });
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            textView.post(new Runnable() {
                @Override
                public void run() {
                    int width = textView.getMeasuredWidth();
                    int height = textView.getMeasuredHeight();
                    Log.d("==========", "onStart view.post  width = " + width + " ; height = " + height);
                }
            });
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            int width = textView.getMeasuredWidth();
            int height = textView.getMeasuredHeight();
            Log.d("==========", "onResume  width = " + width + " ; height = " + height);
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if(hasFocus) {
                int width = textView.getMeasuredWidth();
                int height = textView.getMeasuredHeight();
                Log.d("==========", "onWindowFocusChanged  width = " + width + " ; height = " + height);
            }
        }
    }
    
    //测试结果:
    //onResume  width = 0 ; height = 0
    //onCreate viewTreeObserver.addOnGlobalLayoutListener  width = 391 ; height = 57
    //onCreate view.post  width = 391 ; height = 57
    //onStart view.post  width = 391 ; height = 57
    //onWindowFocusChanged  width = 391 ; height = 57
    
    1. Activity显示流程


      UI界面架构

    ViewRoot对应于ViewRootImpl类,它是连接WindowManager和DecorView的纽带,View的三大流程均是通过viewRoot来完成的。在ActivityThread中,当Activity对象创建完毕后,会将decorView添加到window中,同时会创建ViewRootImpl对象viewRoot,并将viewRoot和decorView建立关联。

    View的绘制流程从viewRoot的performTraversals方法开始的。

    1. View的测量宽高和显示的最终宽高区别
      答:测量宽高是在measure过程中确定的,为onMeasure方法中通过setMeasuredDimension设置的值;最终宽高是在layout过程中确定的,宽为mRight - mLeft,高为mBottom - mTop。

      正常情况下,它两是相等的,某些特殊情况会不一致,比如:

    //重写View的layout方法
    public void layout(int l, int t, int r, int b) {
        //最终宽高会比测量宽高大100px
        super.layout(l, t, r + 100, b + 100);
    }
    
    1. MeasureSpec的赋值原理
      答:MeasureSpec代表一个32位的int值,高2位代表SpecMode,低30位代表SpecSize。
      DecorView:MeasureSpec由窗口的尺寸和自身的LayoutParams共同决定;
      普通View:MeasureSpec由父容器的MeasureSpec和自身的LayoutParams共同决定

    2. 当一个TextView的实例调用setText()方法后执行了什么
      答:setText最后会调用requestLayout()和invalidate()。requestLayout()会调用父容器的requestLayout方法,直至顶层View。requestLayout()会调用measure过程和layout过程,invalidate()会调用draw过程。

    3. 几个常用方法介绍
      requestLayout():调用measure过程和layout过程;
      invalidate():必须要在UI线程调用;View可见的话,会调用onDraw方法;
      postInvalidate():可以在非UI线程调用,通过handler发送一个MSG_INVALIDATE消息,然后在主线程处理消息,执行invalidate()方法。

    相关文章

      网友评论

        本文标题:Android的View绘制流程

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