Android GUI之Activity、Window、View

  相信大家在接触Android之初就已经知道了Activity中的setContentView方法的作用了,很明显此方法是用于为Activity填充相应的布局的。那么,Activity是如何将填充的布局绘制出来的呢?实际上Activity将View的绘制与显示交给了Window对象来处理,下面我们通过源码来进行跟踪分析。

  Activity的源码如下,只给出我们关注的部分:

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {
      ……
    ……
    private Window mWindow;
    private WindowManager mWindowManager;
    ……

  /**
     * Retrieve the current {@link android.view.Window} for the activity.
     * This can be used to directly access parts of the Window API that
     * are not available through Activity/Screen.
     *
     * @return Window The current window, or null if the activity is not
     *         visual.
     */
    public Window getWindow() {
        return mWindow;
    }
    ……
    /**
     * Set the activity content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the activity.
     *
     * @param layoutResID Resource ID to be inflated.
     *
     * @see #setContentView(android.view.View)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

    /**
     * Set the activity content to an explicit view.  This view is placed
     * directly into the activity's view hierarchy.  It can itself be a complex
     * view hierarchy.  When calling this method, the layout parameters of the
     * specified view are ignored.  Both the width and the height of the view are
     * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
     * your own layout parameters, invoke
     * {@link #setContentView(android.view.View,android.view.ViewGroup.LayoutParams)}
     * instead.
     *
     * @param view The desired content to display.
     *
     * @see #setContentView(int)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(View view) {
        getWindow().setContentView(view);
        initWindowDecorActionBar();
    }
    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, IVoiceInteractor voiceInteractor) {
        attachBaseContext(context);
        mFragments.attachActivity(this, mContainer, null);
        mWindow = PolicyManager.makeNewWindow(this);

        ……
    }
……
}

PolicyManager的部分源码:

public final class PolicyManager {
    ……
private static final IPolicy sPolicy;
    static {
        // Pull in the actual implementation of the policy at run-time
       ……
            sPolicy = (IPolicy)policyClass.newInstance();
      ……
    }
    // Cannot instantiate this class
    private PolicyManager() {}
    // The static methods to spawn new policy-specific objects
    public static Window makeNewWindow(Context context) {
        return sPolicy.makeNewWindow(context);
    }
    ……
}

Policy的部分源码:

public class Policy implements IPolicy {
   ……
    public Window makeNewWindow(Context context) {
        return new PhoneWindow(context);
}
……
}

  从给出的源码我们可以看到,Activity内部含有一个Window类型的对象mWindow,当我们调用setContentView方法时,实际上是委托给了Window对象进行处理。Window本身是一个抽象类,它描述了android窗口的基本属性和行为特征。在activity的attach方法中通过mWindow = PolicyManager.makeNewWindow(this)创建了Window对象。通过追踪代码可知,
PolicyManager.makeNewWindow(this)实际上是调用Policy中的makeNewWindow方法,在此方法中创建了一个PhoneWindow对象。而PhoneWindow正是Window的子类。他们的关系图如下:

继续追踪源码,PhoneWindow对Window的抽象方法setContentView(int layoutResId)进行了实现,具体源码如下:

@Override
    public void setContentView(int layoutResID) {
        // 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) {
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                    getContext());
            transitionTo(newScene);
        } else {
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
    }

  在这个方法中我们可以看到首先对mContentParent进行了判断,如果为空的话则调用installDecor方法,通过hasFeature判断window是否具备某些特征,如果窗口不含有FEATURE_CONTENT_TRANSITIONS特征,则清空mContentParent中的所有子元素,为后面加载布局文件到mContentParent中做好准备。通过后面的判断,我们也可以看出无论走那个分支,其实都是对mContentParent布局内容做了更新。由此我们可以推断出mContentParent其实就是我们自己的布局的存放容器,它在PhoneWindow中定义如下:

    // This is the view in which the window contents are placed. It is either
    // mDecor itself, or a child of mDecor where the contents go.
    private ViewGroup mContentParent;

  那么mContentParent是在哪里被创建的呢,很显然是在方法installDecor中,方法installDecor的关键代码如下:

    private void installDecor() {
        if (mDecor == null) {
            mDecor = generateDecor();
           ……
        }
        if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
            ……
       }
}

  在这个方法中,我们可以看到,首先对mDecor进行判断,如果为空在调用generateDecor方法生成mDecor对象,那么mDecor对象是什么呢?通过查看代码,可以知道mDecor的类型为DecorView,此类型是定义在PhoneWindow中的一个内部类,它继承了FrameLayout。紧接着判断mContentParent是否为空,为空则调用generateLayout并通过传入参数mDecor生成了mContentParent对象。在这个方法中通过应用的主题、窗口特征等来确定使用的布局资源并将使用的布局添加mDecor中,而这些布局中都会含有一个id为content的ViewGroup(FrameLayout),此ViewGroup正是mContentParent,方法关键代码如下:

protected ViewGroup generateLayout(DecorView decor) {
        ……
        View in = mLayoutInflater.inflate(layoutResource, null);
        decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        mContentRoot = (ViewGroup) in;
        ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
       ……
        return contentParent;
    }

  由此我们可以确定,view的显示处理顺序为Activity->PhoneWindow->DecorView->ViewGroup(mContentView)->自定义的View(布局)。

  Activity中显示视图的层次结构,具体如下:

  疑问咨询或技术交流,请加入官方QQ群: (452379712)

作者:杰瑞教育

出处:http://blog.csdn.net/jerehedu/

本文版权归烟台杰瑞教育科技有限公司和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 10:27:40

Android GUI之Activity、Window、View的相关文章

activity window view 关系

1.Activity , Window和View的关系是什么? 跟踪Activity的源码就会发现:Activity.attch() -> PolicyManager -> Policy -> PhoneWindow -> mLayoutInflater.inflate()&mContentParent.addView()这只是一个简单的跟踪过程描述.通过跟踪源代码,就可以很清晰的看出他们三者的关系. Activity像一个工匠(控制单元),Window像窗户(承载模型),

Android——UI(1) (activity window decor)

1. activity window decor 之间的关系 android里:1个app里面有一个或多个window1个activity里有1个decor1个window有1个decor1个decor有多个viewgroup/layoutviewgroup/layout中有多个view. activity就是一组相对独立的功能,每个activity有个显示界面,界面显示在窗口上,所以有个窗口. installDecor() //PhoneWindow.java mContentParent =

Android中Activity Window View ViewGroup之间的关系

Activity:是Android四大组件之一,用于展示一个与用户交互的界面 ----展示界面 ----与用户交互 Activity相当于控制器,负责调用业务类的方法.简单的业务可以直接在Activity中处理. Activity通过内置是Window对象的setContentView(资源位置.资源类型.资源)方法来展示界面. 用户通过View操作界面. 与用户交互时,通过View来捕获事件,再通过WindowManagerService传递消息(当前操作的控件,事件的类型).Android框

android中activity,window,view之间的关系

activity:控制单元 window:承载模型 view:显示视图 几个小tip: 1.一个 Activity 构造的时候一定会构造一个 Window(PhoneWindow),并且只有一个 2.每个window有一个 ViewRoot(是一个View或ViewGroup) 3.通过window的addview方法把元素添加到window上. 4.可以通过 LayoutInflater 的 inflater 方法,可以把一个布局文件转换成view对象 5.界面上的点击等操作是由 Window

Activity、View、Window的理解一篇文章就够了

作者:细卷子 博客:http://www.jianshu.com/u/511ccb5a2012 要了解这三者之间的关系,我们带着问题通过分析源码一步一步来揭开它们的神秘面纱! 文章有点长,首先要理解Activity.View.Window,我提出了一些问题,这篇文章可以解答如下问题: 1.为什么要设计Activity.View.Window? 2.Activity工作过程是什么样的?(理解Activity) 3.Window是什么?它的职能是什么? 4.View跟Window有什么联系? 5.A

android——activity与view

1.       activity, view概述 进行android开发,只需具有java语言基础即可.入门的开发并不需要一些高深的java知识,如Swing,网络,线程,数据库等. 刚开始进行android开发,会觉得android的架构设计得很好,接口简单,模块独立.特别是像我这种C++程序员来说,没有了框架自动添加的很多消息映射宏,窗口的启动,过多复杂的类等.如android应用程序的基本构成就采用MVC模式,界面与逻辑开发独立,程序员可以只专注于程序逻辑开发,美工可以专注于界面设计.

Android截屏截图方法汇总(Activity、View、ScrollView、ListView、RecycleView、WebView截屏截图)

Android截屏 Android截屏的原理:获取具体需要截屏的区域的Bitmap,然后绘制在画布上,保存为图片后进行分享或者其它用途 一.Activity截屏 1.截Activity界面(包含空白的状态栏) /** * 根据指定的Activity截图(带空白的状态栏) * * @param context 要截图的Activity * @return Bitmap */ public static Bitmap shotActivity(Activity context) { View vie

Android GUI之View绘制流程

在上篇文章中,我们通过跟踪源码,我们了解了Activity.Window.DecorView以及View之间的关系(查看文章:http://blog.csdn.net/jerehedu/article/details/47021541).那么整个Activity的界面到底是如何绘制出来的呢?既然DecorView作为Activity的顶层界面视图,那么整个界面的绘制工作应该从它开始,下面我们继续跟踪源码,看看是不是这样的. Activity在启动过程中会调用主线程ActivityThread中的

Android 利用addView 动态给Activity添加View组件

本文主要讲述如何动态给UI界面添加布局和控件,在编程的时候很多时候需要动态显示一些内容,在动态添加View的时候,主要使用addView方法. 1. addView方法简介 在Android 中,可以利用排版View的 addView 函数,将动态产生的View 物件加入到排版View 中. 例子如下: 界面代码: <LinearLayout android:id="@+id/viewObj" android:layout_width="wrap_content&quo