当前位置: 首页 > news >正文

android 换肤框架详解2-LayoutInflater源码解析

前面已经讲解了换肤,换资源文件的逻辑,接下来需要讲解一下如何实现自动换肤的逻辑第一步

查看LayoutInflater源码

传送门android 换肤框架详解1-换肤逻辑基本-CSDN博客

要对自动换肤有更深的了解,这里就需要对LayoutInflater源码有所了解

  1. LayoutInflater源码解析

从inflate进来

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {return inflate(resource, root, root != null);}
    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {//这里可以知道,Resources是通过getContext()拿到的,我们可以直接改变getContext()中的Resourcesfinal Resources res = getContext().getResources();if (DEBUG) {Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("+ Integer.toHexString(resource) + ")");}//尝试“预编译”路径(Android 12 以后增加的优化)// 如果系统把这个 layout 提前编译成了预置的 Java 类(precompiled layout),就直接 new 出来返回,不走传统 XML 解析。View view = tryInflatePrecompiled(resource, res, root, attachToRoot);if (view != null) {return view;}//通过 AssetManager 打开一个 XmlResourceParser,指向该 layout xml 文件XmlResourceParser parser = res.getLayout(resource);try {//解析布局return inflate(parser, root, attachToRoot);} finally {parser.close();}}

 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) {Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");final Context inflaterContext = mContext;final AttributeSet attrs = Xml.asAttributeSet(parser);Context lastContext = (Context) mConstructorArgs[0];mConstructorArgs[0] = inflaterContext;View result = root;try {advanceToRootNode(parser);final String name = parser.getName();//解析merge标签if (TAG_MERGE.equals(name)) {if (root == null || !attachToRoot) {throw new InflateException("<merge /> can be used only with a valid "+ "ViewGroup root and attachToRoot=true");}rInflate(parser, root, inflaterContext, attrs, false);} else {// Temp is the root view that was found in the xml//解析xml里面获取到的数据,进行创建Viewfinal View temp = createViewFromTag(root, name, inflaterContext, attrs);ViewGroup.LayoutParams params = null;if (root != null) {if (DEBUG) {System.out.println("Creating params from root: " +root);}// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs);if (!attachToRoot) {// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params);}}if (DEBUG) {System.out.println("-----> start inflating children");}// Inflate all children under temp against its context.rInflateChildren(parser, temp, attrs, true);if (DEBUG) {System.out.println("-----> done inflating children");}// We are supposed to attach all the views we found (int temp)// to root. Do that now.//如果存在VIewGroup,添加View到View上if (root != null && attachToRoot) {root.addView(temp, params);}// Decide whether to return the root that was passed in or the// top view found in xml.if (root == null || !attachToRoot) {result = temp;}}} catch (XmlPullParserException e) {final InflateException ie = new InflateException(e.getMessage(), e);ie.setStackTrace(EMPTY_STACK_TRACE);throw ie;} catch (Exception e) {final InflateException ie = new InflateException(getParserStateDescription(inflaterContext, attrs)+ ": " + e.getMessage(), e);ie.setStackTrace(EMPTY_STACK_TRACE);throw ie;} finally {// Don't retain static reference on context.mConstructorArgs[0] = lastContext;mConstructorArgs[1] = null;Trace.traceEnd(Trace.TRACE_TAG_VIEW);}return result;}}

正在创建View的地方

  private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {return createViewFromTag(parent, name, context, attrs, false);}
    View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,boolean ignoreThemeAttr) {try {//通过注册回调创建,这里就是自动将资源文件布局到View上的关键,这里会通过接口回调,回调到自己创建的//监听上,在这个监听里面保存对应的参数和类,然后切换换肤的时候一键修部署到ViewView view = tryCreateView(parent, name, context, attrs);if (view == null) {final Object lastContext = mConstructorArgs[0];mConstructorArgs[0] = context;try {//没有通过监听创建View,走自己的解析方法if (-1 == name.indexOf('.')) {view = onCreateView(context, parent, name, attrs);} else {view = createView(context, name, null, attrs);}} finally {mConstructorArgs[0] = lastContext;}}return view;} catch (InflateException e) {throw e;} }
   public final View tryCreateView(@Nullable View parent, @NonNull String name,@NonNull Context context,@NonNull AttributeSet attrs) {if (name.equals(TAG_1995)) {// Let's party like it's 1995!return new BlinkLayout(context, attrs);}View view;//判断是否创建了回调类,如果创建了就走创建类的回调创建View,自动换肤的关键在这里if (mFactory2 != null) {view = mFactory2.onCreateView(parent, name, context, attrs);} else if (mFactory != null) {view = mFactory.onCreateView(name, context, attrs);} else {view = null;}if (view == null && mPrivateFactory != null) {view = mPrivateFactory.onCreateView(parent, name, context, attrs);}return view;}
  public void setFactory2(Factory2 factory) {//在View解析的时候,会先执行这里,如果有创建监听,会执行这里的创建View,我们自定义的这个类,可以不走系统的解析//方法,在自己创建的factory中进行对应解析创建,并且保存对应View在xml中的状态,在换行的时候就可以将保存的类进行//换肤处理if (mFactorySet) {throw new IllegalStateException("A factory has already been set on this LayoutInflater");}if (factory == null) {throw new NullPointerException("Given factory can not be null");}mFactorySet = true;if (mFactory == null) {mFactory = mFactory2 = factory;} else {mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2);}}

http://www.dtcms.com/a/326601.html

相关文章:

  • 《零基础入门AI:深度学习基础核心概念解析(从激活函数到反向传播)》
  • 大模型提示词工程实践:提示词工程实践-引导大模型完成任务
  • 直播美颜SDK架构设计指南:美白滤镜的高效实现与跨平台适配
  • MySQL 基本语法
  • 【网络基础】深入理解 TCP/IP 协议体系
  • 秒懂边缘云|1分钟了解边缘安全加速 ESA
  • GCC C++实现Matlab矩阵计算和数学函数功能
  • 乡土诗性的多重奏鸣——儿歌《生我养我的小村庄》文学赏析
  • C5.3:发射极偏置和LED驱动电路
  • 26考研|西安电子科技大学优势学科、25考研复试线及就业质量分析报告
  • 力扣热题100-----322.零钱兑换
  • 事务的特性
  • 下一代防火墙组网方案
  • IoT/透过oc_lwm2m/boudica150 源码中的AT指令序列,分析NB-IoT接入华为云物联网平台IoTDA的工作机制
  • visual studio 2015 使用番茄助手(Visual Assist)给函数自动添加注释模板
  • WSL / Linux安装MySQL(以及注意事项)
  • 嵌入式学习的第四十八天-中断+OCP原则
  • Photoshop图层混合模式:实现图像元素透明度渐变过渡的终极指南
  • Effective C++ 条款36: 绝不重新定义继承而来的非虚函数
  • 数据结构:树与二叉树
  • ARM基础概念 day51
  • easyExcel嵌套子集合导出Excel
  • 2025第十六届蓝桥杯大赛青少组省赛C++真题(初级组和中级组)
  • MCU的设计原理
  • SNMP入门教程:Windows下编译
  • Linux811 YUM;SHELL:if else fi,for
  • 进程线程切换的区别
  • 【k近邻】 K-Nearest Neighbors算法k值的选择
  • 第4节 大模型推理内存与计算优化
  • 【FreeRTOS】任务间通讯6: 任务通知- Task Notification