前陣子開源出了自己的mp3剪切器項目即彪,在鴻洋大神公眾號的宣傳下引來眾人關注。本著持續(xù)維護的心供鸠,計劃中項目要實現(xiàn)一個簡單的應用內(nèi)換膚功能豫柬。
經(jīng)過調研發(fā)現(xiàn)一款很用心的開源的換膚框架Android-skin-support。它支持應用內(nèi)換膚桥温、插件式換膚和自定義加載的方式引矩。在這里只介紹一下應用內(nèi)換膚方式,希望能夠拋磚引玉侵浸,詳細的介紹可以參考它的github旺韭。先來看看效果Gif圖。
效果圖
[圖片上傳失敗...(image-e2132e-1516800042573)]
使用方法
- 先來添加jcenter依賴庫,
compile 'skin.support:skin-support:2.2.3'
compile 'skin.support:skin-support-design:2.2.3'
- Application類中增加初始化sdk代碼
SkinCompatManager.withoutActivity(this) // 基礎控件換膚初始化
.addInflater(new SkinMaterialViewInflater()) // material design 控件換膚初始化[可選]
.setSkinStatusBarColorEnable(false) // 關閉狀態(tài)欄換膚掏觉,默認打開[可選]
.setSkinWindowBackgroundEnable(false) // 關閉windowBackground換膚区端,默認打開[可選]
.loadSkin();
- 設置需要換膚的控件,我這里主要為toolbar換膚澳腹。
<skin.support.widget.SkinCompatToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
- 準備換膚的資源织盼,我這里用的主題顏色以及顏色的名稱,放入arrays.xml中。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 默認主題顏色 -->
<color name="theme_color">#000000</color>
<!-- theme array -->
<color name="theme_color_redbase">#FB5B81</color>
<color name="theme_color_blue">#0077D9</color>
<color name="theme_color_bluelight">#03A9F4</color>
<color name="theme_color_balck">#000000</color>
<color name="theme_color_teal">#009688</color>
<color name="theme_color_brown">#795548</color>
<color name="theme_color_green">#4CAF50</color>
<color name="theme_color_red">#FF5252</color>
<!-- 主題顏色酱塔, 用于換膚沥邻, 例如:@color/xx_redbase -->
<string-array name="theme_colors">
<item>@color/theme_color_redbase</item>
<item>@color/theme_color_blue</item>
<item>@color/theme_color_bluelight</item>
<item>@color/theme_color_balck</item>
<item>@color/theme_color_teal</item>
<item>@color/theme_color_brown</item>
<item>@color/theme_color_green</item>
<item>@color/theme_color_red</item>
</string-array>
<!-- 主題名稱, 用于換膚延旧, 例如:@color/xx_redbase -->
<string-array name="theme_names">
<item>redbase</item>
<item>blue</item>
<item>bluelight</item>
<item>balck</item>
<item>teal</item>
<item>brown</item>
<item>green</item>
<item>red</item>
</string-array>
</resources>
- 換膚api調用, 這里傳入了選中的主題顏色名稱谋国,以及換膚的加載方式(應用內(nèi)換膚)
SkinCompatManager.getInstance().loadSkin(
mThemeColorList.get(mThemeColorAdapter.getPosition()).getName(),
null,SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
問題
- 如何定位需要換膚的view,如何進行換膚操作迁沫?
- 如何遍歷每個activity中的需要換膚的view?
淺析
- 定位需要被換膚view的思路 主要通過
LayoutInflaterFactory
獲取到當前activity中所有的組件芦瘾,通過過濾定位到需要換膚的view〖可以參考鴻洋的這篇文章 Android 探究 LayoutInflater setFactory近弟。
SkinCompatDelegate
類實現(xiàn)了LayoutInflaterFactory
接口,并實現(xiàn)了它的createView()
方法, 這里看SkinCompatViewInflater類的createView()
代碼如下:
case "View":
view = new SkinCompatView(context, attrs);
break;
case "LinearLayout":
view = new SkinCompatLinearLayout(context, attrs);
break;
case "RelativeLayout":
view = new SkinCompatRelativeLayout(context, attrs);
break;
case "FrameLayout":
view = new SkinCompatFrameLayout(context, attrs);
break;
case "TextView":
view = new SkinCompatTextView(context, attrs);
break;
case "ImageView":
view = new SkinCompatImageView(context, attrs);
break;
可以看到在createView
遍歷后將其替換了對應的Skin開頭的對應的類挺智。而Skin開頭的類是什么呢祷愉, 來看SkinCompatView
類。
public class SkinCompatView extends View implements SkinCompatSupportable {
private SkinCompatBackgroundHelper mBackgroundTintHelper;
...
@Override
public void setBackgroundResource(int resId) {
super.setBackgroundResource(resId);
if (mBackgroundTintHelper != null) {
mBackgroundTintHelper.onSetBackgroundResource(resId);
}
}
@Override
public void applySkin() {
if (mBackgroundTintHelper != null) {
mBackgroundTintHelper.applySkin();
}
}
}
可以看到它實現(xiàn)了SkinCompatSupportable
接口以及其applySkin
方法,該方法就是換膚的實現(xiàn)方法了二鳄。
接著來看SkinCompatDelegate.onCreateView()
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
View view = createView(parent, name, context, attrs);
if (view == null) {
return null;
}
if (view instanceof SkinCompatSupportable) {
mSkinHelpers.add(new WeakReference<>((SkinCompatSupportable) view));
}
return view;
}
上文已經(jīng)將view轉換為Skin開頭的View(實現(xiàn)了SkinCompatSupportable
的View)可以看到將其緩存到了mSkinHelpers數(shù)組中赴涵。再看SkinCompatDelegate.applySkin()
public void applySkin() {
if (mSkinHelpers != null && !mSkinHelpers.isEmpty()) {
for (WeakReference ref : mSkinHelpers) {
if (ref != null && ref.get() != null) {
((SkinCompatSupportable) ref.get()).applySkin();
}
}
}
}
該方法就是換膚的操作了,遍歷緩存中所有的view订讼,執(zhí)行其換膚方法applySkin
髓窜。這里我們再看一下applySkin里面的實現(xiàn),來看SkinBuildInLoader.getTargetResourceEntryName()
欺殿。
@Override
public String getTargetResourceEntryName(Context context, String skinName, int resId) {
return context.getResources().getResourceEntryName(resId) + "_" + skinName;
}
- 第二個問題寄纵,可以通過BaseActivity的方式來達到目的,但是有更好的方式脖苏。來看
SkinActivityLifecycle
的構造方法application.registerActivityLifecycleCallbacks(this);
程拭。注冊之后沒個activity的聲明周期回調都會在該類中回調到, 如onActivityCreated()
。
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (isContextSkinEnable(activity)) {
installLayoutFactory(activity);
updateStatusBarColor(activity);
updateWindowBackground(activity);
if (activity instanceof SkinCompatSupportable) {
((SkinCompatSupportable) activity).applySkin();
}
}
}
在每個activity創(chuàng)建后都會對應執(zhí)行到installLayoutFactory(activity)
方法棍潘,從而達到對每個activity 換膚view做收集換膚的目的恃鞋。
到此,基本原理亦歉,都闡述完了山宾,具體不懂的更詳細的還要結合源碼來看,如下: