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

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.xdnf.cn/news/17510.html

相关文章:

  • Mini-Omni: Language Models Can Hear, Talk While Thinking in Streaming
  • openpnp - 顶部相机环形灯光DIY
  • HTTPS 协议原理 ——4种方案
  • 如何解决 JetBrains IntelliJ IDEA 2024.2 和 2025.2 新版本区域选择问题:key is invalid
  • VBA即用型代码手册:计算选择的单词数Count Words in Selection
  • 网络资源模板--基于Android Studio 实现的手绘板App
  • 第9节 大模型分布式推理核心挑战与解决方案
  • glide缓存策略和缓存命中
  • Godot ------ 平滑拖动01
  • GAI 与 Tesla 机器人的具体联动机制
  • 基于Spring Data Elasticsearch的分布式全文检索与集群性能优化实践指南
  • 飞算 JavaAI 智能进阶:从技术工具到金融科技开发范式的革新
  • 开博尔雷电5数据线:120Gbps“闪电传输”,以Intel硬核基因从容优化数字生活
  • 跨国智能制造场景下,如何选择更可靠的SD-WAN服务商?
  • 关系型数据库:原理、演进与生态全景——从理论基石到云原生的深度巡礼
  • 【MySQL✨】服务器安装 MySQL 及配置相关操作
  • 从零构建企业级K8S:高可用集群部署指南
  • TDengine IDMP 基本功能(2.数据建模)
  • 设备 “心电图” 系统专家 —— 一二三物联网智能监测方案,让故障预测精度大幅提升
  • MQTT:Java集成MQTT
  • 【LLM】OpenAI开源GPT级模型,120B及20B参数GPT-OSS
  • 调用springboot接口返回403,问题定位及总结
  • Java 大视界 -- Java 大数据机器学习模型在电商商品销量预测与库存精准管理中的应用(391)
  • 安装1panel之后如何通过nginx代理访问
  • 展锐平台(Android15)WLAN热点名称修改不生效问题分析
  • 【Docker实战】Spring Boot应用容器化
  • Chat2DB入门教程
  • JavaSE:入门
  • 【图像算法 - 11】基于深度学习 YOLO 与 ByteTrack 的目标检测与多目标跟踪系统(系统设计 + 算法实现 + 代码详解 + 扩展调优)
  • MySQL的隔离级别及MVCC原理解析