自定义View 测量过程(Measure)

article/2025/8/25 16:55:24

目录

  • 一、作用
  • 二、储备知识
    • 2.2 ViewGroup.LayoutParams
    • 2.3 MeasureSpec
  • 三、measure过程详解
    • 3.1 单一View的measure过程
      • 具体流程
      • 源码分析
      • 源码总结
    • 3.2 ViewGroup的measure过程
      • 测量原理
      • 具体流程
      • 源码分析
      • 流程总结
  • 四、总结


一、作用

测量View的宽 / 高

在某些情况下,需要多次测量(measure)才能确定View最终的宽/高;
该情况下,measure过程后得到的宽 / 高可能不准确;
此处建议:在layout过程中onLayout()去获取最终的宽 / 高

二、储备知识

了解measure过程前,需要3个储备知识:

2.1自定义View基础知识
2.2ViewGroup.LayoutParams类()
2.3MeasureSpecs类

2.2 ViewGroup.LayoutParams

  • 简介

布局参数类
ViewGroup 的子类(RelativeLayout、LinearLayout)有其对应的 ViewGroup.LayoutParams 子类
如:RelativeLayout的 ViewGroup.LayoutParams子类
= RelativeLayoutParams

  • 作用
    指定视图View 的高度(height) 和 宽度(width)等布局参数。
android:layout_height="wrap_content"   //自适应大小  
android:layout_height="match_parent"   //与父视图等高  
android:layout_height="fill_parent"    //与父视图等高  
android:layout_height="100dp"         //精确设置高度值为 100dip  
  • 构造函数
    构造函数 = View的入口,可用于初始化 & 获取自定义属性
// View的构造函数有四种重载public DIY_View(Context context){super(context);}public DIY_View(Context context,AttributeSet attrs){super(context, attrs);}public DIY_View(Context context,AttributeSet attrs,int defStyleAttr ){super(context, attrs,defStyleAttr);// 第三个参数:默认Style
// 默认Style:指在当前Application或Activity所用的Theme中的默认Style
// 且只有在明确调用的时候才会生效,}public DIY_View(Context context,AttributeSet attrs,int defStyleAttr ,int defStyleRes){super(context, attrs,defStyleAttr,defStyleRes);}// 最常用的是1和2
}

2.3 MeasureSpec

在这里插入图片描述

三、measure过程详解

在这里插入图片描述

3.1 单一View的measure过程

具体流程

在这里插入图片描述

源码分析

