View的测量

在现实生活中,我们想要画一个图形,要知道他的大小和绘制,才回去进行绘画。同样,在Android系统中,绘制View前,也需要知道View的大小和位置,即告诉系统该画一个多大的View(onMeasure),在哪个位置绘画(onLayout),今天主要了解view的测量。

为了更好地理解View的测量,我分别打印出不同的宽、高和模式

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSize=MeasureSpec.getSize(widthMeasureSpec);
    int widthMode=MeasureSpec.getMode(widthMeasureSpec);
    int heightSize=MeasureSpec.getSize(heightMeasureSpec);
    int heightMode=MeasureSpec.getMode(heightMeasureSpec);

    Log.e("tag","widthMode:"+widthSize+"\nwidthSize:"+widthMode+"\nheightMode:"+heightMode+"\nheightSize:"+heightSize);
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.zx.test.CustomTextView
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:background="@android:color/darker_gray"
        app:text="测量"
        app:textColor="@color/colorPrimary"
        app:textSize="22dp"/>
</RelativeLayout>  

效果图:

N~RBMEAHKL`RQCC70DMHX`5.png

Log打印输出:

 E/tag: widthMode:300
       widthSize:1073741824
       heightMode:1073741824
       heightSize:300

布局文件:

<com.zx.test.CustomTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    app:text="测量"
    app:textColor="@color/colorPrimary"
    app:textSize="22dp"/>

Log打印输出:

E/tag: widthMode:480
      widthSize:1073741824
      heightMode:1073741824
      heightSize:732

布局文件:

 <com.zx.test.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    app:text="测量"
    app:textColor="@color/colorPrimary"
    app:textSize="22dp"/>

效果图:

![NEJF$ZCJWO_$FN%K$AMR]{X.png](http://upload-images.jianshu.io/upload_images/2129339-0b76974301b1a6d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Log打印输出:

 E/tag:widthMode:480
      widthSize:1073741824
      heightMode:-2147483648
      heightSize:732

总结:
SpecMode有三类
EXACTLY 父容器已经检测出view的大小,View的大小就是SpecSize所指定的值
AT_MOST 父容器指定了可用的大小即SpecSize,View的带下不能超过这个值
UNSPECIFIED 父容器不对View有任何的限制,要多大有多大,一般用于系统内部测量

但是,为什么上面的自定义view的width和height设置为wrap_content仍然是填充整个屏幕,来看一下onMeausre源码

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;
}

通过源码发现onMeasure方法最终调用了setMeasuredDimension(int measuredWidth, int measuredHeight)方法,而传入的参数已经是测量过的默认宽和高的值了;我们看看getDefaultSize 方法是怎么计算测量宽高的。根据父控件给予的约束,发现AT_MOST (相当于wrap_content )和EXACTLY (相当于match_parent )两种情况返回的测量宽高都是specSize,而这个specSize正是我们上面说的父控件剩余的宽高,所以默认onMeasure方法中wrap_content 和match_parent 的效果是一样的。

下面重写onMeasure方法,通过判断测量的模式,给出不同的测量值。当specMode为EXACTLY时,直接使用指定的specSize即可,当specMode为其他两种模式时,如果是AT_MOST(wrap_content)模式时,要计算控件的尺寸,控件带下=文本的尺寸+左边距+右边距

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSize=MeasureSpec.getSize(widthMeasureSpec);
    int widthMode=MeasureSpec.getMode(widthMeasureSpec);
    int heightSize=MeasureSpec.getSize(heightMeasureSpec);
    int heightMode=MeasureSpec.getMode(heightMeasureSpec);
    Log.e("tag","widthMode"+widthSize+"\nwidthSize"+widthMode+"\nheightMode"+heightMode+"\nheightSize"+heightSize);

    int width=0;
    int height=0;

    if(widthMode==MeasureSpec.AT_MOST){
        width=mRect.width()+getPaddingLeft()+getPaddingRight();
        Log.e("tag","text width"+width);
    }else{
        width=widthSize;
    }
    if(heightMode==MeasureSpec.AT_MOST){
        height=mRect.height()+getPaddingBottom()+getPaddingTop();
        Log.e("tag","text height"+height);
    }else{
        height=heightSize;
    }

    setMeasuredDimension(width,height);
}

Log打印输出:

E/tag:  widthMode480
        widthSize-2147483648
        heightMode-2147483648
        heightSize732
        text width63
        text height31

效果如图所示:

![H6{(YS]H)7NX$CZ0}FAR_IB.png](http://upload-images.jianshu.io/upload_images/2129339-924c3a4d29bffe5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

好了,暂时就写到这吧。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容