Android系统Java源码探索(2)—View工作原理

一 前言

这两天把Android系统的主要进程大致了解了一下,启动顺序大概如下:
(1)系统启动时会创建init进程,该进程是所有进程的始祖;
(2)由init进程孵化出了Zygote进程,该进程是Java虚拟机进程,是所有Java进程的鼻祖;
(3)由Zygote进程孵化出System Service进程(AMS,WMS等进程);
(4)由Zygote进程孵化Media Service进程(navtie进程);
(5)由Zygote进程孵化出App层的第一个进程Launch进程;
(6)由Zygote进程孵化出App层的其他进程,例如系统app,开发者开发的App等;
这篇要讲的View,就要从Zygote进程孵化出Window Manager Service(下文简称WMS)开始讲。

二 WMS与Window 及View的关系梳理

WMS是JAVA Framework层用来管理Window的进程,其实每个Window并不是实际存在的,它是以View的形式存在的。
在App的主进程会创建一个ActivityThread,在主线程中,开发者声明的Activity会被创建。我们看看Activity的attach方法:

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, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback) {
        attachBaseContext(context);
        mFragments.attachHost(null /*parent*/);
        //创建PhoneWindow对象
        mWindow = new PhoneWindow(this, window, activityConfigCallback);
       
        ........

        mWindow.setWindowManager(
                (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
                mToken, mComponent.flattenToString(),
                (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
        if (mParent != null) {
            mWindow.setContainer(mParent.getWindow());
        }
        mWindowManager = mWindow.getWindowManager();
        mCurrentConfig = config;
        mWindow.setColorMode(info.colorMode);
    }

Window是一个抽象类,具体的实现在PhoneWindow,在看看PhoneWindow中的方法:

public void setContentView(View view, ViewGroup.LayoutParams params) {
        // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
        // decor, when theme attributes and the like are crystalized. Do not check the feature
        // before this happens.
        if (mContentParent == null) {
            //1.初始化DecorView方法
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }
     .....
    }

 private void installDecor() {
        mForceDecorInstall = false;
        if (mDecor == null) {
           //2. 生成decorView方法
            mDecor = generateDecor(-1);
         mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            mDecor.setIsRootNamespace(true);
            if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
               mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
            }
        } else {
            mDecor.setWindow(this);
        }


protected DecorView generateDecor(int featureId) {
        // System process doesn't have application context and in that case we need to directly use
        // the context we have. Otherwise we want the application context, so we don't cling to the
        // activity.
        Context context;
        if (mUseDecorContext) {
            Context applicationContext = getContext().getApplicationContext();
            if (applicationContext == null) {
                context = getContext();
            } else {
                //3.实例化DecorView
                context = new DecorContext(applicationContext, getContext().getResources());
                if (mTheme != -1) {
                    context.setTheme(mTheme);
                }
            }
        } else {
            context = getContext();
        }
        //生成decorview
        return new DecorView(context, featureId, this, getAttributes());
    }

Decroview其实是View的根类,一般来说它的包括标题栏(TitleView)和内部栏(ContentView)。
WindowManager类通过创建ViewRootImpl对象,将DecorView添加到PhoneWindow

public void addView(View view, ViewGroup.LayoutParams params,Display display, Window parentWindow) {
    ...
    ViewRootImpl root = new ViewRootImpl(view.getContext(), display);        
    view.setLayoutParams(wparams);    
    mViews.add(view);    
    mRoots.add(root);    
    mParams.add(wparams);        
    root.setView(view, wparams, panelParentView);
    ...
}

Window,View,WindwoManager,WMS的关系如下:


Android系统Java源码探索(2)—View工作原理_第1张图片
Window View WindowManager WMS之间的关系.jpg

三 View的工作原理

View树的绘制是从ViewRootImpl的performTraversals方法开始的,先看源码:

    private void performTraversals() {
       .........
       if (!mStopped || mReportNextDraw) {
        .........
                if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()|| mHeight != host.getMeasuredHeight() || contentInsetsChanged || updatedConfiguration) {
                   //decroView的MeasureSpec 根据屏幕的尺寸和其自身的LayoutParms确定
                    int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
                    int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
                   // 1
                    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);  
                }
            }
        } 
        ........
        if (didLayout) {
            //2
            performLayout(lp, mWidth, mHeight);
        }
        ........
        if (!cancelDraw && !newSurface) {
            // 3
            performDraw();
        } 
    }
 private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
        if (mView == null) {
            return;
        }
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
        try {
           //执行decorview的measure方法
            mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_VIEW);
        }
    }
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
            int desiredWindowHeight) {
       .......
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout");
        try {
           //host代表DecorView,其中decorview距离窗体上下左右的位置分别是0,0,host.getMeasuredWidth(),host.getMeasuredHeight()
            host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
           .......
    }

private void performDraw() {
        ....
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw");
        try {
            draw(fullRedrawNeeded);
        } finally {
            mIsDrawing = false;
            Trace.traceEnd(Trace.TRACE_TAG_VIEW);
        }
.....
}

从上面的代码中可以看出主要执行了三个方法,分别是performMeasure、performLayout、performDraw,在这三个方法内部又会分别调用measure、layout、draw这三个方法来进行不同的流程。
View绘制流程图如下:


Android系统Java源码探索(2)—View工作原理_第2张图片
View的绘制过程.jpg

主要是三个流程:测量,布局,绘制,
测量过程:从根View(DecorView),自上而下递归测量每一级,每一个子View的尺寸和大小;
布局过程:把测量阶段的数据,自上而下递归的复制给相应的View,进行布局;
绘制过程:根据布局好的大小和位置,绘制view;

3.1 Measure测量过程

由于DecorView没有measure方法,我们继续看他的父类FrameLayout,好像也没有measure,继续看FrameLayout的父类ViewGroup,也没有该方法,继续看ViewGroup的父类View,终于有measure方法了,简化版源码如下:

    public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
       ......
        if (forceLayout || needsLayout) {
          .....
            if (cacheIndex < 0 || sIgnoreMeasureCache) {
                // measure ourselves, this should set the measured dimension flag back
                onMeasure(widthMeasureSpec, heightMeasureSpec);
                mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
            } 
           .....
    }

可以看出有执行了onMeasure(widthMeasureSpec, heightMeasureSpec),继续看源码:

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 1.计算View尺寸并保存      
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

getDefaultSize方法用来计算View的尺寸和大小,其中MeasureSpec包含了父View对于子View大小规则限制的信息,它是一个32位的值,前两位代表SpecMode(测量模式),SpecSize(测量大小)。

// 计算View的大小
public static int getDefaultSize(int size, int measureSpec) {
        //size是Layoutparams指定的参数
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
       //获取子View的测量大小
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
       // 父View对子View的大小没有任何限制;
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
        case MeasureSpec.AT_MOST:
        // 父View指定了一个可用大小的SpecSize,View大小不能大于这个值;
        case MeasureSpec.EXACTLY:
        // 父View已经检测出了子View的精确值,View的最终大小就是specSize
           result = specSize;
            break;
        }
        return result;
    }

