1. 概念
Android中的View与我们以前理解的“视图”不同。在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window。ViewGroup是View的子类,所以它也具有View的特性,但它主要用来充当View的容器,将其中的View视作自己的孩子,对它的子View进行管理,当然它的孩子也可以是ViewGroup类型。ViewGroup(树根)和它的孩子们(View和ViewGroup)以树形结构形成了一个层次结构,View类有接受和处理消息的功能,android系统所产生的消息会在这些ViewGroup和 View之间传递。2. Android的窗口系统Android的窗口系统是Client/Server模式的,我在这里只讲窗口系统的客户端(图1)。 我们所提到的概念:View,ViewGroup,DecorView,ViewRoot都是存在于窗口系统的Client端。Android中的Window是表示Top Level等顶级窗口的概念。DecorView是Window的Top-Level View,这个View可以称之为主View,DecorView会缺省的attach到Activity的主窗口中。ViewRoot建立了主View(DecorView)与窗口系统Server端的通讯桥梁, ViewRoot是 Handler的子类,即它其实是个Handler,它接受窗口系统服务器端的消息并将消息投递到窗口系统的客户端(图1),然后消息就从客户端的主View往其下面的子View传递,直到消息被完全处理掉为止。DecorView实际上是一个ViewGroup。在依存关系上来讲,对单个主窗口来讲,DecorView是Top-Level View。View并不是关注的重点,重要的是我们需要知道消息分发路径是建立在什么关系上的。View的成员变量mParent用来管理View上级关系的。而ViewGroup顾名思义就是一组View的管理,于是在ViewGroup构建了焦点管理和子View节点数组。这样通过View的mParent和ViewGroup的mChildren构建了Android中View直接的关系网。3. View的介绍(1) 事件和绘制绘制流程:绘制按照视图树的顺序执行。视图绘制时会先绘制子控件。如果视图的背景可见,视图会在调用onDraw函数之前绘制背景。强制重绘,可以使用invalidate()。事件的基本流程如下:1、事件分配给相应视图,视图处理它,并通知相关监听器。2、操作过程中如果发生视图的尺寸变化,则该视图用调用requestLayout()方法,向父控件请求再次布局。3、操作过程中如果发生视图的外观变化,则该视图用调用invalidate()方法,请求重绘。4、如果requestLayout()或invalidate()有一个被调用,框架会对视图树进行相关的测量、布局和绘制。注意,视图树是单线程操作,直接调用其它视图的方法必须要在UI线程里。跨焦点处理:框架处理焦点的转移,来响应用户输入。isFocusable()函数表示视图是否能接受焦点。setFocusable(boolean)函数可以改变视图能否接受焦点。焦点转移按照就近算法。按哪个方向就近可以在XML布局文件中配置。nextFocusDownnextFocusLeftnextFocusRightnextFocusUp视图请求焦点可以使用requestFocus()。(2) 成员介绍protected ViewParent mParent;protected OnClickListener mOnClickListener;mOnClickListener是click事件的回调接口.大家经常使用的setOnClickListener(OnClickListener listener):- public void setOnClickListener(OnClickListener I) {
- if (!isClickable()) {
- setClickable(true);
- }
- mOnClickListener =I;
- }
可以看出,mOnClickListener其实就是保存我们在应用程序中定义的OnClickListener接口的。
public void draw(Canvas canvas)这个函数用于渲染View和它的孩子,我们不应该在子类对它进行override。protected void onDraw(Canvas canvas)我们一般override此函数来实现自己的绘制操作。IWindowSession getWindowSession() { return mAttachInfo != null ? mAttachInfo.mSession : null;}函数getWindowSession()用户得到窗口系统Client端和服务器端通讯的接口IWindowSession。这是一个AIDL接口,android系统中的跨进程通讯就是用AIDL接口实现的。public final void layout(int l, int t, int r, int b)此函数用于确定View和其子View的尺寸和位置,它的调用发生在onMeasure之后。protected void onLayout(boolean changed, int left, int top, int right, int bottom)此函数在layout调用完成后执行,View的子类一般override此函数,并在函数中对其每个孩子调用layout方法。public View getRootView()此函数用于得到View层次结构的top-level View,即上文中提到的DecorView。public final void measure(int widthMeasureSpec, int heightMeasureSpec)?凡饬俊T趏nMeasure中我们必须调用View.setMeasuredDimension(int, int)来保存测量得到的大小,高和宽分别被保存在View.mMeasuredHeight和View.mMeasureWidth中。public boolean onKeyUp(int keyCode, KeyEvent event)此函数会在键盘按键释放后被调用,但前提是View必须获得焦点。public boolean onTouchEvent(MotionEvent event)此函数用于响应触摸屏事件。public void invalidate()此函数将调用onDraw,强制重绘。public void requestLayout()r]4. ViewGroup介绍ViewGroup继承于View,它可以包含其他的View,就像一个View的容器,我们可以调用其成员函数addView()将View当作孩子放到ViewGroup中。我们经常使用的LinearLayout、relativeLayout等都是ViewGroup的子类,ViewGroup类中有一个内部类ViewGroup.LayoutParams,我们经常使用LayoutParams的子类来构造布局参数。我们也可以自定义自己的布局,以方便日后使用和维护,这时我们就需要继承ViewGroup类并在派生类中重写ViewGroup的一些方法,下面是一个简单的例子:- public class MyViewGroup extends ViewGroup {
- public MyViewGroup(Context context) {
- super(context);
- initChilren(context); //向容器中添加孩子
- }
- private void initChilren (Context context) {
- Button aBtn = new Button(context);
- this.addView(aBtn);
- Button bBtn = new Button(context);
- this.addView(bBtn);
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b)
- {
- //对容器的孩子进行布局。
- ………………
- ………………
- child.measure(r - l, b - t);
- child.layout(0, 50, child.getMeasuredWidth(), child .getMeasuredHeight() + 50);
- ………………
- ………………
- }
- }
经典示例代码,关键是onMeasure和onLayout方法
public class FitCenterFrameLayout extends ViewGroup {
public FitCenterFrameLayout(Context context) { super(context); } public FitCenterFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //修改尺寸 final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec); final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec); setMeasuredDimension(width, height); int childWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED); int childHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { getChildAt(i).measure(childWidthSpec, childHeightSpec); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int childCount = getChildCount(); final int parentLeft = getPaddingLeft(); final int parentTop = getPaddingTop(); final int parentRight = r - l - getPaddingRight(); final int parentBottom = b - t - getPaddingBottom(); final int parentWidth = parentRight - parentLeft; final int parentHeight = parentBottom - parentTop; int unpaddedWidth, unpaddedHeight, parentUnpaddedWidth, parentUnpaddedHeight; int childPaddingLeft, childPaddingTop, childPaddingRight, childPaddingBottom; for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } childPaddingLeft = child.getPaddingLeft(); childPaddingTop = child.getPaddingTop(); childPaddingRight = child.getPaddingRight(); childPaddingBottom = child.getPaddingBottom(); unpaddedWidth = child.getMeasuredWidth() - childPaddingLeft - childPaddingRight; unpaddedHeight = child.getMeasuredHeight() - childPaddingTop - childPaddingBottom; parentUnpaddedWidth = parentWidth - childPaddingLeft - childPaddingRight; parentUnpaddedHeight = parentHeight - childPaddingTop - childPaddingBottom; if (parentUnpaddedWidth * unpaddedHeight > parentUnpaddedHeight * unpaddedWidth) { final int scaledChildWidth = unpaddedWidth * parentUnpaddedHeight / unpaddedHeight + childPaddingLeft + childPaddingRight; child.layout( parentLeft + (parentWidth - scaledChildWidth) / 2, parentTop, parentRight - (parentWidth - scaledChildWidth) / 2, parentBottom); } else { final int scaledChildHeight = unpaddedHeight * parentUnpaddedWidth / unpaddedWidth + childPaddingTop + childPaddingBottom; child.layout( parentLeft, parentTop + (parentHeight - scaledChildHeight) / 2, parentRight, parentTop + (parentHeight + scaledChildHeight) / 2); } } }}
Android ViewGroup提高绘制性能
如果下面有很多子View,绘制的时候,需要开启其子View的绘制缓存功能,从而提高绘制效率。具体的代码如下:
1 2 3 4 5 6 7 8 9 10 | public void setChildrenDrawingCacheEnabled( boolean enabled) { final int count = getChildCount(); for ( int i = 0 ; i < count; i++) { final View view = getChildAt(i); view.setDrawingCacheEnabled( true ); // Update the drawing caches view.buildDrawingCache( true ); } } |
另一方面也可以通过setDrawingCacheQuality(low)将缓存质量降低,减少内存。
最后结束的时候,需要通过以下代码来清空绘制缓存。
1 2 3 4 5 6 7 | void clearChildrenCache() { final int count = getChildCount(); for ( int i = 0 ; i < count; i++) { final CellLayout layout = (CellLayout) getChildAt(i); layout.setChildrenDrawnWithCacheEnabled( false ); } } |