Android控件架构与自定义控件(一)
(本文并非原创文章,整理摘抄方便自己查看,原文地址为Android控件架构与自定义控件详解 和讲讲Android事件拦截机制 )
1、Andorid控件架构
在Android中,控件大致被分为两类,即ViewGroup控件与View控件。ViewGroup控件作为父控件可以包含多个View控件,并管理其包含的View控件。通过ViewGroup,整个界面上的控件形成了一个树形结构,即控件树,上层控件负责下层子控件的测量与绘制,并传递交互事件。在每棵控件树的顶部,都拥有一个ViewParent对象,这就是整棵树的控制核心,所有的交互管理事件都由它来统一调度和分配,从而可以对整个视图进行整体控制。如下图展示了一个View视图树:
PhoneWindow 将一个 DecorView 设置为整个应用窗口的根 View,DecorView 分为 TitleView 和 ContentView。
1、其中DecorView作为窗口界面的顶层视图,封装了一些窗口操作的通用方法。 可以说,DecorView将要显示的具体内容呈现在了PhoneWindow上,这里面的所有的View的监听事件,都通过WindowManagerService进行接收,并通过Activity对象来回调相应的onClickListener。
2、其中ContentView,是一个ID为content的FrameLayout,activity_main.xml 就是设置在这样一个FrameLayout里,最外层是一个FrameLayout,所以当activity_main.xml最外层是一个FrameLayout会造成层次层叠,用merge来代替FrameLayout进行布局的优化。
3、所以这也就说明了,用户通过设置 requestWindowFeature(Window.FEATURE_NO_TITLE); 来设置全屏显示的时候,它一定要放在 setContentView() 方法之前才能生效。
4、在代码中,当程序在 onCreat() 方法中调用 setContentView()方法后,ActivityManagerService会回调 onResume()方法,此时系统才会把整个DecorView添加到 PhoneWindow中,并让其显示出来,从而最终完成界面的绘制。
5、在源码中ViewGroup是继承自View的!!!!!
如果用户通过设置requestWindowFeature(Window.FEATURE_NO_TITLE)来设置全屏显示,那么DecorView中将只有ContentView了,这就解释了为什么调用requestWindowFeature()方法一定要在调用setContentView()方法之前才能生效的原因。在代码中,当程序在onCreate()方法中调用setContentView()方法后,ActivityManagerService会回调onResume()方法,此时系统才会把整个DecorView添加到PhoneWindow中,并让其显示出来,从而完成界面的绘制。
2、ViewRoot和DecorView介绍
2.1、ViewRoot简介
(1)ViewRoot对应于ViewRootImpl 类,它是连接 WindowManager 和 DecorView的纽带,View的三大流程(measure测量、layout布局、draw绘制)都是通过ViewRoot来完成的。
(2)在ActivityThread 中,当Activity对象被创建完毕后,会将 DecorView 添加到Window中,同时会创建 ViewRootImpl对象,并将 ViewRootImpl对象和DecorView建立关联
root = new ViewRootImpl(view.getContext(), display);
root.setView(view, wparams, panelParentView);
(3)View的绘制流程是从ViewRoot 的 performTraversals 方法(源码在sources\android\view\ViewRootImpl.java)开始的,它经过 measure、layout和draw三个过程才能最终将一个View绘制出来,其中measure用来测量View的宽高,layout用来确定View在父容器中的放置位置,draw负责将View绘制在屏幕上。
(4)下面是performTraversals 的大致流程: 源码位置:sources\android\view\ViewRootImpl.Java
在onMeasure方法中会对所有的子元素进行measure过程,这个时候measure流程就从父容器传递到子元素中了,这样就完成了一次measure过程。接着子元素又会重复父容器的measure过程,如此反复就完成了整个View树的遍历。performDraw的传递过程是在draw方法中通过dispatchDraw来实现的。
(5)Measure完成后,可以通过 getMeasuredWidth 和getMeasuredHeight 方法来获取到 View 测量后的宽高。Layout完成后,可以通过 getTop、getBottom、getLeft和getRight 来拿到View的四个顶点的位置,并可以通过 getWidth 和getHeight方法来拿到View的最终宽高。
Draw完成后,View显示在屏幕上。
2.2、DecorView简介:
(1)DecorView作为顶级View,它内部是一个竖直的LinearLayout,其中包含TitleBar和Content。
(2)其中Activity中设置 setContentView 就是将布局文件加载到内容栏的。
(3)内容栏是一个FrameLayout,可以布局优化。
(4)如何获得Content?
ViewGroup content = findViewById(R.android.id.content);
(5)如何获得View?
content.getChildAt(0);
3、view的测量
3.1、MeasureSpec简介
源码位置:sources\android\view\View.java
Android系统在绘制View前,必须对View进行测量,这个过程在onMeasure()方法中进行,借助的是 MeasureSpec 类。MeasureSpec类是一个32位的值,其中高2位为测量的模式SpecMode,低30位为测量的大小SpecSize。
public static class MeasureSpec {
// 移位用的,后面表示大小的30位
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
/**
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
/*
* dp/px
* 父容器对子元素没有任何约束,子元素可以是任意大小
* */
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
/**
* Measure specification mode: The parent has determined an exact size
* for the child. The child is going to be given those bounds regardless
* of how big it wants to be.
*/
/*
* match_parent
* 父容器决定了子元素的大小,子元素和父元素一样大
* */
public static final int EXACTLY = 1 << MODE_SHIFT;
/**
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
/*
* wrap_content
* 子元素不可以超过父容器的大小。
* 通常的控件对这个值都会设定一个默认值来表示wrap_content。
* */
public static final int AT_MOST = 2 << MODE_SHIFT;
/**
* Creates a measure specification based on the supplied size and mode.
*
* The mode must always be one of the following:
* <ul>
* <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
* <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
* <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
* </ul>
*
* <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
* implementation was such that the order of arguments did not matter
* and overflow in either value could impact the resulting MeasureSpec.
* {@link android.widget.RelativeLayout} was affected by this bug.
* Apps targeting API levels greater than 17 will get the fixed, more strict
* behavior.</p>
*
* @param size the size of the measure specification
* @param mode the mode of the measure specification
* @return the measure specification based on size and mode
*/
/*
* 将size和mode打包成一个32位的int值返回:
* */
public static int makeMeasureSpec(int size, int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
/**
* Extracts the mode from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the mode from
* @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
* {@link android.view.View.MeasureSpec#AT_MOST} or
* {@link android.view.View.MeasureSpec#EXACTLY}
*/
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
/**
* Extracts the size from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the size from
* @return the size in pixels defined in the supplied measure specification
*/
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
static int adjust(int measureSpec, int delta) {
return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
}
/**
* Returns a String representation of the specified measure
* specification.
*
* @param measureSpec the measure specification to convert to a String
* @return a String with the following format: "MeasureSpec: MODE SIZE"
*/
public static String toString(int measureSpec) {
int mode = getMode(measureSpec);
int size = getSize(measureSpec);
StringBuilder sb = new StringBuilder("MeasureSpec: ");
if (mode == UNSPECIFIED)
sb.append("UNSPECIFIED ");
else if (mode == EXACTLY)
sb.append("EXACTLY ");
else if (mode == AT_MOST)
sb.append("AT_MOST ");
else
sb.append(mode).append(" ");
sb.append(size);
return sb.toString();
}
}
MeasureSpec的测量模式有三种:
(1)EXACTLY:具体值或者 match_parent。onMeasure()方法默认情况下只支持这种模式。
(2)AT_MOST:wrap_content。不可以比父容器大就可以了,不过通常控件都会有一个默认值。
(3)UNSPECIFIED:View想多大就多大,通常自定义View时使用。
注意点:要让自定义View支持 wrap_content 属性,就必须重写onMeasure()方法来指定wrap_content时的大小。
3.2、MeasureSpec和LayoutParams的对应关系:
(1)在View测量的时候,系统会将LayoutParams在父容器的约束下转换成对应的MeasureSpec,然后再根据这个MeasureSpec来确定View测量后的宽高。
(2)MeasureSpec不仅有LayoutParams决定,还由父容器的大小影响。
(3)DecorView比较特别,由窗口的尺寸和LayoutParams来决定,它没有父容器。
(4)MeasureSpec一旦确定后,onMeasure中就可以确定View的测量宽高。
DecorView在ViewRootImpl中的源码:
(1)DecorView的MeasureSpec创建过程。在measureHierarchy函数中有如下的语句:
if (baseSize != 0 && desiredWindowWidth > baseSize) {
childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);......
对于desiredWindowHeight指的是屏幕的高度,那个desiredWindowWidth不能超过baseSize。if下面的两句的作用是获得宽高,第三句就是通过performMeasure来设置宽高了。
(2)getRootMeasureSpec 方法:
/**
* Figures out the measure spec for the root view in a window based on it's
* layout params.
*
* @param windowSize
* The available width or height of the window
*
* @param rootDimension
* The layout params for one dimension (width or height) of the
* window.
*
* @return The measure spec to use to measure the root view.
*/
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;
}
这个方法很明显了,进来以后通过第二个参数来判断是用窗口大小还是用LinearLayout的值。其中的makeMeasureSpec是SpecMode和SpecSize的打包组合。
我们布局中的View:
(1)View的measure过程由ViewGroup传递而来,先看一下ViewGroup的measureChildWithMargins 方法:
/**
* Ask one of the children of this view to measure itself, taking into
* account both the MeasureSpec requirements for this view and its padding
* and margins. The child must have MarginLayoutParams The heavy lifting is
* done in getChildMeasureSpec.
*
* @param child The child to measure
* @param parentWidthMeasureSpec The width requirements for this view
* @param widthUsed Extra space that has been used up by the parent
* horizontally (possibly by other children of the parent)
* @param parentHeightMeasureSpec The height requirements for this view
* @param heightUsed Extra space that has been used up by the parent
* vertically (possibly by other children of the parent)
*/
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
/* 也是先获取子元素的MeasureSpec,
* getChildMeasureSpec这里的参数,第一个就变成了父类的大小,
* 第二个参数是上下左右的边距
* 第三个参数是LinearLayout的宽高
*/
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
/*
* 得到子元素的MeasureSpec后,调用子元素的measure来设置宽高。
* */
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
(2)我们也来看看普通View的getChildMeasureSpec方法:其中的padding指的是父容器中已占用的空间大小。
/**
* Does the hard part of measureChildren: figuring out the MeasureSpec to
* pass to a particular child. This method figures out the right MeasureSpec
* for one dimension (height or width) of one child view.
*
* The goal is to combine information from our MeasureSpec with the
* LayoutParams of the child to get the best possible results. For example,
* if the this view knows its size (because its MeasureSpec has a mode of
* EXACTLY), and the child has indicated in its LayoutParams that it wants
* to be the same size as the parent, the parent should ask the child to
* layout given an exact size.
*
* @param spec The requirements for this view
* @param padding The padding of this view for the current dimension and
* margins, if applicable
* @param childDimension How big the child wants to be in the current
* dimension
* @return a MeasureSpec integer for the child
*/
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
/*
* 第一个参数是父类的MeasureSpec,所以获取的模式也就是父容器的:
* */
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
//子元素可用大小为父容器尺寸减去padding:
int size = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
/*
* 这里的这个specMode是父类容器的:
* */
switch (specMode) {
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
/*
* 这里的LayoutParams.MATCH_PARENT就是子元素它的LinearLayout
* */
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent has imposed a maximum size on us
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size, but our size is not fixed.
// Constrain child to not be bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent asked to see how big we want to be
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let him have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should
// be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
代码的逻辑图:
就是说只要子元素的LinearLayout是精确值,那子元素就是精确值。
子元素如果是match_parent,那子元素就和父容器一样大小。
子元素如果是wrap_content,那子元素就不能超过父容器的剩余空间大小。
3.3、看看具体的onMeasure实现和如何重写这个方法:
(1)原始的onMeasure在源码中是这样的,也就是重写时它自动构成这样:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
(2)然后我们去查看 super.onMeasure()方法:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
可以发现超类中调用了setMeasuredDimension()方法,它的两个参数是 MeasureSpec 类型变量,这个方法将测量后的宽高值设置进去,从而完成测量工作。
(3)所以当我们想要重写onMeasure()方法时,可以直接重写超类中的setMeasuredDimension()方法,同时自定义两个测量宽高的方法 measureWidth() 和 measureHeight() 来处理 MeasureSpec 类型变量,返回宽高值Size。在超类中是以getDefaultSize()来处理 MeasureSpec 类型变量的,这里我们换成自己写的 measureWidth() 和 measureHeight() 方法:
(注意,这里getDefaultSize返回的是size大小,也就是说将MeasureSpec中的size部分返回。)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
(4)下面就看看需要编写的measureWidth()方法如何实现的:
private int measureWidth(int measureSpec) {
int result = 0;
// 首先从MeasureSpec对象中提取出具体的测量模式和大小:
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// 直接返回精确值
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
// 另外两种模式,200是默认大小
result = 200;
// 但如果是AT_MOST即wrap_content时,还需要取两者的最小值。
// 所以通常情况下,如果我们不重写onMeasure()方法时,都会给这个控件一个默认的比如说200的大小
// 但如果重写了,这里就可以为wrap_content设置一个其他的默认大小。
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
4、View的measure过程
4.1、View的measure过程
源码位置:sources\android\view\View.java
(1)View的measure过程由measure方法来完成,measure方法是一个final类型的方法,意味着子类不能重写此方法,在View的measure方法中会调用VIew的onMeasure 方法,所以只需要看onMeasure方法就可以了:
/**
* <p>
* Measure the view and its content to determine the measured width and the
* measured height. This method is invoked by {@link #measure(int, int)} and
* should be overriden by subclasses to provide accurate and efficient
* measurement of their contents.
* </p>
*
* <p>
* <strong>CONTRACT:</strong> When overriding this method, you
* <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
* measured width and height of this view. Failure to do so will trigger an
* <code>IllegalStateException</code>, thrown by
* {@link #measure(int, int)}. Calling the superclass'
* {@link #onMeasure(int, int)} is a valid use.
* </p>
*
* <p>
* The base class implementation of measure defaults to the background size,
* unless a larger size is allowed by the MeasureSpec. Subclasses should
* override {@link #onMeasure(int, int)} to provide better measurements of
* their content.
* </p>
*
* <p>
* If this method is overridden, it is the subclass's responsibility to make
* sure the measured height and width are at least the view's minimum height
* and width ({@link #getSuggestedMinimumHeight()} and
* {@link #getSuggestedMinimumWidth()}).
* </p>
*
* @param widthMeasureSpec horizontal space requirements as imposed by the parent.
* The requirements are encoded with
* {@link android.view.View.MeasureSpec}.
* @param heightMeasureSpec vertical space requirements as imposed by the parent.
* The requirements are encoded with
* {@link android.view.View.MeasureSpec}.
*
* @see #getMeasuredWidth()
* @see #getMeasuredHeight()
* @see #setMeasuredDimension(int, int)
* @see #getSuggestedMinimumHeight()
* @see #getSuggestedMinimumWidth()
* @see android.view.View.MeasureSpec#getMode(int)
* @see android.view.View.MeasureSpec#getSize(int)
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
代码很简单,setMeasuredDimension 方法会设置View宽高的测量值,因此我们主要看看getDefaultSize 这个方法,第一个参数是getSuggestedMinimumWidth返回值,第二个参数是MeasureSpec的测量宽值。
(2)getDefaultSize方法的实现:返回的是size值。
/**
* Utility to return a default size. Uses the supplied size if the
* MeasureSpec imposed no constraints. Will get larger if allowed
* by the MeasureSpec.
*
* @param size Default size for this view
* @param measureSpec Constraints imposed by the parent
* @return The size this view should be.
*/
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;//下面(3)对这个size做了解释
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;//这两种情况下返回的是测量值大小
break;
}
return result;
}.
我们从这个方法可以得出,View的宽高是由specSize决定的,所以我们可以直接继承View的自定义控件需要重写onMeasure方法并设置wrap_content时的自身大小,否则在布局中使用wrap_content就相当于使用match_parent。什么意思呢?就是说默认情况下,上面代码中写的AT_MOST和EXACTLY这两种case的返回值都是specSize,但是我们可以在自定义的View中设置wrap_content的大小,使得它有一个自己默认的大小。所以在大多数的控件中,比如说TextView、ImageView等的源码就可以知道,针对wrap_content情形,它们的 onMeasure 方法均作了特殊的处理。
我们这里给一个自己可以重写onMeasure的代码):
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(mWidth, mHight);
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(mWidth, heightSpaceSize);
} else if (heightSpecMode == MeasureSpec.AT_MOST){
setMeasuredDimension(widthSpaceSize, mHight);
}
}
(3)setMeasuredDimension 方法中的第一个参数是getSuggestedMinimumWidth方法返回的,它返回的是View在UNSPECIFIED情况下的测量宽高。
/**
* Returns the suggested minimum width that the view should use. This
* returns the maximum of the view's minimum width)
* and the background's minimum width
* ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
* <p>
* When being used in {@link #onMeasure(int, int)}, the caller should still
* ensure the returned width is within the requirements of the parent.
*
* @return The suggested minimum width of the view.
*/
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
如果View没有设置背景,那么View的宽度就是mMinWidh,这个值对应于android:minWidth,这个值默认是为0的。
如果View设置了背景,那么View的宽度就是max(mMinWidth, mBackground.getMinimumWidth())。
(4)mBackground.getMinimumWidth():
/**
* Returns the minimum width suggested by this Drawable. If a View uses this
* Drawable as a background, it is suggested that the View use at least this
* value for its width. (There will be some scenarios where this will not be
* possible.) This value should INCLUDE any padding.
*
* @return The minimum width suggested by this Drawable. If this Drawable
* doesn't have a suggested minimum width, 0 is returned.
*/
public int getMinimumWidth() {
final int intrinsicWidth = getIntrinsicWidth();
return intrinsicWidth > 0 ? intrinsicWidth : 0;
}
可以看到他返回的就是Drawable的原始宽度,前提是这个Drawable有原始宽度,否则就返回0.
那么Drawable在什么情况下有原始宽度呢?ShapeDrawable无原始宽高,而BitmapDrawable有原始宽高(图片的尺寸)。
4.2、ViewGroup的measure过程:
源码位置:sources\android\view\ViewGroup.java
(1)对于ViewGroup而言,除了完成自己的measure过程以外,还会去遍历去调用所有子元素的measure方法,各个子元素再递归去执行这个过程。
(2)和View不同的是,ViewGroup是一个抽象类,因此它没有重写View的onMeasure方法,但它提供了一个叫 measureChildren的方法:
/**
* Ask all of the children of this view to measure themselves, taking into
* account both the MeasureSpec requirements for this view and its padding.
* We skip children that are in the GONE state The heavy lifting is done in
* getChildMeasureSpec.
*
* @param widthMeasureSpec The width requirements for this view
* @param heightMeasureSpec The height requirements for this view
*/
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
(3)在measureChildren中有一个measureChild:
/**
* Ask one of the children of this view to measure itself, taking into
* account both the MeasureSpec requirements for this view and its padding.
* The heavy lifting is done in getChildMeasureSpec.
*
* @param child The child to measure
* @param parentWidthMeasureSpec The width requirements for this view
* @param parentHeightMeasureSpec The height requirements for this view
*/
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
取出子元素的LayoutParams,然后再通过getChildMeasureSpec来创建子元素的MeasureSpec,再然后将它直接传递给View的measure方法来进行测量。
(4)在ViewGroup中并没有定义其测量的具体过程,因为ViewGroup是一个抽象类,它的测量过程的onMeasure方法需要各个子类去实现,比如LinearLayout、RelativeLayout等,因为不同的子类有不同的布局特性,这导致它们的测量细节各不相同。因此ViewGroup无法做统一实现。
(5)我们以LinearLayout为例来讲一下ViewGroup:
源码位置:sources\android\widget\LinearLayout.java
先看看它的onMeasure方法:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mOrientation == VERTICAL) {
measureVertical(widthMeasureSpec, heightMeasureSpec);
} else {
measureHorizontal(widthMeasureSpec, heightMeasureSpec);
}
}
再去看看measureVertical方法部分代码:
/**
* Measures the children when the orientation of this LinearLayout is set
* to {@link #VERTICAL}.
*
* @param widthMeasureSpec Horizontal space requirements as imposed by the parent.
* @param heightMeasureSpec Vertical space requirements as imposed by the parent.
*
* @see #getOrientation()
* @see #setOrientation(int)
* @see #onMeasure(int, int)
*/
void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
...
// See how tall everyone is. Also remember max width.
for (int i = 0; i < count; ++i) {
final View child = getVirtualChildAt(i);
...
// Determine how big this child would like to be. If this or
// previous children have given a weight, then we allow it to
// use all available space (and we will shrink things later
// if needed).
/*
* 遍历子元素并对子元素执行这个方法,
* 这个方法内部会调用子元素的measure方法,
* 这样各个子元素就开始一次进入measure过程,
* 并且系统会通过mTotalLength这个变量来存储LinearLayout在竖直方向的初步高度。
* 没测量一个子元素,mTotalLength都会增加。
* */
measureChildBeforeLayout(
child, i, widthMeasureSpec, 0, heightMeasureSpec,
totalWeight == 0 ? mTotalLength : 0);
if (oldHeight != Integer.MIN_VALUE) {
lp.height = oldHeight;
}
final int childHeight = child.getMeasuredHeight();
final int totalLength = mTotalLength;
mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin +
lp.bottomMargin + getNextLocationOffset(child));
...
}
...
// Add in our padding
mTotalLength += mPaddingTop + mPaddingBottom;
int heightSize = mTotalLength;
// Check against our minimum height
heightSize = Math.max(heightSize, getSuggestedMinimumHeight());
// Reconcile our calculated size with the heightMeasureSpec
int heightSizeAndState = resolveSizeAndState(heightSize, heightMeasureSpec, 0);
heightSize = heightSizeAndState & MEASURED_SIZE_MASK;
...
// 等子元素都测量完毕后,LinearLayout测量自己的大小:
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),//这个方法在下面有介绍
heightSizeAndState);
if (matchWidth) {
forceUniformWidth(count, heightMeasureSpec);
}
}
针对竖直的LinearLayout而言,它的水平方向的测量遵循View的测量过程,在竖直方向的测量过程则和View有所不同。
具体来说,如果它的布局中高度采用的是match_parent或者具体数值,那么它的测量过程和View一致,即高度为specSize;如果它的布局中高度采用的是wrap_content,那么它的高度是所有子元素所占用的高度总和,但是仍然不能超过它的父容器的剩余空间,当然它的最终高度还需要考虑其在竖直方向的padding,这个过程可以参考如下的源码:这个方法是在View.java中实现的:
/**
* Utility to reconcile a desired size and state, with constraints imposed
* by a MeasureSpec. Will take the desired size, unless a different size
* is imposed by the constraints. The returned value is a compound integer,
* with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
* optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
* size is smaller than the size the view wants to be.
*
* @param size How big the view wants to be
* @param measureSpec Constraints imposed by the parent
* @return Size information bit mask as defined by
* {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
*/
public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
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:
if (specSize < size) {
result = specSize | MEASURED_STATE_TOO_SMALL;
} else {
result = size;
}
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result | (childMeasuredState&MEASURED_STATE_MASK);
}
到此为止,measure完成以后,通过 个图MeasuredWidth、MeasuredHeight方法就可以正确地获取到View的测量宽高。注意点:在某些极端情况下,系统需要多次测量measure才能确定最终的测量宽高,这种情况下,在onMeasure方法中拿到的测量宽高很可能是不准确的。一个比较好的习惯是在onLayout方法中去获取View的测量宽高或者说是最终宽高。
4.3、解决View的measure过程和Activity生命周期不同步的问题
(1)当我们想要在Activity已启动的时候就做一个任务,但是这个任务需要获取某个View 的宽高。但是由于View的measure和Activity的生命周期不同步执行,因此无法保证Activity执行了onCreate、onStart、onResume时某个View已经测量完毕了,如果View 还没有测量完毕,那么获得的宽高就是0。
(2)具体的解决方法有四种。
(3)解决方法一:onWindowFocusChanged (源码位置:sources\android\view\View.java)。onWindowFocusChanged 这个方法的含义是:View已经初始化完毕了,宽高已经准备好了,这个时候获取宽高是没有问题的。onWindowFocusChanged会被调用多次,当Activity的窗口得到焦点或失去焦点的时候都会被调用一次。也就是说,当Activity继续执行或暂停执行的时候,onWindowFocusChanged就会被调用。那如果频繁的onResume或onPause时,onWindowFocusChanged 也会被频繁的调用。
源码如下:
/**
* Called when the window containing this view gains or loses focus. Note
* that this is separate from view focus: to receive key events, both
* your view and its window must have focus. If a window is displayed
* on top of yours that takes input focus, then your own window will lose
* focus but the view focus will remain unchanged.
*
* @param hasWindowFocus True if the window containing this view now has
* focus, false otherwise.
*/
public void onWindowFocusChanged(boolean hasWindowFocus) {
InputMethodManager imm = InputMethodManager.peekInstance();
if (!hasWindowFocus) {
if (isPressed()) {
setPressed(false);
}
if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
imm.focusOut(this);
}
removeLongPressCallback();
removeTapCallback();
onFocusLost();
} else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
imm.focusIn(this);
}
refreshDrawableState();
}
重写onWindowFocusChanged 代码如下:
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// 如果重新获得焦点,那就获取宽高值:
if(hasFocus){
int width = view.getMeasuredWidth();
int height = view.getMeasureHeight();
}
}
(4)解决方法二:view.post(runnable) (源码位置:sources\android\app\Activity.java)。通过post可以将一个runnable投递到消息队列的尾部,然后等待Looper调用此runnable的时候,View 也已经初始化好了。
源码如下:
/**
* Called after {@link #onCreate} — or after {@link #onRestart} when
* the activity had been stopped, but is now again being displayed to the
* user. It will be followed by {@link #onResume}.
*
* <p><em>Derived classes must call through to the super class's
* implementation of this method. If they do not, an exception will be
* thrown.</em></p>
*
* @see #onCreate
* @see #onStop
* @see #onResume
*/
protected void onStart() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
mCalled = true;
if (!mLoadersStarted) {
mLoadersStarted = true;
if (mLoaderManager != null) {
mLoaderManager.doStart();
} else if (!mCheckedForLoaderManager) {
mLoaderManager = getLoaderManager("(root)", mLoadersStarted, false);
}
mCheckedForLoaderManager = true;
}
getApplication().dispatchActivityStarted(this);
}
重写代码如下:
protected void onStart() {
super.onStart();
view.post(new Runnable){
@Override
public void run(){
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
}
};
}
(5)解决方法三:ViewTreeObserver
(6)解决方法四:view.measure(int widthMeasureSpec, int heightMeasureSpec)。通过手动对View进行measure来得到View的宽高。这种方法比较复杂,这里要分情况处理,根据View的 LayoutParames 来分:
match_parent:
直接放弃,无法measure出具体的宽高。因为我们此时还没有办法知道父容器的剩余空间。
具体的数值(dp/ps):
// 比如宽高都是100px
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);
wrap_content:
int widthMeasureSpec = MeasureSpec.makeMeasureSpec( (1 << 30) - 1, MeasureSpec.AT_MOST);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec( (1 << 30) - 1,MeasureSpec.AT_MOST);
view.measure(widthMeasureSpec, heightMeasureSpec);
要知道的是(1 << 30) - 1,VIew 的尺寸使用30位二进制表示,也就是说最大是30个1,也就是(1 << 30) - 1。
在最大化模式下,我们使用View理论上能支持的最大值去构造MeasureSpec是合理的。
4.4、常见Measure错误使用方法:
无法通过错误的MeasureSpec去得出合法的SpecMode,从而导致measure过程出错,违背了系统内部的实现规范。
其次不能保证一定能measure出正确结果。
(1)
int widthMeasureSpec = MeasureSpec.makeMeasureSpec( -1, MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec( -1, MeasureSpec.UNSPECIFIED);
view.measure(widthMeasureSpec, heightMeasureSpec);
(2)
view.measure(LayoutParames.WRAP_CONTENT, LayoutParames.WRAP_CONTENT); TENT);
5、View的layout过程
(1)Layout的作用是ViewGroup用来确定子元素的位置,当ViewGroup的位置被确定后,它在 onLayout 中会遍历所有的子元素并调用其layout方法。在layout方法中onLayout方法又会被调用。
(2)layout方法会确定View本身的位置!!!!而onLayout方法会确定所有子元素的位置!!!
(3)先看View的layout方法:源码位置:sources\android\view\View.java。
/**
* Assign a size and position to a view and all of its
* descendants
*
* <p>This is the second phase of the layout mechanism.
* (The first is measuring). In this phase, each parent calls
* layout on all of its children to position them.
* This is typically done using the child measurements
* that were stored in the measure pass().</p>
*
* <p>Derived classes should not override this method.
* Derived classes with children should override
* onLayout. In that method, they should
* call layout on each of their children.</p>
*
* @param l Left position, relative to parent
* @param t Top position, relative to parent
* @param r Right position, relative to parent
* @param b Bottom position, relative to parent
*/
@SuppressWarnings({"unchecked"})
public void layout(int l, int t, int r, int b) {
if ((mPrivateFlags3 & PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT) != 0) {
onMeasure(mOldWidthMeasureSpec, mOldHeightMeasureSpec);
mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
}
int oldL = mLeft;
int oldT = mTop;
int oldB = mBottom;
int oldR = mRight;
/*
* 首先通过setFrame方法来设定View的四个顶点的位置,
* 即初始化mLeft、mTop、mBottom、mRight这四个值,
* 这四个顶点一旦被确定,那么View在父容器中的位置也就确定了
* */
boolean changed = isLayoutModeOptical(mParent) ?
setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
/*
* 接着调用onLayout方法,这个方法的用途是父容器确定子元素的位置,和onMeasure方法类似。
* onLayout的具体实现同样和具体的布局有关,
* 所以View和ViewGroup都没有真正实现onLayout方法。
* */
onLayout(changed, l, t, r, b);
mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnLayoutChangeListeners != null) {
ArrayList<OnLayoutChangeListener> listenersCopy =
(ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
int numListeners = listenersCopy.size();
for (int i = 0; i < numListeners; ++i) {
listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
}
}
}
mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
mPrivateFlags3 |= PFLAG3_IS_LAID_OUT;
}
其中涉及到onLayout方法,我们看看View源码中是如何写的:
/**
* Called from layout when this view should
* assign a size and position to each of its children.
*
* Derived classes with children should override
* this method and call layout on each of
* their children.
* @param changed This is a new size or position for this view
* @param left Left position, relative to parent
* @param top Top position, relative to parent
* @param right Right position, relative to parent
* @param bottom Bottom position, relative to parent
*/
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}
(4)LinearLayout的onLayout方法:源码位置:sources\android\widget\LinearLayout.java。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (mOrientation == VERTICAL) {
layoutVertical(l, t, r, b);
} else {
layoutHorizontal(l, t, r, b);
}
}
同样的,分为垂直和水平两种情况。下面去看看layoutVertical好了。
(5)layoutVertical方法,只看有注释的地方就可以了
/**
* Position the children during a layout pass if the orientation of this
* LinearLayout is set to {@link #VERTICAL}.
*
* @see #getOrientation()
* @see #setOrientation(int)
* @see #onLayout(boolean, int, int, int, int)
* @param left
* @param top
* @param right
* @param bottom
*/
void layoutVertical(int left, int top, int right, int bottom) {
final int paddingLeft = mPaddingLeft;
int childTop;
int childLeft;
// Where right end of child should go
final int width = right - left;
int childRight = width - mPaddingRight;
// Space available for child
int childSpace = width - paddingLeft - mPaddingRight;
// 获取子元素个数:
final int count = getVirtualChildCount();
final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
final int minorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
switch (majorGravity) {
case Gravity.BOTTOM:
// mTotalLength contains the padding already
childTop = mPaddingTop + bottom - top - mTotalLength;
break;
// mTotalLength contains the padding already
case Gravity.CENTER_VERTICAL:
childTop = mPaddingTop + (bottom - top - mTotalLength) / 2;
break;
case Gravity.TOP:
default:
childTop = mPaddingTop;
break;
}
/*
* 遍历所有的子元素,并调用setChildFrame:
* */
for (int i = 0; i < count; i++) {
final View child = getVirtualChildAt(i);
if (child == null) {
childTop += measureNullChild(i);
} else if (child.getVisibility() != GONE) {
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
final LinearLayout.LayoutParams lp =
(LinearLayout.LayoutParams) child.getLayoutParams();
int gravity = lp.gravity;
if (gravity < 0) {
gravity = minorGravity;
}
final int layoutDirection = getLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft = paddingLeft + ((childSpace - childWidth) / 2)
+ lp.leftMargin - lp.rightMargin;
break;
case Gravity.RIGHT:
childLeft = childRight - childWidth - lp.rightMargin;
break;
case Gravity.LEFT:
default:
childLeft = paddingLeft + lp.leftMargin;
break;
}
if (hasDividerBeforeChildAt(i)) {
childTop += mDividerHeight;
}
childTop += lp.topMargin;
/*
* 在这里设置子元素的四个顶点值,
* 其中的childTop会不断增大,
* 这就意味着后面的子元素会被放置在靠下的位置,
* 这刚好符合竖直方向的LinearLayout的特性。
* 但在setChildFrame中,其实它仅仅是调用了子元素的layout方法而已,
* */
setChildFrame(child, childLeft, childTop + getLocationOffset(child),
childWidth, childHeight);
childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);
i += getChildrenSkipCount(child, i);
}
}
}
其中的setChildFrame方法是这样写的:
private void setChildFrame(View child, int left, int top, int width, int height) {
child.layout(left, top, left + width, top + height);
}
还可以发现在setChildFrame中的width和height实际上就是子元素的测量宽高,就是在setChildFrame中的后两个参数是这样获取的:
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
(6)这样父元素在layout方法中完成自己的定位以后,就通过onLayout方法去调用子元素的layout方法,子元素又会通过自己的layout方法来确定自己的位置,这样一层一层地传递下去就完成了整个View树的layout过程。
(7)测量宽高和最终宽高之间的联系:源码位置:sources\android\view\View.java。
getHeight获取的是最终高度:
/**
* Return the height of your view.
*
* @return The height of your view, in pixels.
*/
@ViewDebug.ExportedProperty(category = "layout")
public final int getHeight() {
return mBottom - mTop;
}
getMeasuredHeight获取的是测量高度:
/**
* Like {@link #getMeasuredHeightAndState()}, but only returns the
* raw width component (that is the result is masked by
* {@link #MEASURED_SIZE_MASK}).
*
* @return The raw measured height of this view.
*/
public final int getMeasuredHeight() {
return mMeasuredHeight & MEASURED_SIZE_MASK;
}
其实本质上来说他们两个是相同的,只是测量宽高形成于View的measure过程,而最终宽高形成于View的layout过程,即两者的赋值时机不同,测量宽高的赋值时机稍微早一些。
ViewGroup需要负责子View显示的大小。当ViewGroup的大小为wrap_content时,ViewGroup就需要遍历子View,以便获得所有子View的大小,从而决定自己的大小。而在其他模式下则会通过具体的指定值来设置自身的大小。ViewGroup在测量时通过遍历所有子View,从而调用子View的Measure方法来获得每一个子View的测量结果。当子View测量完毕后,就需要将子View放到合适的位置,这个过程就是View 的Layout过程。
ViewGroup在执行Layout过程时,同样是使用遍历来调用子View的Layout方法,并指定其具体显示的位置,从而来决定其布局位置。在自定义ViewGroup时,通常会去重写onLayout()方法来控制其子View显示位置的逻辑。同样,如果需要支持wrap_content属性,那么它还必须重写onMeasure()方法,这点与View是相同的。