继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android 中 View 绘制流程分析

牧羊人nacy
关注TA
已关注
手记 52
粉丝 5
获赞 25

原文链接

创建Window

在Activity的attach方法中通过调用PolicyManager.makeNewWindo创建Window,将一个View add到WindowManager时,WindowManagerImpl创建一个ViewRoot来管理该窗口的根View。并通过ViewRoot.setView方法把该View传给ViewRoot。

final void attach(Context context, ActivityThread aThread,  
        Instrumentation instr, IBinder token, int ident,  
        Application application, Intent intent, ActivityInfo info,  
        CharSequence title, Activity parent, String id,  
        NonConfigurationInstances lastNonConfigurationInstances,  
        Configuration config) {  
    attachBaseContext(context);  

    mFragments.attachActivity(this, mContainer, null);  

    mWindow = PolicyManager.makeNewWindow(this);  
    mWindow.setCallback(this);  
    mWindow.getLayoutInflater().setPrivateFactory(this);

创建DecorView

DecorView为整个Window界面的最顶层View。

Activity中的Window对象帮我们创建了一个PhoneWindow内部类DecorView(父类为FrameLayout)窗口顶层视图,然后通过LayoutInflater将xml内容布局解析成View树形结构添加到DecorView顶层视图中id为content的FrameLayout父容器上面。Activity的content内容布局最终会添加到DecorView窗口顶层视图上面。

protected boolean initializePanelDecor(PanelFeatureState st) {  
    st.decorView = new DecorView(getContext(), st.featureId);  
    st.gravity = Gravity.CENTER | Gravity.BOTTOM;  
    st.setStyle(getContext());  

    return true;  
}

创建ViewRoot并关联View

WindowManagerImpl保存DecorView到mViews,创建对应的ViewRoot;

ViewRoot用于管理窗口的根View,并和global window manger进行交互。ViewRoot中有一个nested class: W,W是一个Binder子类,用于接收global window manager的各种消息, 如按键消息, 触摸消息等。 ViewRoot有一个W类型的成员mWindow,ViewRoot在Constructor中创建一个W的instance并赋值给mWindow。 ViewRoot是Handler的子类, W会通过Looper把消息传递给ViewRoot。 ViewRoot在setView方法中把mWindow传给sWindowSession。

public void addView(View view, ViewGroup.LayoutParams params,  
        Display display, Window parentWindow) {  
    if (view == null) {  
        throw new IllegalArgumentException("view must not be null");  
    }  
    if (display == null) {  
        throw new IllegalArgumentException("display must not be null");  
    }  
    if (!(params instanceof WindowManager.LayoutParams)) {  
        throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");  
    }  

    final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;  
    if (parentWindow != null) {  
        parentWindow.adjustLayoutParamsForSubWindow(wparams);  
    }  

    ViewRootImpl root;  
    View panelParentView = null;  

    synchronized (mLock) {  
        // Start watching for system property changes.  
        if (mSystemPropertyUpdater == null) {  
            mSystemPropertyUpdater = new Runnable() {  
                @Override public void run() {  
                    synchronized (mLock) {  
                        for (ViewRootImpl viewRoot : mRoots) {  
                            viewRoot.loadSystemProperties();  
                        }  
                    }  
                }  
            };  
            SystemProperties.addChangeCallback(mSystemPropertyUpdater);  
        }  

        int index = findViewLocked(view, false);  
        if (index >= 0) {  
            throw new IllegalStateException("View " + view  
                    + " has already been added to the window manager.");  
        }  

        // If this is a panel window, then find the window it is being  
        // attached to for future reference.  
        if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&  
                wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {  
            final int count = mViews != null ? mViews.length : 0;  
            for (int i=0; i<count; i++) {  
                if (mRoots[i].mWindow.asBinder() == wparams.token) {  
                    panelParentView = mViews[i];  
                }  
            }  
        }  

        root = new ViewRootImpl(view.getContext(), display);  

        view.setLayoutParams(wparams);  

        if (mViews == null) {  
            index = 1;  
            mViews = new View[1];  
            mRoots = new ViewRootImpl[1];  
            mParams = new WindowManager.LayoutParams[1];  
        } else {  
            index = mViews.length + 1;  
            Object[] old = mViews;  
            mViews = new View[index];  
            System.arraycopy(old, 0, mViews, 0, index-1);  
            old = mRoots;  
            mRoots = new ViewRootImpl[index];  
            System.arraycopy(old, 0, mRoots, 0, index-1);  
            old = mParams;  
            mParams = new WindowManager.LayoutParams[index];  
            System.arraycopy(old, 0, mParams, 0, index-1);  
        }  
        index--;  

        mViews[index] = view;  
        mRoots[index] = root;  
        mParams[index] = wparams;  
    }  

    // do this last because it fires off messages to start doing things  
    try {  
        root.setView(view, wparams, panelParentView);  
    } catch (RuntimeException e) {  
        // BadTokenException or InvalidDisplayException, clean up.  
        synchronized (mLock) {  
            final int index = findViewLocked(view, false);  
            if (index >= 0) {  
                removeViewLocked(index, true);  
            }  
        }  
        throw e;  
    }  
}

ViewRoot是GUI管理系统与GUI呈现系统之间的桥梁,需要注意它并不是一个View类型。

它的主要作用如下:

1、向DecorView分发收到的用户发起的event事件,如按键,触屏,轨迹球等事件;
2、与WindowManagerService交互,完成整个Activity的GUI的绘制。

View绘制基本流程

这里先给出Android系统View的绘制流程:依次执行View类里面的如下三个方法:

measure(int ,int) :测量View的大小
layout(int ,int ,int ,int) :设置子View的位置
draw(Canvas) :绘制View内容到Canvas画布上

整个View树的绘图流程是在ViewRoot.Java类的performTraversals()函数展开的,该函数做的执行过程可简单概况为根据之前设置的状态,判断是否需要重新计算视图大小(measure)、是否重新需要安置视图的位置(layout)、以及是否需要重绘 (draw)

 mesarue()测量过程

主要作用:为整个View树计算实际的大小,即设置实际的高(mMeasuredHeight)和宽(mMeasureWidth),每个View的控件的实际宽高都是由父视图和本身视图决定的。

具体的调用如下:

ViewRootImpl 的performTraversals方法中,调用measureHierarchy,然后调用performMeasure

private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {  
       Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");  
       try {  
           mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);  
       } finally {  
           Trace.traceEnd(Trace.TRACE_TAG_VIEW);  
       }  
   }

