LayoutInflater是如何“移花接木”-上篇
开篇之前,先唠叨唠叨为何用移花接木,移花接木,指使用手段更换事物来欺骗他人,不过开始想到的是偷梁换柱,暗渡陈仓,还有暗送秋波这些词,仔细查阅了下资料,无奈,
两者大都是贬义词,后者又是献媚取宠、暗中勾结之类的含义,也是贬义,本着怜香惜玉的原则,最终选择移花接木这个词,为标题所用。
说到LayoutInflater,很多人说熟悉,它将我们的xml布局转化为对应的view,看呐,xml转换为view,多么神奇,犹如魔术师一般,移花接木,这个词很贴切
切入正题,分析下LayoutInflater是如何上演“移花接木”魔术的
来看看,获取LayoutInflater的方式,如下:
1.使用Contex上下文,获取服务
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2.使用Activity获取
xxxActivity.this.getLayoutInflater();
3.使用LayoutInfalter静态方法
LayoutInflater.from(Context context);
以上是我们经常使用的几种获取LayoutInflater的方法,接下来,看看它们内部做了些什么
1.使用Contex上下文
Context本身是抽象类,需要找到子类ContextImpl,重写的getSystemService
Context.java
@Overridepublic Object getSystemService(String name) {return SystemServiceRegistry.getSystemService(this, name);}
这里有个SystemServiceRegistry类,拥有getSystemService(ContextImpl ctx, String name)方法,代码如下:
SystemServiceRegistry.java
public static Object getSystemService(ContextImpl ctx, String name) {ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);return fetcher != null ? fetcher.getService(ctx) : null;}public static String getSystemServiceName(Class<?> serviceClass) {return SYSTEM_SERVICE_NAMES.get(serviceClass);}
private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES = new HashMap<Class<?>, String>();private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS = new HashMap<String, ServiceFetcher<?>>();//静态代码快
static{//省略...registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,new CachedServiceFetcher<LayoutInflater>() {@Overridepublic LayoutInflater createService(ContextImpl ctx) {return new PhoneLayoutInflater(ctx.getOuterContext());}});//...}
private static <T> void registerService(String serviceName, Class<T> serviceClass, ServiceFetcher<T> serviceFetcher) { SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName); SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher); }
从上面看来, SYSTEM_SERVICE_FETCHERS是一个map集合,存储的是ServiceFetcher对象 static代码块,在类加载的时候会被加载,此时,registerService被调用,注册LayoutInflater服务,注册的服务会被缓存到SYSTEM_SERVICE_FETCHERS中,便于在 getSystemService的时候获取 PhoneLayoutInflater实际上是LayoutInfalter的子类,在CreateService方法中被创建并返回 static块中要注册的服务很多,这里只保留一个,具体可以看SystemServiceRegistry源码 那么,SystemServiceRegistry类在哪里用到呢,如果了解ContextImpl代码的话,就容易了,会有以下代码,mServiceCache是成员变量,它的定义是为了后面代码的执行, 无其他用处。 ContextImpl.java
//The system service cache for the system services that are cached per-ContextImpl.final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
SystemServiceRegistry.java
public static Object[] createServiceCache() {return new Object[sServiceCacheSize];}
Activity启动,创建Context上下文实例时,createServiceCache()被执行,从而SystemServiceRegistry的static代码被加载到内存,各种服务注册并缓冲到集合中,方便在其他地 方使用某个服务时,从缓冲集合中获取。也就是,在context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)时,对应的是从SYSTEM_SERVICE_FETCHERS缓冲 集合中取出 2.使用Activity获取 Activity.java
public LayoutInflater getLayoutInflater() {return getWindow().getLayoutInflater();}
getWindow()方法获取的是Activity中的mWindow成员变量,该变量在Activity的attach方法中初始化,Actiivty在启动时会调用attach方法
public Window getWindow() {return mWindow;}final void attach(Context context, ActivityThread aThread,...省略参数//...mWindow = new PhoneWindow(this);//...}
接下来看看PhoneWindow类的getLayoutInflater(),如下: PhowWindow.java
public LayoutInflater getLayoutInflater() {return mLayoutInflater;}
public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); }
继续看LayoutInflater类, LayoutInflater.java
public static LayoutInflater from(Context context) {LayoutInflater LayoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);if (LayoutInflater == null) {throw new AssertionError("LayoutInflater not found.");}return LayoutInflater;}
到了这里,是不是感觉很熟悉,没错,这里获取getLayoutInflater对象方式,最终与第一种方式是一样的 3.使用LayoutInfalter静态方法 LayoutInflater.java
public static LayoutInflater from(Context context) {LayoutInflater LayoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);if (LayoutInflater == null) {throw new AssertionError("LayoutInflater not found.");}return LayoutInflater;}
实际上,还是第一种获取getLayoutInflater对象方式一样 也就是说,我们常用的获取LayoutInflater对象方法,最终都是通过getSystemService系统服务的方式获取的
LayoutInflater是如何“移花接木”-上篇相关推荐
- Android LayoutInflater 源码解析
在上篇文章中我们学习了setContentView的源码,还记得其中的LayoutInflater吗?本篇文章就来学习下LayoutInflater. @Overridepublic void set ...
- Android LayoutInflater 的使用
LayoutInflater 有三种实现方法 1 LayoutInflater inflater = LayoutInflater.from(context) 这种方法常用在弹框 2 LayoutIn ...
- IOS视频编辑功能详解上篇-添加水印
前言 用代码在简单视频编辑中,主要就是加美颜.水印(贴图).视频截取.视频拼接.音视频的处理,在美颜中,使用GPUImage即可实现多种滤镜.磨皮美颜的功能,并且可以脸部识别实时美颜等功能,这个有很多 ...
- TypeScript 从听说到入门(上篇)
我为什么会这样念念又不忘 / 你用什么牌的箭刺穿我心脏 我也久经沙场 / 戎马生涯 / 依然 / 被一箭刺伤 --李荣浩<念念又不忘> 接下来我会分上.下两篇文章介绍 TypeScript ...
- 进程控制概念简介 多线程上篇(三)
进程控制 进程的基本数据信息是操作系统控制管理进程的数据集合,这些信息就是用来控制进程的,此处我们说的进程控制就是进程的管理. 比如进程有状态,那么进程的创建.终止,状态的切换,这都不是进程自主进行的 ...
- “移花接木”偷换广告:HTTPS劫匪木马每天打劫200万次网络访问
本文讲的是"移花接木"偷换广告:HTTPS劫匪木马每天打劫200万次网络访问,近年来,国内各大网站逐渐升级为HTTPS加密连接,以防止网站内容被篡改.用户数据被监听.但是一向被认为 ...
- LayoutInflater的inflate函数用法详解
LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...
- Android中LayoutInflater的使用
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layou ...
- 小程序工程化实践(上篇)-- 手把手教你撸一个小程序 webpack 插件,一个例子带你熟悉 webpack 工作流程...
本文基于 webpack 4 和 babel 7,Mac OS,VS Code 小程序开发现状: 小程序开发者工具不好用,官方对 npm 的支持有限,缺少对 webpack, babel 等前端常用工 ...
最新文章
- pandas为所有的列名添加后缀(add_suffix)
- 贝叶斯估计和最大后验估计
- 设置Eclipse中的Courier New字体
- 怎样在待机时锁定计算机,教你电脑待机锁屏怎么设置
- python无法打开_如何解决Windows命令行无法运行python文件?
- 设计模式学习笔记——组合(Composite)模式
- 科学计算机怎么输入角度,怎么用科学计算器算角度
- UCB CS285课程笔记目录
- linux 限速命令,linux上传限速脚本介绍
- 怎么将.txt改为任意文件格式
- 裸眼3D全息投影技术
- 邮箱的收信服务器在哪里,网易邮箱帮助中心 - 收信
- 数学——行向量和列向量
- mysql limit 01怎么理解_MySQL limit实际用法的详细解析
- Pycharm 引入类报错Unresolved reference ‘attempt_load‘
- 自定义View基础:Android中的颜色和颜色混合
- 计算机网络技术面试题(未完结)
- 动态壁纸,视频特效- Android
- 163邮箱开通后到期怎么续费?收费邮箱续费指南
- 网络计划经典例题讲解