在分析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是怎么来的了