onMeasure-measureVertical方法 该方法会进行下面的几个步骤 声明使用变量 获取子View总高度 计算LinearLayout的高度 将子View中的Weight属性转换成高度,再重新
在继承ViewGroup类时,需要重写两个方法,分别是onMeasure和onLayout。 实际上,view的测量工作在onMeasure(int, int)方法中完成。因此,只有onMeasure(int, int)方法可以且必须被重写。 3,解析onMeasure(int, int)方法 void android.view.View.onMeasure(int widthMeasureSpec, int heightMeasureSpec 调用父view的onMeasure(int, int)是合法有效的用法。 view的基本测量数据默认取其背景尺寸,除非允许更大的尺寸。 子view必须重写onMeasure(int, int)来提供其内容更加准确的测量数值。
将LinearLayout中代码Copy了一份存在本地,然后再在里面加了几个子View,打印出来LinearLayout.onMeasure中的那些变量的值 如下图所示,LinearLayout中有4
mTotalLength:表示所有子View所需要的高度 maxWidth:表示这个LinearLayout的宽度,最后设置宽度的时候用到的 childState: alertnativeMaxWidth:没有Weight属性的子View中,最大的宽度 weightedMaxWidth :有weight属性的子View中,最大的宽度 allFillParent :是否所有的子View都是fillParent的 totalWeight:所有子View的weight和,到时候会用来计算剩余空间的分配 count:子View的总数 widthMode:宽度的MeasureSpec的Mode heightMode:高度的MeasureSpec的Mode matchWidth: baselineChildIndex: useLargestChild:是否用最高的子View大小 largestChildHeight:子View中最高的大小
for (int i = 0; i < count; ++i) { final View child = getVirtualChildAt(i); if (child == null) { mTotalLength += measureNullChild(i); continue; } if (child.getVisibility() == View.GONE) {
if (!allFillParent && widthMode != MeasureSpec.EXACTLY) { maxWidth = alternativeMaxWidth; } maxWidth += mPaddingLeft + mPaddingRight; // Check against our minimum width maxWidth = Math.max(maxWidth, getSuggestedMinimumWidt
// Either expand children with weight to take up available space or // shrink them if they extend beyond our current bounds int delta = heightSize - mTotalLength; if (delta != 0 && totalWeight > 0.0f) { float weightSum = mWeightSum > 0.
如果这个View是通过LayoutInflater来构建的,会报: java.lang.NullPointerException at android.widget.RelativeLayout.onMeasure
前言: 自定义控件的三大方法: 测量: onMeasure(): 测量自己的大小,为正式布局提供建议 布局: onLayout(): 使用layout()函数对所有子控件布局 绘制: onDraw (): 根据布局的位置绘图 onDraw() 里面是绘制的操作,可以看下其他的文章,下面来了解 onMeasure()和onLayout()方法。 一、onMeasure()、测量 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 参数即父类传过来的两个宽高的 : MeasureSpec.AT_MOST = 2 MeasureSpec.EXACTLY = 1 MeasureSpec.UNSPECIFIED = 0 上面我们知道了 onMeasure 这个方法和onMeasure()方法类似。其实这个方法的作用就是 设置当前View的宽高。 (2)、 ? 这个方法就和 ?
类的onMeasure总都做了什么? onMeasure方法了: [java] viewplaincopy 1. /** 2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 5. * 这个方法必须由onMeasure(int, int)来调用,来存储测量的宽,高值。 3. */ 4. 而具体的测量任务就交给我们在子类中重写的onMeasure方法。 measureChildren() [java] viewplaincopy 1. /** 2.
正文 Android自定义View时常重写三个方法onMeasure和onLayout以及onDraw。 他们的作用 onMeasure 计算当前View的宽高 onLayout 处理子View的布局 onDraw 绘制当前View 调用的顺序为onMeasure–>onLayout–>onDraw 就需要先调用requestLayout 再调用invalidate onMeasure细要 @Override protected void onMeasure(int widthMeasureSpec , int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 获取宽-测量规则的模式和大小 widthSize, mHeight); } else { setMeasuredDimension(widthSize, heightSize); } } 我们可以重写onMeasure
一、onLayout、onMeasure和onDraw方法 1.1 onMeasure(int widthMeasureSpec, int heightMeasureSpec) onMeasure方法用于测量 方法 在CircleView类中,重写onMeasure方法,根据MeasureSpec来计算并设置View的宽高。 通过这个案例,我们可以看到,onMeasure、onLayout和onDraw这三个方法在自定义View中的重要作用。 方法 在CustomLayout类中,重写onMeasure方法,根据MeasureSpec来计算并设置ViewGroup的宽高。 onMeasure方法用于测量View的大小,onDraw方法用于绘制View的内容,onLayout方法用于确定View的位置。
整个 Design 图形化操作界面没了 , 报错信息如下 : java.lang.IllegalStateException: View with id -1: com.example.MyView#onMeasure () 方法 中 没有调用 setMeasuredDimension() 方法导致的 ; 实现 onMeasure() 方法时,需要 调用 setMeasuredDimension() 来设置 View 以下是一个示例,在这个示例中,自定义 View 的 onMeasure() 方法调用了 setMeasuredDimension() 来设置 View 的测量宽度和高度。 : View(context, attrs) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 实现 onMeasure() 方法时,必须在最后 调用 setMeasuredDimension() 来设置视图的测量宽度和高度。
= MEASURED_DIMENSION_SET) { throw new IllegalStateException("onMeasure() did not set 函数,因此真正有变数的是onMeasure函数,onMeasure的默认实现很简单,源码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec ,即可完成View的测量,当然你也可以重载onMeasure,并调用setMeasuredDimension来设置任意大小的布局,但一般不这么做,因为这种做法太“专政”,至于为何“专政”,读完本文就会明白 对于ViewGroup的子类而言,往往会重载onMeasure函数负责其children的measure工作,重载时不要忘记调用setMeasuredDimension来设置自身的mMeasuredWidth 如果我们在layout的时候不需要依赖子视图的大小,那么不重载onMeasure也可以,但是必须重载onLayout来安排子视图的位置,这在下一篇博客中会介绍。
继续查看View类的onMeasure()方法: ? onMeasure方法 其实View类的onMeasure方法一般是由其子类来重写的。 如对于用来应用程序窗口的顶层视图的DecorView类来说,它是通过父类FrameLayout来重写祖父类View的onMeasure方法的,接下来我们就分析FrameLayout类的onMeasure 分析onMeasure方法,我们先从子类DecorView的onMeasure方法入手,这个方法主要是调整了两个入参高度和宽度,然后调用其父类的onMeasure方法。 ? DecorView的onMeasure方法 再看FrameLayout的onMeasure方法,主要是遍历所有的子View进行测量,然后设置高度、宽度。 ? View的绘制主流程 在measure方法中,会调用onMeasure方法,在onMeasure方法中会对所有的子元素进行measure过程,这个时候measure流程就从父容器传递给子容器,这样就完成了一次测量
() 动手重写onMeasure函数 onDraw() ---- 自定义View 首先我们要明白,为什么要自定义View? 自定义View我们大部分时候只需重写两个函数:onMeasure()、onDraw()。onMeasure负责对当前View的尺寸进行测量,onDraw负责把当前这个View绘制出来。 当然了,View类给了默认的处理,但是如果View类的默认处理不满足我们的要求,我们就得重写 onMeasure函数啦。 先看看onMeasure函数原型: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 参数中的 widthMeasureSpec ---- 动手重写onMeasure函数 上面讲了太多理论,我们实际操作一下吧,感受一下onMeasure的使用,假设我们要实现这样一个效果:将当前的View以正方形的形式显示,即要宽高相等,并且默认的宽高值为
拿今天要讲的自定义View来说,其实我们在写自定义View的时候,需要去重写系统为我们设计好的一系列方法(如下所示): onMeasure -> onLayout -> onDraw 这些方法。 那我们今天就先聊一聊第一步onMeasure。 2 盖房子的时候,我们先会确定一块地方,然后用工具量好建房子的地面范围,量好的这块地就是我们可以盖房子的范围。 这个过程与我们在自定义View的onMeasure过程是类似的,都是确定一块我们接下来工作的范围。 测量的过程中,我们不单会测量整个房子的大小,我们要综合考虑房子里面的房间格局和大小。 谷歌的工程师看到这一切,深深的吸了一口气,然后缓慢的吐出来,转身回到工位上,盯着想了很久的代码: protected void onMeasure() { // TODO 我该咋弄捏? 好了,onMeasure阶段就讲到这里了。下一节onLayout再见。拜拜~ 动动小手,关注一下不迷路哦~
研究了俩星期硬是没有研究view的measure,接下来终于可以来好好研究研究了) 先总体分析一下view的measure,发现关于view的measure研究主要涉及到两个方法和一个类 两个方法是 onMeasure 接下来对onMeasure进行一个分析,onMeasure属于生命周期方法,先来看一下onMeasure方法的实现与介绍。 Chapter Two,onMeasure方法简介 1>,onMeasure方法用于测量view以及其内容的宽高,得到一组宽和高的值measurewidth/heigh,在调用measure方法时会调用 (measure方法属于view的public方法),View的子类应该覆写onMeasure方法来提供一组准确有效的测量值。 方法重新进行测量,对于onMeasure方法的调用,有个版本界限,19版本以下是不论缓存区有没有存储值都会强制调用onMeasure,19版本以上不会。
---- 接下来让我们开启自定义控件之路 关于自定义控件,一般辉遵循一下几个套路 首先重写 onMeasure() 方法 其次重写 onDraw() 方法 总所周知 onMeasure() 方法是用来重新测量 所以这时就需要重写 onMeasure 方法,设定其宽高相等。 ---- 那么该如何重写 onMeasure() 方法呢? 首先把 onMeasure() 打出来 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 这时大家不眠会好奇,明明是重绘大小,那么给我提供宽高就行了呀 ---- 开始重写 onMeasure() 方法 首先,无论是 width 还是 height ,我们都得先判断类型,再去计算大小,so~ 咱先写个方法专门用于计算并返回大小。
但是他内部通过调用onMeasure进行了测量。那么我们只需要看onMeasure的具体实现即可。 View的onMeasure代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension 不管你走不走onMeasure这个方法,它最后都会走setMeasuredDimensionRaw这个方法。也就是设置测量的宽和高。 这个方法看完了。 我们回到onMeasure中在看看其他方法到底处理了什么? ViewGroup的measure流程 在viewGroup没有onMeasure方法,但是有MeasureChildren方法。