ViewRoot根对象地属性mView(其类型一般为ViewGroup类型)调用measure()方法去计算View树的大小,回调View/ViewGroup对象的onMeasure()方法,该方法实现的功能如下:

1、设置本View视图的最终大小,该功能的实现通过调用setMeasuredDimension()方法去设置实际的高(mMeasuredHeight)和宽(mMeasureWidth)

2、如果该View对象是个ViewGroup类型,需要重写onMeasure()方法,对其子视图进行遍历的measure()过程。

对每个子视图的measure()过程,是通过调用父类ViewGroup.java类里的measureChildWithMargins()方法去实现,该方法内部只是简单地调用了View对象的measure()方法。

整个measure调用流程就是个树形的递归过程measure()方法两个参数都是父View传递过来的,也就是代表了父view的规格。他由两部分组成,高2位表示MODE,定义在MeasureSpec类(View的内部类)中,有三种类型,MeasureSpec.EXACTLY表示确定大小, MeasureSpec.AT_MOST表示最大大小, MeasureSpec.UNSPECIFIED不确定。低30位表示size,也就是父View的大小。对于系统Window类的DecorVIew对象Mode一般都为MeasureSpec.EXACTLY ,而size分别对应屏幕宽高。对于子View来说大小是由父View和子View共同决定的。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),  
            getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));  
}
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {  
     boolean optical = isLayoutModeOptical(this);  
     if (optical != isLayoutModeOptical(mParent)) {  
         Insets insets = getOpticalInsets();  
         int opticalWidth  = insets.left + insets.right;  
         int opticalHeight = insets.top  + insets.bottom;  

         measuredWidth  += optical ? opticalWidth  : -opticalWidth;  
         measuredHeight += optical ? opticalHeight : -opticalHeight;  
     }  
     mMeasuredWidth = measuredWidth;  
     mMeasuredHeight = measuredHeight;  

     mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;  
 }

layout布局过程

主要作用 :为将整个根据子视图的大小以及布局参数将View树放到合适的位置上。

具体的调用如下:

ViewRootImpl 的performTraversals方法中,调用performLayout

