美文网首页
View的测量

View的测量

作者: Jshu_Zx | 来源:发表于2016-12-05 16:18 被阅读0次

在现实生活中,我们想要画一个图形,要知道他的大小和绘制,才回去进行绘画。同样,在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](https://img.haomeiwen.com/i2129339/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](https://img.haomeiwen.com/i2129339/924c3a4d29bffe5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

好了,暂时就写到这吧。

相关文章

  • 安卓自定义view(二) - 测量

    view的测量过程 之所以先讲view的测量过程,是因为ViewGroup测量的时候是先把他的所有子view测量完...

  • Android-View的测量

    Android View的测量 在绘制View之前,要对整个View进行测量,这个过程就在onMeasure()方...

  • Android通过View生成Bitmap

    已测量过的View生成Bitmap 即经过测量、布局、绘制并显示在界面上的View,此类View无需再次进行测量和...

  • Android自定义View笔记

    View的测量: View测量模式有三种:EXACTLY,AT_MOST,UNSPECIFIED。一般测量都在on...

  • 自定义view

    测量,赋值,绘制 测量:父view调用子view的onMeasure()方法,首先看子view是一个view还是v...

  • View测量流程

    View测量流程简介 ViewGroup继承自View,在View的测量方法measure方法中,调用了onMea...

  • android基础-view的测量,布局,绘制

    知识点 view的测量 view的布局 view的绘制 android中的view显示方式主要就是测量出大小→决定...

  • View的测量、布局和绘制过程中父View(当前View)和子V

    View的测量、布局和绘制过程中父View(当前View)和子View的先后顺序 View的测量、布局和绘制过程中...

  • Android view架构

    view测量与绘制 view的测量MeasureSpec:定义:由SpecMode(int)于SpecSize(i...

  • 第八单元

    一、View绘制流程 1、流程 measure:确定View的测量宽高 layout:根据测量的宽高确定View在...

网友评论

      本文标题:View的测量

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