在很多Android應(yīng)用上必孤,都有資源動(dòng)態(tài)加載的功能猾骡,比如更換主題皮膚,替換聊天界面背景圖片等敷搪。
以微信為例兴想,當(dāng)用戶選擇模板時(shí),會(huì)先從網(wǎng)絡(luò)上下載相應(yīng)的圖片資源赡勘,然后再替換為聊天界面的背景圖片嫂便。我們知道,應(yīng)用中的資源文件狮含,包括圖片顽悼,xml文件等,都是在編譯的時(shí)候打包好的几迄,那怎樣才能動(dòng)態(tài)加載資源呢蔚龙?
其實(shí)有一個(gè)比較簡(jiǎn)單的思路,將需要替換的資源文件打包在一個(gè)apk文件中映胁,動(dòng)態(tài)下發(fā)到本地木羹,然后通過重新構(gòu)造Resources對(duì)象訪問apk中的資源,進(jìn)行本地的動(dòng)態(tài)替換。主要有以下幾個(gè)步驟:
一坑填、指定資源文件加載路徑
Android應(yīng)用中的資源是通過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(mStringBlocks);
return res;
}
}
很顯然這是個(gè)隱藏的API脐瑰,所以需要通過反射來調(diào)用妖枚。
private AssetManager createAssetManager(String skinFilePath) {
try {
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.invoke(assetManager, skinFilePath);
return assetManager;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
二、構(gòu)造Resources對(duì)象
private Resources createResources(Context context, AssetManager assetManager) {
Resources superRes = context.getResources();
Resources resources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
return resources;
}
有了Resource對(duì)象苍在,就可以訪問指定路徑的資源文件绝页,進(jìn)行動(dòng)態(tài)替換,示例如下:
public class SkinManager {
private Resources mResources;
/**
* 獲取APK資源
* @param context 上下文
* @param apkPath APK路徑
*/
public void loadSkinRes(Context context, String skinFilePath) {
if (TextUtils.isEmpty(skinFilePath)) {
return ;
}
try {
AssetManager assetManager = createAssetManager(skinFilePath);
mResources = createResources(context, assetManager);
} catch (Exception e) {
e.printStackTrace();
}
}
private AssetManager createAssetManager(String skinFilePath) {
try {
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.invoke(assetManager, skinFilePath);
return assetManager;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private Resources createResources(Context context, AssetManager assetManager) {
Resources superRes = context.getResources();
Resources resources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
return resources;
}
public Resources getSkinResource() {
return mResources;
}
}
在進(jìn)入Activity的時(shí)候進(jìn)行檢查寂恬,如果有資源apk文件续誉,則通過新的Resources對(duì)象進(jìn)行資源獲取。
public class MainActivity extends Activity {
private Context mContext;
private ImageView mBgView;
private SkinManager mSkinManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBgView = (ImageView) findViewById(R.id.bg);
mContext = this;
mSkinManager = new SkinManager();
checkNewSkin();
}
private void checkNewSkin() {
String skinDir = "/mnt/sdcard/skin";
File file = new File(skinDir);
File[] skinFile = file.listFiles();
if (skinFile == null || skinFile.length == 0) {
return ;
}
mSkinManager.loadSkinRes(mContext, skinFile[0].getAbsolutePath());
if (mSkinManager.getSkinResource() != null) {
mBgView.setBackgroundDrawable(mSkinManager.getSkinResource().getDrawable(R.mipmap.skin));
}
}
}
這里是非常簡(jiǎn)單的處理初肉,將編譯好的資源apk文件push到本地sd卡直接加載酷鸦,正常情況下應(yīng)該是從網(wǎng)絡(luò)下載,根據(jù)不同的模板名稱進(jìn)行資源的動(dòng)態(tài)替換牙咏。