Android Resource.getLayout函数分析-(基于10.0.0)

在分析LayoutInflater的时候有一处是需要根据资源ID生成一个XmlResourceParser对象的,这个是通过调用Resource类的getLayout函数实现的,该函数的代码如下

/frameworks/base/core/java/android/content/res/Resources.java

    public XmlResourceParser getLayout(@LayoutRes int id) throws NotFoundException {
        return loadXmlResourceParser(id, "layout");
    }

可以看出它内部是调用loadXmlResourceParser并传入“layou”来实现,看来loadXmlResourceParser函数是用来解析多种类别xml的,在Resources类中 getLayout、getAnimation、getXml都是在内部直接调用loadXmlResourceParser的,我们继续往下看

/frameworks/base/core/java/android/content/res/Resources.java

    XmlResourceParser loadXmlResourceParser(@AnyRes int id, @NonNull String type) throws NotFoundException {
        // 获取一个TypedValue对象,这里使用了一个单Item的对象池,减小对象的申请和释放
        final TypedValue value = obtainTempTypedValue();
        try {
            final ResourcesImpl impl = mResourcesImpl;

            // 根据资源ID获取对应的文件路径,然后把相关信息存储在value里面
            impl.getValue(id, value, true);
            if (value.type == TypedValue.TYPE_STRING) {
                // 根据文件路径创建XML解释器
                return impl.loadXmlResourceParser(value.string.toString(), id,
                        value.assetCookie, type);
            }
            throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id)
                    + " type #0x" + Integer.toHexString(value.type) + " is not valid");
        } finally {
            // 将TypeValue对象归还给对象池
            releaseTempTypedValue(value);
        }
    }

先调用ResourcesImpl类的getValue函数获取XML文件的路径

/frameworks/base/core/java/android/content/res/ResourcesImpl.java

 void getValue(@AnyRes int id, TypedValue outValue, boolean resolveRefs)
            throws NotFoundException {
        boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs);
        if (found) {
            return;
        }
        throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id));
    }

在getValue中实际上调用了AssetManager.getResourceValue(); AssetManager是Android中的资源管理类,下一篇文章我们分析它

在通过AssetManager获取到资源ID对应的文件路径后调用ResourcesImpl类中的loadXmlResourceParser函数来获取XMLResourceParser

/frameworks/base/core/java/android/content/res/ResourcesImpl.java

XmlResourceParser loadXmlResourceParser(@NonNull String file, @AnyRes int id, int assetCookie,
            @NonNull String type)
            throws NotFoundException {
        if (id != 0) {
            try {
                synchronized (mCachedXmlBlocks) {
                    // 在缓存中找已经缓存的XmlBlock,XmlBlock是一个编译过的xml文件
                    final int[] cachedXmlBlockCookies = mCachedXmlBlockCookies;
                    final String[] cachedXmlBlockFiles = mCachedXmlBlockFiles;
                    final XmlBlock[] cachedXmlBlocks = mCachedXmlBlocks;
                    // First see if this block is in our cache.
                    final int num = cachedXmlBlockFiles.length;
                    for (int i = 0; i < num; i++) {
                        if (cachedXmlBlockCookies[i] == assetCookie && cachedXmlBlockFiles[i] != null
                                && cachedXmlBlockFiles[i].equals(file)) {
                            return cachedXmlBlocks[i].newParser(id);
                        }
                    }
                    // 没有已缓存的XmlBlock,就新建一个,并返回
                    // Not in the cache, create a new block and put it at
                    // the next slot in the cache.
                    final XmlBlock block = mAssets.openXmlBlockAsset(assetCookie, file);
                    if (block != null) {
                        final int pos = (mLastCachedXmlBlockIndex + 1) % num;
                        mLastCachedXmlBlockIndex = pos;
                        final XmlBlock oldBlock = cachedXmlBlocks[pos];
                        if (oldBlock != null) {
                            oldBlock.close();
                        }
                        cachedXmlBlockCookies[pos] = assetCookie;
                        cachedXmlBlockFiles[pos] = file;
                        cachedXmlBlocks[pos] = block;
                        return block.newParser(id);
                    }
                }
            } catch (Exception e) {
                final NotFoundException rnf = new NotFoundException("File " + file
                        + " from xml type " + type + " resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
        }

        throw new NotFoundException("File " + file + " from xml type " + type + " resource ID #0x"
                + Integer.toHexString(id));
    }

到此我们就知道XmlResourceParser是怎么来的了

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值