背景
目前很多app都具有換膚功能,用戶可以根據需要切換不同的皮膚,為使我們的App支持換膚功能,給用戶提供更好的體驗,在這里對換膚原理進行研究總結,並選擇一個合適的換膚解決方案。
換膚介紹
App換膚主要涉及的有頁面中文字的顏色、控件的背景顏色、一些圖片資源和主題顏色等資源。
為了實現換膚資源不與原項目混淆,盡量降低風險,可以將這些資源封裝在一個獨立的Apk資源文件中。在App運行時,主程序動態的從Apk皮膚包中讀取相應的資源,無需Acitvity重啟即可實現皮膚的實時更換,皮膚包與原安裝包相分離,從而實現插件式換膚。
換膚原理
1. 如何加載皮膚資源文件
使用插件式換膚,皮膚資源肯定不會在被封裝到主工程中,要怎么加載外部的皮膚資源呢?
先看下 Apk 的打包流程
這里流程中,有兩個關鍵點
1.R文件的生成
R文件是一個Java文件,通過R文件我們就可以找到對應的資源。R文件就像一張映射表,幫助我們找到資源文件。
2.資源文件的打包生成
資源文件經過壓縮打包,生成 resources 文件,通過R文件找到里面保存的對映的資源文件。在 App 內部,我們一般通過下面代碼,獲取資源:
context.getResource.getString(R.string.hello); context.getResource.getColor(R.color.black); context.getResource.getDrawable(R.drawable.splash);
這個時獲取 App 內部的資源,能我們家在皮膚資源什么思路嗎?加載外部資源的 Resources 能通過類似的思路嗎?
我們查看下 Resources 類的源碼,發現 Resources 的構造函數
public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) { this(assets, metrics, config, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO); }
這里關鍵是第一個參數如何獲取,第二和第三個參數可以通過 Activity 獲取到。我們再去看下 AssetManager 的代碼,同時會發現下面的這個
/** * 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; } }
AssetManager 可以加載一個zip 格式的壓縮包,而 Apk 文件不就是一個 壓縮包嗎。我們通過反射的方法,拿到 AssetManager,加載 Apk 內部的資源,獲取到 Resources 對象,這樣再想辦法,把 R文件里面保存的ID獲取到,這樣既可以拿到對應的資源文件了。理論上我們的思路時成立的。
我們看下,如何通過代碼獲取 Resources 對象。
AssetManager assetManager = AssetManager.class.newInstance(); Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class); addAssetPath.invoke(assetManager, skinPkgPath); Resources superRes = context.getResources(); Resources skinResource = new Resources(assetManager,superRes.getDisplayMetrics(),superRes.getConfiguration());
2. 如何標記需要換膚的View
找到資源文件之后,我們要接着標記需要換膚的 View 。
找到需要換膚的 View
怎么尋找哪些是我們要關注的 View 呢? 我們還是重 View 的創建時機尋找機會。我們添加一個布局文件時,會使用 LayoutInflater的 Inflater方法,我們看下這個方法是怎么講一個View添加到Activity 中的。
LayoutInflater 中有個接口
public interface Factory { /** * Hook you can supply that is called when inflating from a LayoutInflater. * You can use this to customize the tag names available in your XML * layout files. * * <p> * Note that it is good practice to prefix these custom names with your * package (i.e., com.coolcompany.apps) to avoid conflicts with system * names. * * @param name Tag name to be inflated. * @param context The context the view is being created in. * @param attrs Inflation attributes as specified in XML file. * * @return View Newly created view. Return null for the default * behavior. */ public View onCreateView(String name, Context context, AttributeSet attrs); }
根據這里的注釋描述,我們可以自己實現這個接口,在 onCreateView 方法中選擇我們需要標記的View,根據 AttributeSet 值,過濾不需要關注的View。
標記 View 與對應的資源
我們在 View 創建時,通過過濾 Attribute 屬性,找到我們要標記的 View ,下面我們就把這些View的屬性記下來
for (int i = 0; i < attrs.getAttributeCount(); i++){ String attrName = attrs.getAttributeName(i); String attrValue = attrs.getAttributeValue(i); if(!AttrFactory.isSupportedAttr(attrName)){ continue; } if(attrValue.startsWith("@")){ try { int id = Integer.parseInt(attrValue.substring(1)); String entryName = context.getResources().getResourceEntryName(id); String typeName = context.getResources().getResourceTypeName(id); SkinAttr mSkinAttr = AttrFactory.get(attrName, id, entryName, typeName); if (mSkinAttr != null) { viewAttrs.add(mSkinAttr); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } } }
然后把這些 View 和屬性值,一起封裝保存起來
if(!ListUtils.isEmpty(viewAttrs)){ SkinItem skinItem = new SkinItem(); skinItem.view = view; skinItem.attrs = viewAttrs; mSkinItems.add(skinItem); if(SkinManager.getInstance().isExternalSkin()){ skinItem.apply(); } }
3. 如何做到及時更新UI
由於我們把需要更新的View 以及屬性值都保存起來了,更新的時候只要把他們取出來遍歷一遍即可。
@Override public void onThemeUpdate() { if(!isResponseOnSkinChanging){ return; } mSkinInflaterFactory.applySkin(); } //applySkin 的具體實現 public void applySkin(){ if(ListUtils.isEmpty(mSkinItems)){ return; } for(SkinItem si : mSkinItems){ if(si.view == null){ continue; } si.apply(); } }
4. 如何制作皮膚包
皮膚包制作相對簡單
1.創建獨立工程 model,包名任意。
2.添加資源文件到 model 中,不需要 java 代碼
3.運行 build.gradle 腳本,打包命令,生成apk文件,修改名稱為 xxx.skin 皮膚包即可。
基於ThemeSkinning的換膚框架
根據以上換膚原理,在github上面選擇了一個第三方開源框架ThemeSkinning,具體使用方法如下:
1. 集成步驟:
1) 添加依賴 compile 'com.solid.skin:skinlibrary:1.3.1'
2) 使項目中的Application繼承於SkinBaseApplication
3) 使項目中的Activity繼承於SkinBaseActivity,如果使用了Fragment則繼承於SkinBaseFragment
4) 在需要換膚的根布局上添加 xmlns:skin="http://schemas.android.com/android/skin" ,然后在需要換膚的View上加上 skin:enable="true"
5) 新建一個項目模塊(只包含有資源文件),其中包含的資源文件的name一定要和原項目中有換膚需求的View所使用的資源name一致。
6) 打包皮膚文件,放入assets中的skin目錄下(skin目錄是自己新建的)
7) 調用換膚方法:
- 在 assets/skin 文件夾中的皮膚
2.換膚屬性的擴展
該開源庫默認僅支持View的textColor和background兩個屬性的換膚,如果需要對其他屬性進行換膚,那么就需要去自定義了。
那么如何自定義呢?看下面這個例子:
ImageView大家應該都用過吧。它的src屬性就是定義圖片資源引用,
新建一個ImageSrcAttr繼承於 SkinAttr,然后重寫apply方法。apply方法在換膚的時候就會被調用,代碼的詳細實現:
public class ImageSrcAttr extends SkinAttr {
@Override
public void apply(View view) {
if (view instanceof ImageView) {
ImageView iv = (ImageView) view;
if (RES_TYPE_NAME_DRAWABLE.equals(attrValueTypeName)) {
Drawable drawable = SkinResourcesUtils.getDrawable(attrValueRefId);
iv.setImageDrawable(drawable);
}
} }
}
注:attrValueRefId:就是資源id。SkinResourcesUtils是用來獲取皮膚包里的資源,這里設置color或者drawable一定要使用本工具類。
當上面的工作完成之后,就到我們自己的Application的onCreate方法中加入 SkinConfig.addSupportAttr("src", new ImageSrcAttr());我們就可以正常使用了src屬性了。
此外,對於動態創建的view,我們需要動態添加支持,調用
dynamicAddSkinEnableView(View view, String attrName, int attrValueResId)方法添加支持。
3.其他一些重要的api
- SkinConfig.isDefaultSkin(context):判斷當前皮膚是否是默認皮膚
- SkinManager.getInstance().restoreDefaultTheme(): 重置默認皮膚