Android 多颜色主题框架MagicaSakura使用

MagicaSakura的github地址

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) {

                }
            });
        }
    }
}

效果

这里写图片描述 这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值