这里引用一段《Android开发者艺术探索》对普通View MeasureSpec创建规则的几点解释:

  • 当View采用固定宽/高时(即设置固定的dp/px),不管父容器的MeasureSpec是什么,View的MeasureSpec都是EXACTLY模式,并且大小遵循我们设置的值。
  • 当View的宽/高是match_parent时,View的MeasureSpec都是EXACTLY模式并且其大小等于父容器的剩余空间。
  • 当View的宽/高是wrap_content时,View的MeasureSpec都是AT_MOST模式并且其大小不能超过父容器的剩余空间。
  • 父容器的UNSPECIFIED模式,一般用于系统内部多次Measure时,表示一种测量的状态,一般来说我们不需要关注此模式。

3.2 Layout布局过程

根据测量阶段的测量大小,开始布局。
先看View的layout源码

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); //1.设置View的四个顶点位置
        if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
            //根据view的位置和真正大小开始布局
            onLayout(changed, l, t, r, b);
        ......
        }
    }

我们先查看View的onLayout方法

 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    //空方法,自定义View会集成该View,进行布局;
    }

3.3 Draw绘制过程

根据layout布局的位置开始绘制View。

public void draw(Canvas canvas) {
        final int privateFlags = mPrivateFlags;
        final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
                (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
        mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
        /*
         * Draw traversal performs several drawing steps which must be executed
         * in the appropriate order:
         *
         *      1.绘制背景图—— Draw the background
         *      2.如果有必要,保存画布图层 If necessary, save the canvas' layers to prepare for fading
         *      3.绘制View内容——Draw view's content
         *      4.绘制View的子View(如果有子view)——Draw children
         *      5.如果有必要,绘制View的阴影效果If necessary, draw the fading edges and restore layers
         *      6. 绘制装饰(例如滚动条)——Draw decorations (scrollbars for instance)
         */
        // Step 1, draw the background, if needed
        int saveCount;
        if (!dirtyOpaque) {
            drawBackground(canvas);
        }
        // skip step 2 & 5 if possible (common case)
        final int viewFlags = mViewFlags;
        boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
        boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
        if (!verticalEdges && !horizontalEdges) {
            // Step 3, draw the content
            if (!dirtyOpaque) onDraw(canvas);
            // Step 4, draw the children
            dispatchDraw(canvas);
            drawAutofilledHighlight(canvas);
            // Overlay is part of the content and draws beneath Foreground
            if (mOverlay != null && !mOverlay.isEmpty()) {
                mOverlay.getOverlayView().dispatchDraw(canvas);
            }
            // Step 6, draw decorations (foreground, scrollbars)
            onDrawForeground(canvas);
            // Step 7, draw the default focus highlight
            drawDefaultFocusHighlight(canvas);
            if (debugDraw()) {
                debugDrawFocus(canvas);
            }
            // we're done...
            return;
        }
      .......
    }

由于能力有限,有的方面可能理解不当,后期会根据情况随时调整。

你可能感兴趣的:(Android系统Java源码探索(2)—View工作原理)