先前只是在看Input子系统的时候,因为需要处理Activity之间的焦点问题和视图控件行为了解了按键在视图体系中的分发和基本的视图层级组成,没有深入去看这一部分。现在着重看下这部分,因为可能以后会有视图层定制的需求(现在的ROM定制基本都是针对这一部分定制系统层视图类或者Luncher)。
这里还是一样,基于Android4.4的代码从应用层通过实例来分析整个框架。
首先看一下基本的类之间的关系。
1.Context.java (frameworks\base\core\java\android\content)
2.View.java (frameworks\base\core\java\android\view)
3.Window.java (frameworks\base\core\java\android\view)
4.ViewGroup.java (frameworks\base\core\java\android\view)
5.Activity.java (frameworks\base\core\java\android\app)
6.Service.java (rameworks\base\core\java\android\app)
7.Intent.java (frameworks\base\core\java\android\content)
8.ViewRootImpl.java (frameworks\base\core\java\android\view)
9.Application.java (frameworks\base\core\java\android\app)
Context常用来建立包含关系,android应用框架中就是使用了对象包含关系来体现各种组合的。
主要有这么几点:
1.View和Context是聚合关系
2.Activity和Context是继承关系
3.ViewGroup和View是继承和组合关系
4.window中包含多个view
5.Activity和Service,Application都是Context的派生类
以上几点,总结成一个类图:
这些类的具体作用,最好是看源码中的注释:
Context是一个和应用场景相关的全局信息接口,通过Context可以访问应用相关的资源和类,也用于向上实施一些应用级别的诸如启动activities,broadcasting和接收intents之类的操作:
/**
* Interface to global information about an application environment. This is
* an abstract class whose implementation is provided by
* the Android system. It
* allows access to application-specific resources and classes, as well as
* up-calls for application-level operations such as launching activities,
* broadcasting and receiving intents, etc.
*/
View提供了一个基本的用户界面组件,view自身拥有的一个方向区域可以用来进行UI描绘和事件处理。还是widgets这个用来创建UI组件的体系的基类。
View的一个子类ViewGroup是layouts的基类,是一个用来承载其他Views、ViewGroups或者定义布局属性的不可见容器。
* This class represents the basic building block for user interface components. A View
* occupies a rectangular area on the screen and is responsible for drawing and
* event handling. View is the base class for <em>widgets</em>, which are
* used to create interactive UI components (buttons, text fields, etc.). The
* {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
* are invisible containers that hold other Views (or other ViewGroups) and define
* their layout properties.
Window是一个顶级的用于控制窗口样式和行为的抽象策略类,Window的实例应该被当成顶级的View被加入到Window manager中。
Window提供标准的诸如背景,标题区域,默认的按键处理方式这些基本策略
这个类只有PhoneWindow这个派生类,PhoneWindow在使用的时候需要初始化,通常都会被重构,而且会有一个工厂方法用来在不知道详细实现方式的时候创建Window实例。
/**
* Abstract base class for a top-level window look and behavior policy. An
* instance of this class should be used as the top-level view added to the
* window manager. It provides standard UI policies such as a background, title
* area, default key processing, etc.
*
* <p>The only existing implementation of this abstract class is
* android.policy.PhoneWindow, which you should instantiate when needing a
* Window. Eventually that class will be refactored and a factory method
* added for creating Window instances without knowing about a particular
* implementation.
*/
Activity是一个可以被用户处理的独立的的一个类。几乎所有的Activity都会和用户有交互,所以Activity这个类关注于创建window以方便用户通过setContentView来放置UI组件。activitys通常以全屏Window的方式展现给用户,并且通常可以以浮动window(通过windowIsFloating这个属性)或者嵌入到其他activity(通过ActivityGroup)的方式被用户使用。
* An activity is a single, focused thing that the user can do. Almost all
* activities interact with the user, so the Activity class takes care of
* creating a window for you in which you can place your UI with
* {@link #setContentView}. While activities are often presented to the user
* as full-screen windows, they can also be used in other ways: as floating
* windows (via a theme with {@link android.R.attr#windowIsFloating} set)
* or embedded inside of another activity (using {@link ActivityGroup}).
*
Service是一个用于处理长时间任务操作或者为其他应用提供功能函数的组件。Serivce需要在AndroidManifest.xml中注册,Service通常可以通过startService和bindService两种方式启动(难怪说是两种方式,出自这里)
* A Service is an application component representing either an application's desire
* to perform a longer-running operation while not interacting with the user
* or to supply functionality for other applications to use. Each service
* class must have a corresponding
* {@link android.R.styleable#AndroidManifestService <service>}
* declaration in its package's <code>AndroidManifest.xml</code>. Services
* can be started with
* {@link android.content.Context#startService Context.startService()} and
* {@link android.content.Context#bindService Context.bindService()}.
*
Intent是一个行为的抽象描述。可以通过startActivity的方式启动一个应用,可以通过sendBroadcast发送到注册了BroadcastReceiver的组件,或者通过startService、bindService去与后台service交互。
Intent提供了不同应用之间延迟运行时绑定的能力。通常被用在启动activities,充当activity之间的纽带,通常是一个带有对行为的抽象描述的被动数据结构。
* An intent is an abstract description of an operation to be performed. It
* can be used with {@link Context#startActivity(Intent) startActivity} to
* launch an {@link android.app.Activity},
* {@link android.content.Context#sendBroadcast(Intent) broadcastIntent} to
* send it to any interested {@link BroadcastReceiver BroadcastReceiver} components,
* and {@link android.content.Context#startService} or
* {@link android.content.Context#bindService} to communicate with a
* background {@link android.app.Service}.
*
* <p>An Intent provides a facility for performing late runtime binding between the code in
* different applications. Its most significant use is in the launching of activities, where it
* can be thought of as the glue between activities. It is basically a passive data structure
* holding an abstract description of an action to be performed.</p>
*
ViewRootImpl是一个顶级的View,实现了View和WindowManager之间的协议。大多是对WindowManagerGlobal细节的一些实现
/**
* The top of a view hierarchy, implementing the needed protocol between View
* and the WindowManager. This is for the most part an internal implementation
* detail of {@link WindowManagerGlobal}.
*
* {@hide}
*/
ViewGroup是一个用于承载其他视图的特殊视图,是布局和视图容器的基类,同时类中还定义LayoutParams这个用来表示布局属性的基类。
* A <code>ViewGroup</code> is a special view that can contain other views
* (called children.) The view group is the base class for layouts and views
* containers. This class also defines the
* {@link android.view.ViewGroup.LayoutParams} class which serves as the base
* class for layouts parameters.
Layout是一个用来管理可视text layout的基类,在text会被编辑的时候可以用DynamicLayout,不会被编辑的时候用staticLayout
/**
* A base class that manages text layout in visual elements on
* the screen.
* <p>For text that will be edited, use a {@link DynamicLayout},
* which will be updated as the text changes.
* For text that will not change, use a {@link StaticLayout}.
*/