private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,  
        int desiredWindowHeight) {  
    mLayoutRequested = false;  
    mScrollMayChange = true;  
    mInLayout = true;  

    final View host = mView;  
    if (DEBUG_ORIENTATION || DEBUG_LAYOUT) {  
        Log.v(TAG, "Laying out " + host + " to (" +  
                host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");  
    }  

    Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout");  
    try {  
        host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());  

        mInLayout = false;  
        int numViewsRequestingLayout = mLayoutRequesters.size();  
        if (numViewsRequestingLayout > 0) {  
            // requestLayout() was called during layout.  
            // If no layout-request flags are set on the requesting views, there is no problem.  
            // If some requests are still pending, then we need to clear those flags and do  
            // a full request/measure/layout pass to handle this situation.  
            ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters,  
                    false);  
            if (validLayoutRequesters != null) {  
                // Set this flag to indicate that any further requests are happening during  
                // the second pass, which may result in posting those requests to the next  
                // frame instead  
                mHandlingLayoutInLayoutRequest = true;  

                // Process fresh layout requests, then measure and layout  
                int numValidRequests = validLayoutRequesters.size();  
                for (int i = 0; i < numValidRequests; ++i) {  
                    final View view = validLayoutRequesters.get(i);  
                    Log.w("View", "requestLayout() improperly called by " + view +  
                            " during layout: running second layout pass");  
                    view.requestLayout();  
                }  
                measureHierarchy(host, lp, mView.getContext().getResources(),  
                        desiredWindowWidth, desiredWindowHeight);  
                mInLayout = true;  
                host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());  

                mHandlingLayoutInLayoutRequest = false;  

                // Check the valid requests again, this time without checking/clearing the  
                // layout flags, since requests happening during the second pass get noop'd  
                validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true);  
                if (validLayoutRequesters != null) {  
                    final ArrayList<View> finalRequesters = validLayoutRequesters;  
                    // Post second-pass requests to the next frame  
                    getRunQueue().post(new Runnable() {  
                        @Override  
                        public void run() {  
                            int numValidRequests = finalRequesters.size();  
                            for (int i = 0; i < numValidRequests; ++i) {  
                                final View view = finalRequesters.get(i);  
                                Log.w("View", "requestLayout() improperly called by " + view +  
                                        " during second layout pass: posting in next frame");  
                                view.requestLayout();  
                            }  
                        }  
                    });  
                }  
            }  

        }  
    } finally {  
        Trace.traceEnd(Trace.TRACE_TAG_VIEW);  
    }  
    mInLayout = false;  
}

host.layout()开始View树的布局,继而回调给View/ViewGroup类中的layout()方法。具体流程如下

1 、layout方法会设置该View视图位于父视图的坐标轴,即mLeft,mTop,mLeft,mBottom(调用setFrame()函数去实现),接下来回调onLayout()方法(如果该View是ViewGroup对象,需要实现该方法,对每个子视图进行布局)。

2、如果该View是个ViewGroup类型,需要遍历每个子视图chiildView,调用该子视图的layout()方法去设置它的坐标值。

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {  
}
public void layout(int l, int t, int r, int b) {  
    int oldL = mLeft;  
    int oldT = mTop;  
    int oldB = mBottom;  
    int oldR = mRight;  
    boolean changed = isLayoutModeOptical(mParent) ?  
            setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);  
    if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {  
        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;  
}

draw()绘图过程

ViewRootImpl 的performTraversals方法中,调用了mView的draw方法

mView.draw()开始绘制,draw()方法实现的功能如下:

1 、绘制该View的背景
2 、为显示渐变框做一些准备操作
3、调用onDraw()方法绘制视图本身   (每个View都需要重载该方法,ViewGroup不需要实现该方法)
4、调用dispatchDraw ()方法绘制子视图(如果该View类型不为ViewGroup,即不包含子视图,不需要重载该方法)

值得说明的是,ViewGroup类已经为我们重写了dispatchDraw ()的功能实现,应用程序一般不需要重写该方法,但可以重载父类函数实现具体的功能。

dispatchDraw()方法内部会遍历每个子视图,调用drawChild()去重新回调每个子视图的draw()方法。

5、绘制滚动条

刷新视图

Android中实现view的更新有两个方法,一个是invalidate,另一个是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。

requestLayout()方法 :会导致调用measure()过程 和 layout()过程 。

说明:只是对View树重新布局layout过程包括measure()和layout()过程,不会调用draw()过程,但不会重新绘制
任何视图包括该调用者本身。
一般引起invalidate()操作的函数如下:

1、直接调用invalidate()方法,请求重新draw(),但只会绘制调用者本身。

2、setSelection()方法 :请求重新draw(),但只会绘制调用者本身。

3、setVisibility()方法 : 当View可视状态在INVISIBLE转换VISIBLE时,会间接调用invalidate()方法,继而绘制该View。

4 、setEnabled()方法 : 请求重新draw(),但不会重新绘制任何视图包括该调用者本身。

内容参考源码,借鉴了网上的一些分析。


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP