惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
腾讯CDC
T
Tailwind CSS Blog
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
The Cloudflare Blog
D
DataBreaches.Net
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
B
Blog
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - exce4

高级php面试题 IOS学习之路--OC的基础知识 1000台电脑找出一只猫的“神经网络” 麦克斯韦方程组(彩图完美解释版) PDF的信息表达原理及特点分析 现在开始(Do It Now) QML引擎的演进,第一部分 犀利点评:csdn某文<第一次创业还是失败了---分享失败的经验> 【筆記】八部金剛功-張道長口述記載 为什么V8引擎这么快? node.js入门及express.js框架 Android源代码结构分析 Android文件系统的结构 android linux 命令 android root权限破解分析 乔布斯《遗失的访谈》全文:尘封16年的预见 重新认识Java Dom加载让图片加载完再执行 Java开发超级工具集
重新认识Android
exce4 · 2013-09-30 · via 博客园 - exce4

首先我们来看下源码中源于Activity的定义:

  1. public class Activity extends ContextThemeWrapper  
  2.         implements LayoutInflater.Factory2,  
  3.         Window.Callback, KeyEvent.Callback,  
  4.         OnCreateContextMenuListener, ComponentCallbacks2 {  
  5.     ...  
  6. }  

下面我们来详细分析每一部分的具体意义:

extends ContextThemeWrapper表示Activity本质上是一个ContextThemeWrapper,而ContextThemeWrapper具体是什么呢?看ContextThemeWrapper在源码中的定义:

  1. public class ContextThemeWrapper extends ContextWrapper {  
  2.     ...  
  3. }  

可见ContextThemeWrapper是一个ContextWrapper,继续往下看:

  1. public class ContextWrapper extends Context {  
  2.       Context mBase;  
  3.     ...  
  4. }  

ContextWrapper本质上是一个Context,context 的定义如下:

  1. public abstract class Context {  
  2.     ...  
  3. }  

整体结构如下图所示(图引用自:http://blog.csdn.net/qinjuning/article/details/7310620):

 

Context是一个抽象类,因此可以知道Activity其实就是一个Context,并实现了一些接口,如何理解Context呢?

Context 俗称上下文,在很多对象定义中我们都用到了Context,例如ImageViewimageView = new ImageView(this); 这里的this就是当前Activity所在的Context,源码中对Context的解释如下:

Interface to global information about anapplication environment. This is an abstract class whose implementation isprovided by the Android system. It allows access to application-specificresources and classes, as well as up-calls for application-level operationssuch as launching activities, broadcasting and receiving intents, etc.

Context只是一个接口,真正实现Context功能的是ContexImpl类,为了了解Context具体有什么作用,我们先来了解下Context中定义了哪些接口:

  1. public abstract ComponentNamestartService(Intent service);  
  2. public abstract boolean stopService(Intentservice);  
  3. public abstract void startActivity(Intentintent);  
  4. public abstract void sendBroadcast(Intentintent);  
  5. public abstract Intent registerReceiver(BroadcastReceiverreceiver, IntentFilter filter);  
  6. public abstract Resources getResources();  


以上是Context众多接口中的一个片段,看着接口是不是很熟悉?其实我们经常用到的一些函数其实都是在Context中定义的,因此Context可以被认为是用来封装一下通用功能的一个类,当然这个类不仅仅是针对Activity,最常见的service也是继承自Context,以及一大堆类都继承自Context,具体可参考:http://developer.android.com/reference/android/content/Context.html,关于Context的具体介绍可参考:http://blog.csdn.net/qinjuning/article/details/7310620

ContextWrapper仅仅是对Context的简单封装,如果要对Context修改,我们只需要修改ContextWrapper,而不需要对通用的Context进行修改,ContextWrapper的目的仅此而已。而ContextThemeWrapper只是在ContextWrapper的基础上加入了Theme相关的一些内容,对于Activity来说需要处理一些Theme相关的东西,但是对于Service来说只需继承ContextWrapper,因为Service不需要处理Theme相关的内容。

分析完extends部分,我们再来看下implements部分,extends决定了Activity的本质,implements部分可以认为是对Activity的扩展。

  • LayoutInflater.Factory:通过LayoutInflater来inflate一个layout时的回调接口
  • Window.Callback: Activity 靠这个接口才有机会对消息进行处理,这部分涉及到消息的传递,以后将专门介绍。
  • ComponentCallbacks2:定义了内存管理的接口,内存过低时的回调和处理处理接口
  • KeyEvent.Callback:键盘事件响应的回调接口,例如onKeyDown()等
  • OnCreateContextMenuListener:上下文菜单显示事件的监听接口,通过实现该方法来处理上下文菜单显示时的一些操作

通过以上分析,我们大概了解了Activity具体是什么了,这对以后理解Activity应该能带来一定的帮助。

Examples of android:scaleType attribute.

Top row (l-r) centercenterCrop,centerInside.

Bottom row (l-r): fitCenterfitStartfitEndfitXY.

 ActionBar