美文网首页
View的绘制流程小结

View的绘制流程小结

作者: hellog_ | 来源:发表于2017-02-18 15:59 被阅读0次

View绘制三个的主要阶段:onMeasure()、onLayout()、onDraw()

一、onMeasure

View测量:

ViewRoot.performTraversals() ---> View.measure(int widthMeasureSpec, int heightMeasureSpec) ---> View.onMeasure(int widthMeasureSpec, int heightMeasureSpec)

MeasureSpec由specSize(记录大小)和specMode(记录规格)组成,specMode有三种模式:

1、UNSPECIFIED  //父容器不对View做任何限制,一般适用于系统内部
2、EXACTLY  //父容器希望View的大小由SpecSize决定,对应LayoutParams的match_parent和具体数值两种模式
3、AT_MOST  //View最多只能是SpecSize指定的大小,对应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;  //  View测量后的大小
            break;
        }
        return result;
    }

如果不想使用系统默认的测量方式,可以重写onMeasure(),按自己的意愿定制:

public class MyView extends View {

    ......


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(200, 200);
    }

}

注意:在setMeasuredDimension()方法调用之后,我们才能使用getMeasuredWidth()getMeasuredHeight()来获取视图测量出的宽高,以此之前调用这两个方法得到的值都会是0。

对于DecorView,其MeasureSpec由windows的尺寸和自身的LayoutParams共同决定;
对于普通View,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams共同决定

如果是View,通过measure就完成绘制过程;
如果是ViewGroup,除了完成自己的测量过程,还会遍历去调用子元素的measure方法

    protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        for (int i = 0; i < size; ++i) {
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

二、onLayout

View布局:

onMeasure结束后 ---> View.layout(int l, int t, int r, int b) ---> View.onLayout(boolean changed, int l, int t, int r, int b)
//layout四个参数代表坐标左、上、右、下,坐标相对于父视图而言

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

View的onLayout是一个空方法,因为onLayout()过程是为了确定视图在布局中所在的位置,而这个操作应该是由布局来完成的,即父视图决定子视图的显示位置。既然如此,我们来看下ViewGroup中的onLayout()方法是怎么写的吧,代码如下:

@Override  
protected abstract void onLayout(boolean changed, int l, int t, int r, int b);  

可以看到,ViewGroup中的onLayout()方法竟然是一个抽象方法,这就意味着所有ViewGroup的子类都必须重写这个方法。没错,像LinearLayout、RelativeLayout等布局,都是重写了这个方法,然后在内部按照各自的规则对子视图进行布局的。

例子:

public class SimpleLayout extends ViewGroup {  
  
    public SimpleLayout(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
  
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
        if (getChildCount() > 0) {    //如果SimpleLayout包含子视图,则测量子视图大小
            View childView = getChildAt(0);  
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);  
        }  
    }  
  
    @Override  
    protected void onLayout(boolean changed, int l, int t, int r, int b) {  
        if (getChildCount() > 0) {    //如果SimpleLayout包含子视图,则确定它在SimpleLayout中的位置
            View childView = getChildAt(0);  
            childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());    
            //childView.getMeasuredWidth()和childView.getMeasuredHeight()是在onMeasure中测量出来的
        }  
    }  
}  


<com.example.viewtest.SimpleLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
      
    <ImageView   
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:src="@drawable/ic_launcher"  
        />  
      
</com.example.viewtest.SimpleLayout>  

要想改变ImageView的位置,只要改变childView.layout的四个参数就行了

getWidth()与getMeasureWidth()的区别:

在onLayout()过程结束后,我们就可以调用getWidth()方法和getHeight()方法来获取视图的宽高了。getWidth()方法和getMeasureWidth()方法到底有什么区别呢?

首先getMeasureWidth()方法在measure()过程结束后就可以获取到了,而getWidth()方法要在layout()过程结束后才能获取到。另外,getMeasureWidth()方法中的值是通过setMeasuredDimension()方法来进行设置的,而getWidth()方法中的值则是通过视图右边的坐标减去左边的坐标计算出来的。

观察SimpleLayout中onLayout()方法的代码,这里给子视图的layout()方法传入的四个参数分别是0、0、childView.getMeasuredWidth()和childView.getMeasuredHeight(),因此getWidth()方法得到的值就是childView.getMeasuredWidth() - 0 = childView.getMeasuredWidth() ,所以此时getWidth()方法和getMeasuredWidth() 得到的值就是相同的,但如果你将onLayout()方法中的代码进行如下修改:

@Override  
protected void onLayout(boolean changed, int l, int t, int r, int b) {  
    if (getChildCount() > 0) {  
        View childView = getChildAt(0);  
        childView.layout(0, 0, 200, 200);  
    }  
}  

这样getWidth()方法得到的值就是200 - 0 = 200,不会再和getMeasuredWidth()的值相同了。当然这种做法充分不尊重measure()过程计算出的结果,通常情况下是不推荐这么写的。

3、onDraw

View绘制:

onLayout结束后 ---> View.draw(Canvas canvas) ---> View.onDraw(Canvas canvas)

进去源码可以看到,onDraw是空方法,需要子类去实现

例子:

public class MyView extends View {  
  
    private Paint mPaint;  
  
    public MyView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        mPaint.setColor(Color.YELLOW);  
        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);  
        mPaint.setColor(Color.BLUE);  
        mPaint.setTextSize(20);  
        String text = "Hello View";  
        canvas.drawText(text, 0, getHeight() / 2, mPaint);  
    }  
}  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
  
    <com.example.viewtest.MyView   
        android:layout_width="200dp"  
        android:layout_height="100dp"  
        />  
  
</LinearLayout>  

参考链接:
郭霖
鸿洋

相关文章

网友评论

      本文标题:View的绘制流程小结

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