为了扫除学习中的盲点,尽可能多的覆盖Android知识的边边角角,决定对自定义View做一个稍微全面一点的使用方法总结,在内容上面并没有什么独特的地方,其他大神们的博客上面基本上都有讲这方面的内容,如果你对自定义View很熟了,那么就不用往下看啦~。如果对自定义View不是很熟,或者说很多内容忘记了想复习一下,更或者说是从来没用过,欢迎跟我一起重温这方面的知识,或许我的博文更符合你的胃口呢(*^__^*) 嘻嘻……
1.自定义View
首先我们要明白,为什么要自定义View?主要是Android系统内置的View无法实现我们的需求,我们需要针对我们的业务需求定制我们想要的View。自定义View我们大部分时候只需重写两个函数:onMeasure()、onDraw()。onMeasure负责对当前View的尺寸进行测量,onDraw负责把当前这个View绘制出来。当然了,你还得写至少写2个构造函数:
<code class="language-java hljs has-numbering"> <span class="hljs-keyword">public</span> <span class="hljs-title">MyView</span>(Context context) { <span class="hljs-keyword">super</span>(context); } <span class="hljs-keyword">public</span> <span class="hljs-title">MyView</span>(Context context, AttributeSet attrs) { <span class="hljs-keyword">super</span>(context, attrs); }</code>
1.1.onMeasure
我们自定义的View,首先得要测量宽高尺寸。为什么要测量宽高尺寸?我在刚学自定义View的时候非常无法理解!因为我当时觉得,我在xml文件中已经指定好了宽高尺寸了,我自定义View中有必要再次获取宽高并设置宽高吗?既然我自定义的View是继承自View类,google团队直接在View类中直接把xml设置的宽高获取,并且设置进去不就好了吗?那google为啥让我们做这样的“重复工作”呢?客官别急,马上给您上茶~
在学习Android的时候,我们就知道,在xml布局文件中,我们的layout_width
和layout_height
参数可以不用写具体的尺寸,而是wrap_content
或者是match_parent
。其意思我们都知道,就是将尺寸设置为“包住内容”和“填充父布局给我们的所有空间”。这两个设置并没有指定真正的大小,可是我们绘制到屏幕上的View必须是要有具体的宽高的,正是因为这个原因,我们必须自己去处理和设置尺寸。当然了,View类给了默认的处理,但是如果View类的默认处理不满足我们的要求,我们就得重写onMeasure函数啦~。这里举个例子,比如我们希望我们的View是个正方形,如果在xml中指定宽高为wrap_content
,如果使用View类提供的measure处理方式,显然无法满足我们的需求~。
先看看onMeasure函数原型:
<code class="language-java hljs has-numbering"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onMeasure</span>(<span class="hljs-keyword">int</span> widthMeasureSpec, <span class="hljs-keyword">int</span> heightMeasureSpec) </code><ul style="" class="pre-numbering"><li>1</li></ul>
参数中的widthMeasureSpec
和heightMeasureSpec
是个什么鬼?看起来很像width和height,没错,这两个参数就是包含宽和高的信息。什么?包含?难道还要其他信息?是的!它还包含测量模式,也就是说,一个int整数,里面放了测量模式和尺寸大小。那么一个数怎么放两个信息呢?我们知道,我们在设置宽高时有3个选择:wrap_content
、match_parent
以及指定固定尺寸
,而测量模式也有3种:UNSPECIFIED
,EXACTLY
,AT_MOST
,当然,他们并不是一一对应关系哈,这三种模式后面我会详细介绍,但测量模式无非就是这3种情况,而如果使用二进制,我们只需要使用2个bit就可以做到,因为2个bit取值范围是[0,3]里面可以存放4个数足够我们用了。那么Google是怎么把一个int同时放测量模式和尺寸信息呢?我们知道int型数据占用32个bit,而google实现的是,将int数据的前面2个bit用于区分不同的布局模式,后面30个bit存放的是尺寸的数据。
那我们怎么从int数据中提取测量模式和尺寸呢?放心,不用你每次都要写一次移位<<
和取且&
操作,Android内置类MeasureSpec帮我们写好啦~,我们只需按照下面方法就可以拿到啦:
<code class="language-java hljs has-numbering"><span class="hljs-keyword">int</span> widthMode = MeasureSpec.getMode(widthMeasureSpec); <span class="hljs-keyword">int</span> widthSize = MeasureSpec.getSize(widthMeasureSpec);</code>
爱思考的你肯定会问,既然我们能通过widthMeasureSpec拿到宽度尺寸大小,那我们还要测量模式干嘛?测量模式会不会是多余的?请注意:这里的的尺寸大小并不是最终我们的View的尺寸大小,而是父View提供的参考大小。我们看看测量模式,测量模式是干啥用的呢?
测量模式 | 表示意思 |
---|---|
UNSPECIFIED | 父容器没有对当前View有任何限制,当前View可以任意取尺寸 |
EXACTLY | 当前的尺寸就是当前View应该取的尺寸 |
AT_MOST | 当前尺寸是当前View能取的最大尺寸 |
而上面的测量模式跟我们的布局时的wrap_content
、match_parent
以及写成固定的尺寸有什么对应关系呢?
match_parent
—>EXACTLY。怎么理解呢?match_parent
就是要利用父View给我们提供的所有剩余空间,而父View剩余空间是确定的,也就是这个测量模式的整数里面存放的尺寸。
wrap_content
—>AT_MOST。怎么理解:就是我们想要将大小设置为包裹我们的view内容,那么尺寸大小就是父View给我们作为参考的尺寸,只要不超过这个尺寸就可以啦,具体尺寸就根据我们的需求去设定。
固定尺寸(如100dp)
—>EXACTLY。用户自己指定了尺寸大小,我们就不用再去干涉了,当然是以指定的大小为主啦。
1.2.动手重写onMeasure函数
上面讲了太多理论,我们实际操作一下吧,感受一下onMeasure的使用,假设我们要实现这样一个效果:将当前的View以正方形的形式显示,即要宽高相等,并且默认的宽高值为100像素。就可以这些编写:
<code class="language-java hljs has-numbering"> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getMySize</span>(<span class="hljs-keyword">int</span> defaultSize, <span class="hljs-keyword">int</span> measureSpec) { <span class="hljs-keyword">int</span> mySize = defaultSize; <span class="hljs-keyword">int</span> mode = MeasureSpec.getMode(measureSpec); <span class="hljs-keyword">int</span> size = MeasureSpec.getSize(measureSpec); <span class="hljs-keyword">switch</span> (mode) { <span class="hljs-keyword">case</span> MeasureSpec.UNSPECIFIED: {<span class="hljs-comment">//如果没有指定大小,就设置为默认大小</span> mySize = defaultSize; <span class="hljs-keyword">break</span>; } <span class="hljs-keyword">case</span> MeasureSpec.AT_MOST: {<span class="hljs-comment">//如果测量模式是最大取值为size</span> <span class="hljs-comment">//我们将大小取最大值,你也可以取其他值</span> mySize = size; <span class="hljs-keyword">break</span>; } <span class="hljs-keyword">case</span> MeasureSpec.EXACTLY: {<span class="hljs-comment">//如果是固定的大小,那就不要去改变它</span> mySize = size; <span class="hljs-keyword">break</span>; } } <span class="hljs-keyword">return</span> mySize; } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onMeasure</span>(<span class="hljs-keyword">int</span> widthMeasureSpec, <span class="hljs-keyword">int</span> heightMeasureSpec) { <span class="hljs-keyword">super</span>.onMeasure(widthMeasureSpec, heightMeasureSpec); <span class="hljs-keyword">int</span> width = getMySize(<span class="hljs-number">100</span>, widthMeasureSpec); <span class="hljs-keyword">int</span> height = getMySize(<span class="hljs-number">100</span>, heightMeasureSpec); <span class="hljs-keyword">if</span> (width < height) { height = width; } <span class="hljs-keyword">else</span> { width = height; } setMeasuredDimension(width, height); } </code>
我们设置一下布局
<code class="language-xml hljs has-numbering"> <span class="hljs-tag"><<span class="hljs-title">com.hc.studyview.MyView </span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"100dp"</span> <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"#ff0000"</span> /></span> </code>
看看使用了我们自己定义的onMeasure函数后的效果:
而如果我们不重写onMeasure,效果则是如下:
1.3.重写onDraw
上面我们学会了自定义尺寸大小,那么尺寸我们会设定了,接下来就是把我们想要的效果画出来吧~绘制我们想要的效果很简单,直接在画板Canvas对象上绘制就好啦,过于简单,我们以一个简单的例子去学习:假设我们需要实现的是,我们的View显示一个圆形,我们在上面已经实现了宽高尺寸相等的基础上,继续往下做:
<code class="language-java hljs has-numbering"> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onDraw</span>(Canvas canvas) { <span class="hljs-comment">//调用父View的onDraw函数,因为View这个类帮我们实现了一些</span> <span class="hljs-comment">// 基本的而绘制功能,比如绘制背景颜色、背景图片等</span> <span class="hljs-keyword">super</span>.onDraw(canvas); <span class="hljs-keyword">int</span> r = getMeasuredWidth() / <span class="hljs-number">2</span>;<span class="hljs-comment">//也可以是getMeasuredHeight()/2,本例中我们已经将宽高设置相等了</span> <span class="hljs-comment">//圆心的横坐标为当前的View的左边起始位置+半径</span> <span class="hljs-keyword">int</span> centerX = getLeft() + r; <span class="hljs-comment">//圆心的纵坐标为当前的View的顶部起始位置+半径</span> <span class="hljs-keyword">int</span> centerY = getTop() + r; Paint paint = <span class="hljs-keyword">new</span> Paint(); paint.setColor(Color.GREEN); <span class="hljs-comment">//开始绘制</span> canvas.drawCircle(centerX, centerY, r, paint); } </code>
1.4.自定义布局属性
如果有些属性我们希望由用户指定,只有当用户不指定的时候才用我们硬编码的值,比如上面的默认尺寸,我们想要由用户自己在布局文件里面指定该怎么做呢?那当然是通我们自定属性,让用户用我们定义的属性啦~
首先我们需要在res/values/styles.xml
文件(如果没有请自己新建)里面声明一个我们自定义的属性:
<code class="language-xml hljs has-numbering"><span class="hljs-tag"><<span class="hljs-title">resources</span>></span> <span class="hljs-comment"><!--name为声明的"属性集合"名,可以随便取,但是最好是设置为跟我们的View一样的名称--></span> <span class="hljs-tag"><<span class="hljs-title">declare-styleable</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"MyView"</span>></span> <span class="hljs-comment"><!--声明我们的属性,名称为default_size,取值类型为尺寸类型(dp,px等)--></span> <span class="hljs-tag"><<span class="hljs-title">attr</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"default_size"</span> <span class="hljs-attribute">format</span>=<span class="hljs-value">"dimension"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">declare-styleable</span>></span> <span class="hljs-tag"></<span class="hljs-title">resources</span>></span> </code>
接下来就是在布局文件用上我们的自定义的属性啦~
<code class="language-xml hljs has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="utf-8"?></span> <span class="hljs-tag"><<span class="hljs-title">LinearLayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">xmlns:hc</span>=<span class="hljs-value">"http://schemas.android.com/apk/res-auto"</span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span>></span> <span class="hljs-tag"><<span class="hljs-title">com.hc.studyview.MyView </span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"100dp"</span> <span class="hljs-attribute">hc:default_size</span>=<span class="hljs-value">"100dp"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">LinearLayout</span>></span> </code>
注意:需要在根标签(LinearLayout)里面设定命名空间,命名空间名称可以随便取,比如hc
,命名空间后面取得值是固定的:"http://schemas.android.com/apk/res-auto"
最后就是在我们的自定义的View里面把我们自定义的属性的值取出来,在构造函数中,还记得有个AttributeSet属性吗?就是靠它帮我们把布局里面的属性取出来:
<code class="language-java hljs has-numbering"> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> defalutSize; <span class="hljs-keyword">public</span> <span class="hljs-title">MyView</span>(Context context, AttributeSet attrs) { <span class="hljs-keyword">super</span>(context, attrs); <span class="hljs-comment">//第二个参数就是我们在styles.xml文件中的<declare-styleable>标签</span> <span class="hljs-comment">//即属性集合的标签,在R文件中名称为R.styleable+name</span> TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); <span class="hljs-comment">//第一个参数为属性集合里面的属性,R文件名称:R.styleable+属性集合名称+下划线+属性名称</span> <span class="hljs-comment">//第二个参数为,如果没有设置这个属性,则设置的默认的值</span> defalutSize = a.getDimensionPixelSize(R.styleable.MyView_default_size, <span class="hljs-number">100</span>); <span class="hljs-comment">//最后记得将TypedArray对象回收</span> a.recycle(); } </code>
最后,把MyView的完整代码附上:
<code class="language-java hljs has-numbering"><span class="hljs-keyword">package</span> com.hc.studyview; <span class="hljs-keyword">import</span> android.content.Context; <span class="hljs-keyword">import</span> android.content.res.TypedArray; <span class="hljs-keyword">import</span> android.graphics.Canvas; <span class="hljs-keyword">import</span> android.graphics.Color; <span class="hljs-keyword">import</span> android.graphics.Paint; <span class="hljs-keyword">import</span> android.util.AttributeSet; <span class="hljs-keyword">import</span> android.util.Log; <span class="hljs-keyword">import</span> android.view.MotionEvent; <span class="hljs-keyword">import</span> android.view.View; <span class="hljs-javadoc">/** * Package com.hc.studyview * Created by HuaChao on 2016/6/3. */</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span> {</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> defalutSize; <span class="hljs-keyword">public</span> <span class="hljs-title">MyView</span>(Context context) { <span class="hljs-keyword">super</span>(context); } <span class="hljs-keyword">public</span> <span class="hljs-title">MyView</span>(Context context, AttributeSet attrs) { <span class="hljs-keyword">super</span>(context, attrs); <span class="hljs-comment">//第二个参数就是我们在styles.xml文件中的<declare-styleable>标签</span> <span class="hljs-comment">//即属性集合的标签,在R文件中名称为R.styleable+name</span> TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); <span class="hljs-comment">//第一个参数为属性集合里面的属性,R文件名称:R.styleable+属性集合名称+下划线+属性名称</span> <span class="hljs-comment">//第二个参数为,如果没有设置这个属性,则设置的默认的值</span> defalutSize = a.getDimensionPixelSize(R.styleable.MyView_default_size, <span class="hljs-number">100</span>); <span class="hljs-comment">//最后记得将TypedArray对象回收</span> a.recycle(); } <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getMySize</span>(<span class="hljs-keyword">int</span> defaultSize, <span class="hljs-keyword">int</span> measureSpec) { <span class="hljs-keyword">int</span> mySize = defaultSize; <span class="hljs-keyword">int</span> mode = MeasureSpec.getMode(measureSpec); <span class="hljs-keyword">int</span> size = MeasureSpec.getSize(measureSpec); <span class="hljs-keyword">switch</span> (mode) { <span class="hljs-keyword">case</span> MeasureSpec.UNSPECIFIED: {<span class="hljs-comment">//如果没有指定大小,就设置为默认大小</span> mySize = defaultSize; <span class="hljs-keyword">break</span>; } <span class="hljs-keyword">case</span> MeasureSpec.AT_MOST: {<span class="hljs-comment">//如果测量模式是最大取值为size</span> <span class="hljs-comment">//我们将大小取最大值,你也可以取其他值</span> mySize = size; <span class="hljs-keyword">break</span>; } <span class="hljs-keyword">case</span> MeasureSpec.EXACTLY: {<span class="hljs-comment">//如果是固定的大小,那就不要去改变它</span> mySize = size; <span class="hljs-keyword">break</span>; } } <span class="hljs-keyword">return</span> mySize; } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onMeasure</span>(<span class="hljs-keyword">int</span> widthMeasureSpec, <span class="hljs-keyword">int</span> heightMeasureSpec) { <span class="hljs-keyword">super</span>.onMeasure(widthMeasureSpec, heightMeasureSpec); <span class="hljs-keyword">int</span> width = getMySize(defalutSize, widthMeasureSpec); <span class="hljs-keyword">int</span> height = getMySize(defalutSize, heightMeasureSpec); <span class="hljs-keyword">if</span> (width < height) { height = width; } <span class="hljs-keyword">else</span> { width = height; } setMeasuredDimension(width, height); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onDraw</span>(Canvas canvas) { <span class="hljs-comment">//调用父View的onDraw函数,因为View这个类帮我们实现了一些</span> <span class="hljs-comment">// 基本的而绘制功能,比如绘制背景颜色、背景图片等</span> <span class="hljs-keyword">super</span>.onDraw(canvas); <span class="hljs-keyword">int</span> r = getMeasuredWidth() / <span class="hljs-number">2</span>;<span class="hljs-comment">//也可以是getMeasuredHeight()/2,本例中我们已经将宽高设置相等了</span> <span class="hljs-comment">//圆心的横坐标为当前的View的左边起始位置+半径</span> <span class="hljs-keyword">int</span> centerX = getLeft() + r; <span class="hljs-comment">//圆心的纵坐标为当前的View的顶部起始位置+半径</span> <span class="hljs-keyword">int</span> centerY = getTop() + r; Paint paint = <span class="hljs-keyword">new</span> Paint(); paint.setColor(Color.GREEN); <span class="hljs-comment">//开始绘制</span> canvas.drawCircle(centerX, centerY, r, paint); } } </code>
代码中的注释我已经写得很详细,不再对每一行代码进行讲解。上面的onMeasure将子View测量好了,以及把自己的尺寸也设置好了,接下来我们去摆放子View吧~
<code class="language-java hljs has-numbering"> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLayout</span>(<span class="hljs-keyword">boolean</span> changed, <span class="hljs-keyword">int</span> l, <span class="hljs-keyword">int</span> t, <span class="hljs-keyword">int</span> r, <span class="hljs-keyword">int</span> b) { <span class="hljs-keyword">int</span> count = getChildCount(); <span class="hljs-comment">//记录当前的高度位置</span> <span class="hljs-keyword">int</span> curHeight = t; <span class="hljs-comment">//将子View逐个摆放</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < count; i++) { View child = getChildAt(i); <span class="hljs-keyword">int</span> height = child.getMeasuredHeight(); <span class="hljs-keyword">int</span> width = child.getMeasuredWidth(); <span class="hljs-comment">//摆放子View,参数分别是子View矩形区域的左、上、右、下边</span> child.layout(l, curHeight, l + width, curHeight + height); curHeight += height; } }</code>
我们测试一下,将我们自定义的ViewGroup里面放3个Button ,将这3个Button的宽度设置不一样,把我们的ViewGroup的宽高都设置为包裹内容wrap_content
,为了看的效果明显,我们给ViewGroup加个背景:
<code class="language-xml hljs has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="utf-8"?></span> <span class="hljs-tag"><<span class="hljs-title">LinearLayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span>></span> <span class="hljs-tag"><<span class="hljs-title">com.hc.studyview.MyViewGroup </span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"#ff9900"</span>></span> <span class="hljs-tag"><<span class="hljs-title">Button </span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"100dp"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:text</span>=<span class="hljs-value">"btn"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">Button </span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"200dp"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:text</span>=<span class="hljs-value">"btn"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">Button </span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"50dp"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:text</span>=<span class="hljs-value">"btn"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">com.hc.studyview.MyViewGroup</span>></span> <span class="hljs-tag"></<span class="hljs-title">LinearLayout</span>></span> </code>
看看最后的效果吧~
是不是很激动~我们自己也可以实现LinearLayout的效果啦~~~~
最后附上MyViewGroup的完整源码:
<code class="language-java hljs has-numbering"><span class="hljs-keyword">package</span> com.hc.studyview; <span class="hljs-keyword">import</span> android.content.Context; <span class="hljs-keyword">import</span> android.util.AttributeSet; <span class="hljs-keyword">import</span> android.view.MotionEvent; <span class="hljs-keyword">import</span> android.view.View; <span class="hljs-keyword">import</span> android.view.ViewGroup; <span class="hljs-javadoc">/** * Package com.hc.studyview * Created by HuaChao on 2016/6/3. */</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyViewGroup</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ViewGroup</span> {</span> <span class="hljs-keyword">public</span> <span class="hljs-title">MyViewGroup</span>(Context context) { <span class="hljs-keyword">super</span>(context); } <span class="hljs-keyword">public</span> <span class="hljs-title">MyViewGroup</span>(Context context, AttributeSet attrs) { <span class="hljs-keyword">super</span>(context, attrs); } <span class="hljs-javadoc">/*** * 获取子View中宽度最大的值 */</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getMaxChildWidth</span>() { <span class="hljs-keyword">int</span> childCount = getChildCount(); <span class="hljs-keyword">int</span> maxWidth = <span class="hljs-number">0</span>; <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < childCount; i++) { View childView = getChildAt(i); <span class="hljs-keyword">if</span> (childView.getMeasuredWidth() > maxWidth) maxWidth = childView.getMeasuredWidth(); } <span class="hljs-keyword">return</span> maxWidth; } <span class="hljs-javadoc">/*** * 将所有子View的高度相加 **/</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getTotleHeight</span>() { <span class="hljs-keyword">int</span> childCount = getChildCount(); <span class="hljs-keyword">int</span> height = <span class="hljs-number">0</span>; <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < childCount; i++) { View childView = getChildAt(i); height += childView.getMeasuredHeight(); } <span class="hljs-keyword">return</span> height; } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onMeasure</span>(<span class="hljs-keyword">int</span> widthMeasureSpec, <span class="hljs-keyword">int</span> heightMeasureSpec) { <span class="hljs-keyword">super</span>.onMeasure(widthMeasureSpec, heightMeasureSpec); <span class="hljs-comment">//将所有的子View进行测量,这会触发每个子View的onMeasure函数</span> <span class="hljs-comment">//注意要与measureChild区分,measureChild是对单个view进行测量</span> measureChildren(widthMeasureSpec, heightMeasureSpec); <span class="hljs-keyword">int</span> widthMode = MeasureSpec.getMode(widthMeasureSpec); <span class="hljs-keyword">int</span> widthSize = MeasureSpec.getSize(widthMeasureSpec); <span class="hljs-keyword">int</span> heightMode = MeasureSpec.getMode(heightMeasureSpec); <span class="hljs-keyword">int</span> heightSize = MeasureSpec.getSize(heightMeasureSpec); <span class="hljs-keyword">int</span> childCount = getChildCount(); <span class="hljs-keyword">if</span> (childCount == <span class="hljs-number">0</span>) {<span class="hljs-comment">//如果没有子View,当前ViewGroup没有存在的意义,不用占用空间</span> setMeasuredDimension(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>); } <span class="hljs-keyword">else</span> { <span class="hljs-comment">//如果宽高都是包裹内容</span> <span class="hljs-keyword">if</span> (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) { <span class="hljs-comment">//我们将高度设置为所有子View的高度相加,宽度设为子View中最大的宽度</span> <span class="hljs-keyword">int</span> height = getTotleHeight(); <span class="hljs-keyword">int</span> width = getMaxChildWidth(); setMeasuredDimension(width, height); } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (heightMode == MeasureSpec.AT_MOST) {<span class="hljs-comment">//如果只有高度是包裹内容</span> <span class="hljs-comment">//宽度设置为ViewGroup自己的测量宽度,高度设置为所有子View的高度总和</span> setMeasuredDimension(widthSize, getTotleHeight()); } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (widthMode == MeasureSpec.AT_MOST) {<span class="hljs-comment">//如果只有宽度是包裹内容</span> <span class="hljs-comment">//宽度设置为子View中宽度最大的值,高度设置为ViewGroup自己的测量值</span> setMeasuredDimension(getMaxChildWidth(), heightSize); } } } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLayout</span>(<span class="hljs-keyword">boolean</span> changed, <span class="hljs-keyword">int</span> l, <span class="hljs-keyword">int</span> t, <span class="hljs-keyword">int</span> r, <span class="hljs-keyword">int</span> b) { <span class="hljs-keyword">int</span> count = getChildCount(); <span class="hljs-comment">//记录当前的高度位置</span> <span class="hljs-keyword">int</span> curHeight = t; <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < count; i++) { View child = getChildAt(i); <span class="hljs-keyword">int</span> height = child.getMeasuredHeight(); <span class="hljs-keyword">int</span> width = child.getMeasuredWidth(); child.layout(l, curHeight, l + width, curHeight + height); curHeight += height; } } } </code>
好啦~自定义View的学习到此结束,是不是发现自定义View如此简单呢?