/*** 源码分析:measure()* 定义:Measure过程的入口;属于View.java类 & final类型,即子类不能重写此方法* 作用:基本测量逻辑的判断*/ public final void measure(int widthMeasureSpec, int heightMeasureSpec) {// 参数说明:View的宽 / 高测量规格...int cacheIndex = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ? -1 :mMeasureCache.indexOfKey(key);if (cacheIndex < 0 || sIgnoreMeasureCache) {onMeasure(widthMeasureSpec, heightMeasureSpec);// 计算视图大小 ->>分析1} else {...}/*** 分析1:onMeasure()* 作用:a. 根据View宽/高的测量规格计算View的宽/高值:getDefaultSize()*      b. 存储测量后的View宽 / 高:setMeasuredDimension()*/ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  // 参数说明:View的宽 / 高测量规格setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),  getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));  // setMeasuredDimension() :获得View宽/高的测量值 ->>分析2// 传入的参数通过getDefaultSize()获得 ->>分析3
}/*** 分析2:setMeasuredDimension()* 作用:存储测量后的View宽 / 高* 注:该方法即为我们重写onMeasure()所要实现的最终目的*/protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {  //参数说明:测量后子View的宽 / 高值// 将测量后子View的宽 / 高值进行传递mMeasuredWidth = measuredWidth;  mMeasuredHeight = measuredHeight;  mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;  } // 由于setMeasuredDimension()的参数是从getDefaultSize()获得的// 下面继续看getDefaultSize()的介绍/*** 分析3:getDefaultSize()* 作用:根据View宽/高的测量规格计算View的宽/高值*/public static int getDefaultSize(int size, int measureSpec) {  // 参数说明:// size:提供的默认大小// measureSpec:宽/高的测量规格(含模式 & 测量大小)// 设置默认大小int result = size; // 获取宽/高测量规格的模式 & 测量大小int specMode = MeasureSpec.getMode(measureSpec);  int specSize = MeasureSpec.getSize(measureSpec);  switch (specMode) {  // 模式为UNSPECIFIED时,使用提供的默认大小 = 参数Sizecase MeasureSpec.UNSPECIFIED:  result = size;  break;  // 模式为AT_MOST,EXACTLY时,使用View测量后的宽/高值 = measureSpec中的Sizecase MeasureSpec.AT_MOST:  case MeasureSpec.EXACTLY:  result = specSize;  break;  }  // 返回View的宽/高值return result;  }    

上面提到,当测试规格的模式(mode)是UNSPECIFIED时,使用的是提供的默认大小(即getDefaultSize()的第一个参数size)。那么,提供的默认大小具体是多少呢?

答:getSuggestedMinimumWidth() / getSuggestedMinimumHeight()。具体请看下面源码分析。

protected int getSuggestedMinimumWidth() {return (mBackground == null) ? mMinWidth : max(mMinWidth,mBackground.getMinimumWidth());
}// 逻辑说明
// 1. 若View无设置背景,那么View的宽度 = mMinWidth// 即android:minWidth属性所指定的值,若无指定则为0.
// 2. 若View设置了背景,View的宽度为mMinWidth和mBackground.getMinimumWidth()中的最大值// 下面继续看mBackground.getMinimumWidth()的源码分析/*** mBackground.getMinimumWidth()源码分析*/ public int getMinimumWidth() {final int intrinsicWidth = getIntrinsicWidth();// 即mBackground.getMinimumWidth()的大小 = 背景图Drawable的原始宽度return intrinsicWidth > 0 ? intrinsicWidth :0 ;// 若无原始宽度,则为0;
}

源码总结

对于单一View的测量流程(Measure)各个方法说明如下所示。
在这里插入图片描述

测量宽高的关键在于getDefaultSize(),该方法的测量逻辑如下图所示。
在这里插入图片描述

3.2 ViewGroup的measure过程

测量原理

从ViewGroup至子View、自上而下遍历进行(即树形递归),通过计算整个ViewGroup中各个View的属性,从而最终确定整个ViewGroup的属性。即:

1.遍历测量所有子View的尺寸(宽/高);
2.合并所有子View的尺寸(宽/高),最终得到ViewGroup父视图的测量值。

在这里插入图片描述

具体流程

在这里插入图片描述

源码分析

/*** 源码分析:measure()* 作用:*    1. 基本测量逻辑的判断;*    2. 调用onMeasure()* 注:与单一View measure过程中讲的measure()一致*/ public final void measure(int widthMeasureSpec, int heightMeasureSpec) {// 仅展示核心代码// ...int cacheIndex = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ? -1 :mMeasureCache.indexOfKey(key);if (cacheIndex < 0 || sIgnoreMeasureCache) {// 调用onMeasure()计算视图大小 -> 分析1onMeasure(widthMeasureSpec, heightMeasureSpec);mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;} else {// ...
}/*** 分析1:onMeasure()* 作用:遍历子View &测量* 注:ViewGroup = 一个抽象类 = 无重写View的onMeasure(),需自身复写**/

ps:

onMeasure()方法的作用是测量View的宽/高值,而不同的ViewGroup(如LinearLayout、RelativeLayout、自定义ViewGroup子类等)具备不同的布局特性,这导致它们的子View测量方法各有不同,所以onMeasure()的实现也会有所不同。
因此,ViewGroup无法对onMeasure()作统一实现。这个也是单一View的measure过程与ViewGroup的measure过程最大的不同。

复写onMeasure()

