可以说重载onMeasure(),onLayout(),onDraw()三个函数构建了自定义View的外观形象。再加上onTouchEvent()等重载视图的行为,可以构建任何我们需要的可感知到的自定义View。
本节我们探索自定义View中onMeasure()起到了什么样的作用,题外要插的一句是,Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益。
我们知道,不管是自定义View还是系统提供的TextView这些,它们都必须放置在LinearLayout等一些ViewGroup中,因此理论上我们可以很好的理解onMeasure(),onLayout(),onDraw()这三个函数:1.View本身大小多少,这由onMeasure()决定;2.View在ViewGroup中的位置如何,这由onLayout()决定;3.绘制View,onDraw()定义了如何绘制这个View。
首先我们看看TextView.java中的onMeasure()源码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
...
if (widthMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
width = widthSize;
} else {
if (mLayout != null && mEllipsize == null) {
des = desired(mLayout);
}
...
setMeasuredDimension(width, height);
了解了这两个参数的来源,还要知道这两个值的作用。我们只取heightMeasureSpec作说明。这个值由高32位和低16位组成,高32位保存的值叫specMode,可以通过如代码中所示的MeasureSpec.getMode()获取;低16位为specSize,同样可以由MeasureSpec.getSize()获取。那么specMode和specSize的作用有是什么呢?要想知道这一点,我们需要知道代码中的最后一行,所有的View的onMeasure()的最后一行都会调用setMeasureDimension()函数的作用——这个函数调用中传进去的值是View最终的视图大小。也就是说onMeasure()中之前所作的所有工作都是为了最后这一句话服务的。
我们知道在ViewGroup中,给View分配的空间大小并不是确定的,有可能随着具体的变化而变化,而这个变化的条件就是传到specMode中决定的,specMode一共有三种可能:
MeasureSpec.EXACTLY:父视图希望子视图的大小应该是specSize中指定的。
MeasureSpec.AT_MOST:子视图的大小最多是specSize中指定的值,也就是说不建议子视图的大小超过specSize中给定的值。
MeasureSpec.UNSPECIFIED:我们可以随意指定视图的大小。
由TextView中源码也可以知道这个值的设计意义是为了根据ViewGroup中具体能够提供的空间大小来指定子View的视图大小。
通过以上这些分析,可以知道视图最终的大小由父视图,子视图以及程序员根据需要决定,良好的设计一般会根据子视图的measureSpec设置合适的布局大小。
讲到上述这些内容,可能已经了解了如何去使用onMeasure来设置我们的视图的大小,但还有一个疑惑的地方,EXACTLY,AT_MOST,UNSPECIFIED和layout_是如何对应的呢?什么情况下对应什么值呢?
我们通过如下例子,稍作了解:
package com.sean.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
Log.d("MyView","------", new Throwable());
int speSize = MeasureSpec.getSize(heightMeasureSpec);
int speMode = MeasureSpec.getMode(heightMeasureSpec);
Log.d("MyView", "---speSize = " + speSize + "");
Log.d("MyView", "---speMode = " + speMode + "");
if(speMode == MeasureSpec.AT_MOST){
Log.d("MyView", "---AT_MOST---");
}
if(speMode == MeasureSpec.EXACTLY){
Log.d("MyView", "---EXACTLY---");
}
if(speMode == MeasureSpec.UNSPECIFIED){
Log.d("MyView", "---UNSPECIFIED---");
}
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), speSize);
}
}
widthMeasureSpecD/MyView ( 3506): java.lang.Throwable
D/MyView ( 3506): at com.sean.myview.MyView.onMeasure(MyView.java:18)
D/MyView ( 3506): at android.view.View.measure(View.java:15775)
D/MyView ( 3506): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
D/MyView ( 3506): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
D/MyView ( 3506): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1059)
D/MyView ( 3506): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
D/MyView ( 3506): at android.view.View.measure(View.java:15775)
D/MyView ( 3506): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
D/MyView ( 3506): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
D/MyView ( 3506): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1059)
D/MyView ( 3506): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
D/MyView ( 3506): at android.view.View.measure(View.java:15775)
D/MyView ( 3506): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
D/MyView ( 3506): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
D/MyView ( 3506): at android.view.View.measure(View.java:15775)
D/MyView ( 3506): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
D/MyView ( 3506): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
D/MyView ( 3506): at android.view.View.measure(View.java:15775)
D/MyView ( 3506): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
D/MyView ( 3506): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
D/MyView ( 3506): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2193)
D/MyView ( 3506): at android.view.View.measure(View.java:15775)
D/MyView ( 3506): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2212)
D/MyView ( 3506): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1291)
D/MyView ( 3506): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1486)
D/MyView ( 3506): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
D/MyView ( 3506): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
D/MyView ( 3506): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
D/MyView ( 3506): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
D/MyView ( 3506): at android.view.Choreographer.doFrame(Choreographer.java:548)
D/MyView ( 3506): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
D/MyView ( 3506): at android.os.Handler.handleCallback(Handler.java:800)
D/MyView ( 3506): at android.os.Handler.dispatchMessage(Handler.java:100)
D/MyView ( 3506): at android.os.Looper.loop(Looper.java:194)
D/MyView ( 3506): at android.app.ActivityThread.main(ActivityThread.java:5391)
D/MyView ( 3506): at java.lang.reflect.Method.invokeNative(Native Method)
D/MyView ( 3506): at java.lang.reflect.Method.invoke(Method.java:525)
D/MyView ( 3506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
D/MyView ( 3506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
D/MyView ( 3506): at dalvik.system.NativeStart.main(Native Method)
D/MyView ( 3506): ---speSize = 940
D/MyView ( 3506): ---speMode = -2147483648
D/MyView ( 3506): ---AT_MOST---
查看onMeasure()的调用堆栈,然后查看源码,可以知道heightMeasureSpec和widthMeasureSpec的值在ViewRootImpl.java中初始化,而初始化又参考了view和LinearLayout中的属性参数的设置最终得到了heightMeasureSpec和widthMeasureSpec的值。
而specMode怎么对应呢?我先给出设置对应值的地方,以下代码皆出自ViewRootImpl.java
childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);//这里是赋值的代码
private static int getRootMeasureSpec(int windowSize, int rootDimension) {
int measureSpec;
switch (rootDimension) {
case ViewGroup.LayoutParams.MATCH_PARENT:
// Window can't resize. Force root view to be windowSize.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
break;
case ViewGroup.LayoutParams.WRAP_CONTENT:
// Window can resize. Set max size for root view.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
break;
default:
// Window wants to be an exact size. Force root view to be that size.
measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
break;
}
return measureSpec;
}
从这里我们基本上可以看出了MATCH_PARENT对应于EXACTLY,WRAP_CONTENT对应于AT_MOST,其他情况也对应于EXACTLY,它和MATCH_PARENT的区别在于size值不一样。现在我们需要知道这个rootDimension即lp.height对应于什么。
private void performTraversals() {
// cache mView since it is used so much below...
...
WindowManager.LayoutParams lp = mWindowAttributes;
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
synchronized (this) {
if (mView == null) {
mView = view;
mViewLayoutDirectionInitial = mView.getRawLayoutDirection();
mFallbackEventHandler.setView(view);
mWindowAttributes.copyFrom(attrs);
D/MyView ( 4249): ---speSize = 940
D/MyView ( 4249): ---speMode = 1073741824
D/MyView ( 4249): ---EXACTLY---
而更改LinearLayout中的这个值对这里的specMode是没有影响的。
下面我们再来通过实验来验证specSize的值由那些属性决定:
我们可以通过控制变量法,逐一改变代码中的LinearLayout和MyView中的相关属性值,看看是哪些影响了specSize,这里我直接给出答案:
530 = 300 * 2.0 - 20 * 2.0 - 15 * 2.0
影响specSize height的因素为:父视图的layout_height和paddingTop以及自身的layout_marginTop。但是我们不要忘记有weight时的影响。