Android之基于AssetManager实现换肤方案
生活随笔
收集整理的這篇文章主要介紹了
Android之基于AssetManager实现换肤方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AssetManager的addAssetPath負責將另一個apk的資源文件加載進當前應用,這里由于是api隱藏方法,采用反射方式調用。
查看addAssetPath方法注釋,允許傳遞的路徑為資源目錄或者zip文件。/**
* 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}
*/通過實例化的AssetManager對象,生成插件包對應的Resources對象,拿到該對象即可操作插件包的相關資源文件。private Resources pluginRes;//插件Resource對象
private String pluginApkPackageName;//插件Apk的包名
public ResPluginOnAssetManagerPattern initManager(Context curContext, String pluginApkPath) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, PackageManager.NameNotFoundException, ClassNotFoundException {
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.invoke(assetManager, pluginApkPath);
Resources curAppRes = curContext.getResources();
pluginRes = new Resources(assetManager, curAppRes.getDisplayMetrics(), curAppRes.getConfiguration());
pluginApkPackageName = UtilsSystem.getPackageNameThroughApkPath(curContext, pluginApkPath);
return this;
}想要獲取插件包的資源,可以通過以下方式引用(這里僅給出string以及drawable的調用方式,其他資源類似):/**
* 獲取ResID
*
* @param resName
* @param resType
* @return
*/
private int getResId(String resName, String resType) {
if (pluginRes != null && !UtilsString.isEmptyBaseTrim(pluginApkPackageName)) {
return pluginRes.getIdentifier(resName, resType, pluginApkPackageName);
}
return -1;
}
@Override
public String getString(String resName) {
return pluginRes.getString(getResId(resName, "string"));
}
@Override
public Drawable getDrawable(String resName) {
return pluginRes.getDrawable(getResId(resName, "drawable"));
}
源碼地址:https://github.com/xiaoxuan948/AndroidUnityLab/tree/master/unity_base_dev_helper/src/main/java/com/coca/unity_base_dev_helper/plugin
來自為知筆記(Wiz)
查看addAssetPath方法注釋,允許傳遞的路徑為資源目錄或者zip文件。/**
* 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}
*/通過實例化的AssetManager對象,生成插件包對應的Resources對象,拿到該對象即可操作插件包的相關資源文件。private Resources pluginRes;//插件Resource對象
private String pluginApkPackageName;//插件Apk的包名
public ResPluginOnAssetManagerPattern initManager(Context curContext, String pluginApkPath) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, PackageManager.NameNotFoundException, ClassNotFoundException {
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.invoke(assetManager, pluginApkPath);
Resources curAppRes = curContext.getResources();
pluginRes = new Resources(assetManager, curAppRes.getDisplayMetrics(), curAppRes.getConfiguration());
pluginApkPackageName = UtilsSystem.getPackageNameThroughApkPath(curContext, pluginApkPath);
return this;
}想要獲取插件包的資源,可以通過以下方式引用(這里僅給出string以及drawable的調用方式,其他資源類似):/**
* 獲取ResID
*
* @param resName
* @param resType
* @return
*/
private int getResId(String resName, String resType) {
if (pluginRes != null && !UtilsString.isEmptyBaseTrim(pluginApkPackageName)) {
return pluginRes.getIdentifier(resName, resType, pluginApkPackageName);
}
return -1;
}
@Override
public String getString(String resName) {
return pluginRes.getString(getResId(resName, "string"));
}
@Override
public Drawable getDrawable(String resName) {
return pluginRes.getDrawable(getResId(resName, "drawable"));
}
源碼地址:https://github.com/xiaoxuan948/AndroidUnityLab/tree/master/unity_base_dev_helper/src/main/java/com/coca/unity_base_dev_helper/plugin
來自為知筆記(Wiz)
轉載于:https://www.cnblogs.com/linux007/p/5806308.html
總結
以上是生活随笔為你收集整理的Android之基于AssetManager实现换肤方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 学习笔记十九 django
- 下一篇: 系统聚类(hierarchical cl