针对Measure流程,自定义ViewGroup的关键在于:根据需求复写onMeasure(),从而实现子View的测量逻辑。复写onMeasure()的步骤主要分为三步:
1.遍历所有子View及测量:measureChildren()
2.合并所有子View的尺寸大小,最终得到ViewGroup父视图的测量值:需自定义实现
3.存储测量后View宽/高的值:setMeasuredDimension()
具体如下所示。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  //仅展示关键代码...// 步骤1:遍历所有子View & 测量 -> 分析1measureChildren(widthMeasureSpec, heightMeasureSpec);// 步骤2:合并所有子View的尺寸大小,最终得到ViewGroup父视图的测量值void measureCarson{... // 需自定义实现}// 步骤3:存储测量后View宽/高的值setMeasuredDimension(widthMeasure,  heightMeasure);  // 类似单一View的过程,此处不作过多描述
}/*** 分析1:measureChildren()* 作用:遍历子View & 调用measureChild()进行下一步测量*/ protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {// 参数说明:父视图的测量规格(MeasureSpec)final int size = mChildrenCount;final View[] children = mChildren;// 遍历所有子viewfor (int i = 0; i < size; ++i) {final View child = children[i];// 调用measureChild()进行下一步的测量 ->分析2measureChild(child, widthMeasureSpec, heightMeasureSpec);}}/*** 分析2:measureChild()* 作用:1. 计算单个子View的MeasureSpec*      2. 测量每个子View最后的宽 / 高:调用子View的measure()*/ protected void measureChild(View child, int parentWidthMeasureSpec,int parentHeightMeasureSpec) {// 1. 获取子视图的布局参数final LayoutParams lp = child.getLayoutParams();// 2. 根据父视图的MeasureSpec & 布局参数LayoutParams,计算单个子View的MeasureSpecfinal int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,mPaddingLeft + mPaddingRight, lp.width);final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,mPaddingTop + mPaddingBottom, lp.height);// 3. 将计算好的子View的MeasureSpec值传入measure(),进行最后的测量// 下面的流程即类似单一View的过程,此处不作过多描述child.measure(childWidthMeasureSpec, childHeightMeasureSpec);}

流程总结

对于视图组ViewGroup的测量流程(Measure)各个方法说明总结如下所示。
在这里插入图片描述

四、总结

  • 测量流程(Measure)根据视图(View)的类型分为两种情况:单一View和视图组ViewGroup;
  • 二者最大的区别在于:单一View的measure过程对onMeasure()有作统一实现,而ViewGroup的Measuer过程没有;

http://chatgpt.dhexx.cn/article/KVfYym6t.shtml

相关文章

Android MeasureSpec解析

1. MeasureSpec组成 MeasureSpec是View的一个内部类&#xff0c;由一个32位的int值组成&#xff0c;前两位代表SpecMode测量模式&#xff0c;后30位代表SpecSize大小值。 其中测量模式共有三种&#xff1a; EXACTLY&#xff08;确定&#xff09;&#xff1a;父控件为子View指…

使用View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)在在onCreate中获得控件的大小问题

android 在onCreate中获得控件的大小 int w View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); edt_height.measure(w, h); int height edt_height.getMeasuredHeight(); int w…

MeasureSpec中三种模式:UNSPECIFIED,AT_MOST,EXACTLY

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的MeasureSpec来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确模式…

评测指标(metrics)

