背景
目前很多app都具有換膚功能,用戶可以根據(jù)需要切換不同的皮膚,為使我們的App支持換膚功能,給用戶提供更好的體驗(yàn),在這里對(duì)換膚原理進(jìn)行研究總結(jié),并選擇一個(gè)合適的換膚解決方案。
換膚介紹
App換膚主要涉及的有頁面中文字的顏色、控件的背景顏色、一些圖片資源和主題顏色等資源。
為了實(shí)現(xiàn)換膚資源不與原項(xiàng)目混淆,盡量降低風(fēng)險(xiǎn),可以將這些資源封裝在一個(gè)獨(dú)立的Apk資源文件中。在App運(yùn)行時(shí),主程序動(dòng)態(tài)的從Apk皮膚包中讀取相應(yīng)的資源,無需Acitvity重啟即可實(shí)現(xiàn)皮膚的實(shí)時(shí)更換,皮膚包與原安裝包相分離,從而實(shí)現(xiàn)插件式換膚。
換膚原理
1. 如何加載皮膚資源文件
使用插件式換膚,皮膚資源肯定不會(huì)在被封裝到主工程中,要怎么加載外部的皮膚資源呢?
先看下 Apk 的打包流程

這里流程中,有兩個(gè)關(guān)鍵點(diǎn)
1.R文件的生成
R文件是一個(gè)Java文件,通過R文件我們就可以找到對(duì)應(yīng)的資源。R文件就像一張映射表,幫助我們找到資源文件。
2.資源文件的打包生成
資源文件經(jīng)過壓縮打包,生成 resources 文件,通過R文件找到里面保存的對(duì)映的資源文件。在 App 內(nèi)部,我們一般通過下面代碼,獲取資源:
context.getResource.getString(R.string.hello);`
context.getResource.getColor(R.color.black);`
context.getResource.getDrawable(R.drawable.splash);`
這個(gè)時(shí)獲取 App 內(nèi)部的資源,能我們家在皮膚資源什么思路嗎?加載外部資源的 Resources 能通過類似的思路嗎? 我們查看下 Resources 類的源碼,發(fā)現(xiàn) Resources 的構(gòu)造函數(shù)
public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
this (assets, metrics, config, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
}
這里關(guān)鍵是第一個(gè)參數(shù)如何獲取,第二和第三個(gè)參數(shù)可以通過 Activity 獲取到。我們?cè)偃タ聪?AssetManager 的代碼,同時(shí)會(huì)發(fā)現(xiàn)下面的這個(gè)
/**
* 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 可以加載一個(gè)zip 格式的壓縮包,而 Apk 文件不就是一個(gè) 壓縮包嗎。我們通過反射的方法,拿到 AssetManager,加載 Apk 內(nèi)部的資源,獲取到 Resources 對(duì)象,這樣再想辦法,把 R文件里面保存的ID獲取到,這樣既可以拿到對(duì)應(yīng)的資源文件了。理論上我們的思路時(shí)成立的。
我們看下,如何通過代碼獲取 Resources 對(duì)象。
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. 如何標(biāo)記需要換膚的View
找到資源文件之后,我們要接著標(biāo)記需要換膚的 View 。
找到需要換膚的 View
怎么尋找哪些是我們要關(guān)注的 View 呢? 我們還是重 View 的創(chuàng)建時(shí)機(jī)尋找機(jī)會(huì)。我們添加一個(gè)布局文件時(shí),會(huì)使用 LayoutInflater的 Inflater方法,我們看下這個(gè)方法是怎么講一個(gè)View添加到Activity 中的。
LayoutInflater 中有個(gè)接口
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);
}
根據(jù)這里的注釋描述,我們可以自己實(shí)現(xiàn)這個(gè)接口,在 onCreateView 方法中選擇我們需要標(biāo)記的View,根據(jù) AttributeSet 值,過濾不需要關(guān)注的View。
標(biāo)記 View 與對(duì)應(yīng)的資源
我們?cè)?View 創(chuàng)建時(shí),通過過濾 Attribute 屬性,找到我們要標(biāo)記的 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. 如何做到及時(shí)更新UI
由于我們把需要更新的View 以及屬性值都保存起來了,更新的時(shí)候只要把他們?nèi)〕鰜肀闅v一遍即可。
@Override
public void onThemeUpdate() {
if (!isResponseOnSkinChanging){
return ;
}
mSkinInflaterFactory.applySkin();
}
//applySkin 的具體實(shí)現(xiàn)
public void applySkin(){
if (ListUtils.isEmpty(mSkinItems)){
return ;
}
for (SkinItem si : mSkinItems){
if (si.view == null ){
continue ;
}
si.apply();
}
}
4. 如何制作皮膚包
皮膚包制作相對(duì)簡單
1.創(chuàng)建獨(dú)立工程 model,包名任意。
2.添加資源文件到 model 中,不需要 java 代碼
3.運(yùn)行 build.gradle 腳本,打包命令,生成apk文件,修改名稱為 xxx.skin 皮膚包即可。
基于ThemeSkinning的換膚框架
根據(jù)以上換膚原理,在github上面選擇了一個(gè)第三方開源框架ThemeSkinning,具體使用方法如下:
1. 集成步驟:
添加依賴 compile 'com.solid.skin:skinlibrary:1.3.1'使項(xiàng)目中的Application繼承于SkinBaseApplication使項(xiàng)目中的Activity繼承于SkinBaseActivity,如果使用了Fragment則繼承于SkinBaseFragment在需要換膚的根布局上添加 xmlns:skin="http://schemas.android.com/android/skin" ,然后在需要換膚的View上加上 skin:enable="true"新建一個(gè)項(xiàng)目模塊(只包含有資源文件),其中包含的資源文件的name一定要和原項(xiàng)目中有換膚需求的View所使用的資源name一致。打包皮膚文件,放入assets中的skin目錄下(skin目錄是自己新建的)調(diào)用換膚方法:
- 在 assets/skin 文件夾中的皮膚

2.換膚屬性的擴(kuò)展
該開源庫默認(rèn)僅支持View的textColor和background兩個(gè)屬性的換膚,如果需要對(duì)其他屬性進(jìn)行換膚,那么就需要去自定義了。
那么如何自定義呢?看下面這個(gè)例子:
ImageView大家應(yīng)該都用過吧。它的src屬性就是定義圖片資源引用,
新建一個(gè)ImageSrcAttr繼承于 SkinAttr,然后重寫apply方法。apply方法在換膚的時(shí)候就會(huì)被調(diào)用,代碼的詳細(xì)實(shí)現(xiàn):
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是用來獲取皮膚包里的資源,這里設(shè)置color或者drawable一定要使用本工具類。
當(dāng)上面的工作完成之后,就到我們自己的Application的onCreate方法中加入 SkinConfig.addSupportAttr("src", new ImageSrcAttr());我們就可以正常使用了src屬性了。
此外,對(duì)于動(dòng)態(tài)創(chuàng)建的view,我們需要?jiǎng)討B(tài)添加支持,調(diào)用
dynamicAddSkinEnableView(View view, String attrName, int attrValueResId)方法添加支持。
3.其他一些重要的api
1. SkinConfig.isDefaultSkin(context):判斷當(dāng)前皮膚是否是默認(rèn)皮膚
2. SkinManager.getInstance().restoreDefaultTheme(): 重置默認(rèn)皮膚