MagicaSakura是B站的主题框架,可以不重启app进行主题的切换,但是这里的多主题只是颜色的切换。本文演示一下MagicaSakura的用法。
- step1
添加gradle dependency
compile 'com.bilibili:magicasakura:0.1.5@aar'
- step2
Application需要实现ThemeUtils.Switcher接口,该接口有两个方法来给提供切换的颜色。
public interface switchColor {
@ColorInt int replaceColorById(Context context, @ColorRes int colorId);
@ColorInt int replaceColor(Context context, @ColorInt int color);
}
然后设置Switcher
public class MyApplication extends Application implements ThemeUtils.switchColor{
@Override
public void onCreate() {
super.onCreate();
ThemeUtils.setSwitchColor(this);
}
@Override
public int replaceColorById(Context context, @ColorRes int colorId) {
return xx;//将colorId 替换的颜色resId
}
@Override
public int replaceColor(Context context, @ColorInt int originColor) {
return xx;//将originColor替换的颜色resId
}
}
该库提供一系列的基于原生android控件的TintXX控件,可以满足一般的开发需求。
以下为demo布局文件
<?xml version="1.0" encoding="utf-8"?>
<com.bilibili.magicasakura.widgets.TintLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<com.bilibili.magicasakura.widgets.TintToolbar
android:id="@+id/common_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@android:color/white"
android:orientation="vertical">
<com.bilibili.magicasakura.widgets.TintButton
android:id="@+id/btn_red"
android:text="red"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
<com.bilibili.magicasakura.widgets.TintButton
android:id="@+id/btn_yellow"
android:text="yellow"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.bilibili.magicasakura.widgets.TintButton
android:id="@+id/btn_green"
android:text="green"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.bilibili.magicasakura.widgets.TintButton
android:id="@+id/btn_blue"
android:text="blue"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.bilibili.magicasakura.widgets.TintButton
android:id="@+id/btn_brown"
android:text="brown"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</com.bilibili.magicasakura.widgets.TintLinearLayout>
MainActvity代码
按不同的按钮,就会将主题对应的字符串持久化,然后通过EventBus通知activity换肤,每一次换肤,只需调用ThemeUtils.refreshUI(Context context, ExtraRefreshable extraRefreshable),然后遍历View,在Tint控件中回调switchColor的方法。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.common_toolbar);
initToolBar(toolbar);
EventBus.getDefault().register(this);
}
protected void initToolBar(Toolbar toolbar) {
if (toolbar == null) return;
toolbar.setTitle(getString(R.string.app_name));
setSupportActionBar(toolbar);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public void onClick(View view){
switch (view.getId()){
case R.id.btn_red:
ThemeSpUtils.setTheme(this,ThemeSpUtils.THEME_RED);
break;
case R.id.btn_blue:
ThemeSpUtils.setTheme(this,ThemeSpUtils.THEME_BLUE);
break;
case R.id.btn_brown:
ThemeSpUtils.setTheme(this,ThemeSpUtils.THEME_BROWN);
break;
case R.id.btn_green:
ThemeSpUtils.setTheme(this,ThemeSpUtils.THEME_GREEN);
break;
case R.id.btn_yellow:
ThemeSpUtils.setTheme(this,ThemeSpUtils.THEME_YELLOW);
break;
}
EventBus.getDefault().post("theme_change");
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(Object event) {
if (event.toString().equals("theme_change")) {
ThemeUtils.refreshUI(this, new ThemeUtils.ExtraRefreshable() {
@Override
public void refreshGlobal(Activity activity) {
if (Build.VERSION.SDK_INT >= 21) {
ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(null, null, ThemeUtils.getThemeAttrColor(MainActivity.this, android.R.attr.colorPrimary));
setTaskDescription(taskDescription);
getWindow().setStatusBarColor(ThemeUtils.getColorById(MainActivity.this, R.color.colorPrimaryDark));
}
}
@Override
public void refreshSpecificView(View view) {
}
});
}
}
}
效果