1、背景
看了点东西,写点东西
2、资源加载
总所周知,Android资源加载通过Resource和AssetManager,比如一个textview的setText过程(api 21):
1、textView.setText(R.string.str);
//调用Resrouce的getText()
2、public final void setText(int resid) {
setText(getContext().getResources().getText(resid));
}
//调用AssetManager的getResourceText()方法
3、public CharSequence getText(int id) throws NotFoundException {
CharSequence res = mAssets.getResourceText(id);
if (res != null) {
return res;
}
throw new NotFoundException("String resource ID #0x"
+ Integer.toHexString(id));
}
//AssetManager的getResourceText()具体实现
4、final CharSequence getResourceText(int ident) {
synchronized (this) {
TypedValue tmpValue = mValue;
int block = loadResourceValue(ident, (short) 0, tmpValue, true);
if (block >= 0) {
if (tmpValue.type == TypedValue.TYPE_STRING) {
return mStringBlocks[block].get(tmpValue.data);
}
return tmpValue.coerceToString();
}
}
return null;
}
而AssetManager提供了一个方法addAssetPath()来加载外部资源
/**
* Add an additional set of assets to the asset manager. This can be
* either a directory or ZIP file. Not for use by applications. Returns
* the cookie of the added asset, or 0 on failure.
* {@hide}
*/
public final int addAssetPath(String path) {
synchronized (this) {
int res = addAssetPathNative(path);
makeStringBlocks(mSt