评测指标(metrics) metric主要用来评测机器学习模型的好坏程度,不同的任务应该选择不同的评价指标, 分类,回归和排序问题应该选择不同的评价函数. 不同的问题应该不同对待,即使都是 分类问题也不应该唯评价函数论,不同问题不同分析. 回归(Regression) 均方误差(MSE) (1) l ( y…

MeasureSpec学习—对Integer.MAX_VALUE 2的认识

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的 MeasureSpec 来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确…

理解Android中的MeasureSpec

文章收藏的好句子&#xff1a;永远要相信美好的事情即将发生。 ps&#xff1a;本文源码是基于 Android Api 31 来分析的 目录 1、MeasureSpec 1、1 SpecMode 1、2 MeasureSpec 的 int 值和 LayoutParams 的对应关系 1、MeasureSpec 我们在 Android 手机上看到的界面&#xff0c…

android Measurespec测量模式

MeasureSpecs 类 1、是一个32位的二进制数&#xff0c;由模式&#xff08;mode&#xff09;和大小&#xff08;size&#xff09;组成&#xff0c; 2、其中&#xff1a;32和31位代表测量模式&#xff08;mode&#xff09;、后30位代表测量大小&#xff08;size&#xff09; 3、…

MeasureSpec源码解读

文章目录 MeasureSpec的源码MeasureSpec与LayoutParams 今天来讲讲MeasureSpec吧。因为他与View的测量流程相关性很大&#xff0c;只有正确的理解了MeasureSpec的工作原理&#xff0c;我们才能更好的自定义View。那么MeasureSpec它的作用是什么呢&#xff1f;一般来说&#xff…

理解 MeasureSpec

在开始本篇文章之前&#xff0c;我们先看一段代码&#xff1a; Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expendSpec MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);super.onMeasure(widthMe…

对MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE 2, MeasureSpec.AT_MOST)的一点理解

之前 遇到ScrollView中嵌入ListView&#xff0c;GridView冲突的解决&#xff08;让ListView全显示出来&#xff09; 链接 网上查找资料&#xff0c;代码大致如下&#xff1a; import android.content.Context; import android.util.AttributeSet; import android.widget.ListV…

View的基本概念与MeasureSpec

1.基本概念 View的绘制是由measuer、layout、draw三个过程才能完整的绘制一个View&#xff0c;其中measure是测量View的宽、高&#xff0c;layout是为了确认View在父容器所在的位置&#xff0c;draw是负责在屏幕上将View绘制出来。View的绘制流程是从ViewRoot的performTraversa…

Android之:了解MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE 2,MeasureSpec.AT_MOST)

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的 MeasureSpec 来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确…

Android开发 MeasureSpec介绍

搬家后的博客链接: IT客栈 www.itkezhan.org 在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的MeasureSpec来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中…

Android-测量规格(MeasureSpec)

目录 一、简介二、组成三、具体使用 一、简介 二、组成 测量规格(MeasureSpec)是由测量模式(mode)和测量大小(size)组成&#xff0c;共32位(int类型)&#xff0c;其中&#xff1a; 测量模式(mode)&#xff1a;占测量规格(MeasureSpec)的高2位&#xff1b;测量大小(size)&…

MeasureSpec学习 - 转

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的 MeasureSpec 来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确…

MeasureSpec介绍

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的MeasureSpec来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确模式…

Android 中MeasureSpec的创建规则

概述 在Android中&#xff0c;View的onMeasure()方法用来对控件进行测量&#xff0c;确定控件的宽高。该方法的两个参数widthMeasureSpec和heightMeasureSpec由父View计算后传入子view的measure()方法&#xff0c;再由子view的measure()方法传入onMeasure()方法&#xff0c;本…

关于google浏览器打不开网页问题之容易被忽略的点

其实google浏览器打不开 网页&#xff0c;原因网上有好多种&#xff0c;包括什么关闭防火墙、取消高级设置LAN单选框等&#xff0c;我也都试了&#xff0c;搞到最后要崩溃了&#xff0c;后来无意中&#xff0c;我输入一个http://baidu.com然后enter管用了&#xff0c;能打开页面…

谷歌浏览器打不开网页

今天起来发现谷歌浏览器和IE都打不开网页了&#xff0c;估计是我电脑代理又被修改了 在谷歌浏览器的设置--> 高级 --> 打开代理设置中 取消勾选即可修复问题。

关于谷歌浏览器打不开的解决方法

关于谷歌浏览器打不开的解决方法 打开Google,搜索&#xff0c;出现下面的问题&#xff0c;怎么解决呢&#xff0c;下面两种方法提供参考。 打开Google,首页显示输入网址&#xff0c;我们可以输入任意一个网址&#xff0c;例如www.baidu.com,然后就可以搜索了。 打开选项-设置…