• Stars
    star
    3,552
  • Rank 12,488 (Top 0.3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MagicaSakura 是 Android 多主题框架。~ is an Android multi theme library which supporting both daily colorful theme and night theme.

cover

MagicaSakura

Download

MagicaSakura is an Android multi theme library which supporting both daily colorful theme and night theme.

Feature

  1. Support both the daily colorful theme and the night theme.
  1. Switch different theme without recreating activities.
  1. Provide TintXXX Widgets for adapting multi theme to be more convenient and fast.
  1. Just writie a drawable.xml or layout.xml can be automatically adapted to different theme styles.
  1. Offer backward-compatible versions of the Android system that can be used with 4.0.3 or higher.
  1. Support Vector Drawable with appcompat-v7.
  1. Easy to integrate to your app.

Demo

ScreenShot.gif

You can download the lastest sample apk from Google Play.

Gradle Dependency

compile 'com.bilibili:magicasakura:0.1.9-beta3@aar'

Maven Dependency

<dependency>
  <groupId>com.bilibili</groupId>
  <artifactId>magicasakura</artifactId>
  <version>0.1.9-beta3</version>
  <type>aar</type>
</dependency>

Usage

  • STEP1 :

Define your app global theme color variates in values/color.xml, like as:

<color name="theme_color_primary">#fb7299</color>
<color name="theme_color_primary_dark">#b85671</color>
<color name="theme_color_primary_trans">#99f0486c</color>

Must use these color variates in layout xml , color xml or drawable xml when these xml files need to be automatically adapted to different theme styles. If you use direct color value or other color variates, adapting different theme styles will be out of work.

  • STEP2 :

Implement ThemeUtils.switchColor interface in the app Applaction; You Define your own rules combining with the color variates(defining in Step 1) for switching different colors when choosing different themes.

public class MyApplication extends Application implements ThemeUtils.switchColor {
     @Override
     public void onCreate() {
         super.onCreate();
         //init
         ThemeUtils.setSwitchColor(this);
     }
     
     @Override
     public int replaceColorById(Context context, @ColorRes int colorId) {
       ...
       if(ThemeHelper.getThemeId(context) == "blue"){
           switch (colorId) {
             // define in Step 1
             case R.color.theme_color_primary:
               return R.color.blue;
               ...
           }
       ...
     }

     @Override
     public int replaceColor(Context context, @ColorInt int originColor) {
       if (ThemeHelper.isDefaultTheme(context)) {
           return originColor;
       }
       ...
     }
}
  • STEP3 :

The library provides a series of TintXXX widgets which including most common android widgets.

When some place in your app needs to adapter multi theme, you can use these TintXXX widgets combining with the color variates(defining in Step 1) or color xml(using the color variates) or drawable xml(the color variates) , then they will be auto adapting.

  • Drawable Xml TintXXX widgets support common drawable xml tag, such as , , , , and etc in drawable xml.

    (Note: when using not supporting drawable xml tag, just can't be adapted to multi theme)

    Specially, TintXXX widgets additional support directly tint drawable with app:drawableTint and app:drawableTintMode and set color alpha with android:alpha.

    Here is an example:

    //drawable xml
    //tint directly
    <selector
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
      <item android:drawable="@drawable/icon " android:state_pressed="true" app:drawableTint="@color/theme_color_primary" />
      <item android:drawable="@drawable/icon" app:drawableTint="@color/gray_dark" />
    </selector>
    // set color alpha in color
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="true" android:state_pressed="true">
            <shape>
              <corners android:radius="4dp" />
              <solid android:color="@color/theme_color_primary_dark" />
            </shape>
        </item>
        <item android:state_enabled="true">
            <shape>
              <solid android:color="@color/theme_color_primary" />
              <corners android:radius="4dp" />
            </shape>
        </item>
        <item android:state_enabled="false">
            <shape>
              <solid android:alpha="0.3" android:color="@color/theme_color_primary" />
              <corners android:radius="4dp" />
            </shape>
        </item>
    </selector>
  • Layout Xml TintXXX widgets can be tinted directly in layout xml that supporting most common android drawable attrs , such as background , src , drawableLeft, button and etc.

    (Note: when tinting directly in layout xml , must use the color variates(defining in Step 1), otherwise adapting different theme styles will be out of work)

    Here is an example:

    // shape_lock.xml
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <size android:width="1dp" />
        <solid android:color="@color/theme_color_primary" />
        <stroke android:color="@color/gray_dark" />
    </shape>
    
    // TintTextView
    // The selector_lock and selector_text is a ColorStateList
    // The shape_lock is a shape drawable.
    <com.bilibili.magicasakura.widgets.TintTextView
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:drawablePadding="@dimen/padding_half"
         android:drawableRight="@drawable/selector_lock"
         android:drwableLeft="@drawable/shape_lock"
         android:text="@string/textview_title"
         android:textColor="@color/selector_text"
         android:textSize="19sp" 
         app:drawableRightTint="@color/selector_lock" 
         app:drawableRightTintMode="src_in"/>

    Here is the table that supporting attr of TintXXX widgets:

    attr tint tintMode
    background backgroundTint backgroundTintMode
    src imageTint imageTintMode
    button compoundButtonTint compoundButtonTintMode
    drawableXxx drawableXxxTint drawableXxxTintMode
    progress progressTint,progressIndeterminateTint
    track trackTint trackTintMode
    thumb thumbTint thumbTintMode
  • Java code TintXXX widgets can also be tinted in java code. The way of tinting drawable is the same as android native methods.

    Here is an example:

    //the background of tintTextView is a shape selector, we can call method setBackgroundResource to tint the shape.
    tintTextView.setBackgroundResource(R.drawable.selector_shape_lock);
    
    //the src of tintImageView is a selector containing the png,we need call method setImageTintList than the android native method call once more.
    tintImageView.setImageResource(R.drawable.selecor_png_lock);
    tintImageView.setImageTintList(R.color.selector_color_lock);
  • STEP4 :

    The library provides utility class ThemeUtils to meet some special needs or your own custom widgets.

    Utility class ThemeUtils mainly provides the method of tinting drawable and convert the color variates(defining in Step 1) with the current theme including colorStateList and color.

    // R.color.selector_color.lock is a colorStateList, the method of ThemeUtils.getThemeColorStateList return the colorStateList with the current theme.
    ThemeUtils.getThemeColorStateList(context, R.color.selector_color.lock);
    ThemeUtils.getThemeColorStateList(context, context.getResource().getColorStateList(R.color.selector_color.lock));
  • STEP5 :

    About to support the night theme, there are two ways to choose.

    • Build night resource directories whitch are corresponding the default resource directories and put the independent night xml into corresponding directories, such as values-night/values, color-night/night ...

    • Define a series of color variates both in values-night/values which are same name but different value, then you just write a xml once with using the colors variates to adapt the night theme.

    // in value/color.xml
    <color name="theme_color_primary">#2EA200</color>
    <color name="theme_color_primary_dark">#057748</color>
    <color name="theme_color_primary_trans">#992EA200</color>
    <color name="theme_color_secondary">#2EA200</color> // special used for night theme primary color
    
    // in values-night/color.xml
    <color name="theme_color_primary">#2d2d2d</color>
    <color name="theme_color_primary_dark">#242424</color>
    <color name="theme_color_primary_trans">#992d2d2d</color>
    <color name="theme_color_secondary">#057748</color> // special used for night theme primary color
  • STEP6 :

    About to switch daily colorful theme, you can directly call the method Theme.refreshUI in main thread and this method also provides optional callback params to meet your custom needs during switching theme.

    About to switch night theme, when the version of your android support library is below 23.2.0, you can call the method of ThemeUtils.updateNightMode to switch in the night and daily theme, and when the version is above 23.2.0, you can use android native method in android support library.

Download

Demo Download here

TODO

  • Refactor part of the code including TintManager...

License

Copyright 2016 Bilibili

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

ijkplayer

Android/iOS video player based on FFmpeg n3.4, with MediaCodec, VideoToolbox support.
C
32,382
star
2

flv.js

HTML5 FLV Player
JavaScript
22,882
star
3

DanmakuFlameMaster

Android开源弹幕引擎·烈焰弹幕使 ~
Java
9,535
star
4

ailab

Python
5,588
star
5

boxing

Android multi-media selector based on MVP mode.
Java
3,193
star
6

overlord

Overlord是哔哩哔哩基于Go语言编写的memcache和redis&cluster的代理及集群管理功能,致力于提供自动化高可用的缓存服务解决方案。
Go
2,213
star
7

gengine

Go
1,947
star
8

discovery

A registry for resilient mid-tier load balancing and failover.
Go
1,779
star
9

WebAV

WebAV is an SDK built on WebCodecs, designed for creating and editing video files on the web platform. WebAV 是基于 WebCodecs 构建的 SDK,用于在 Web 平台上创建/编辑视频文件。
TypeScript
895
star
10

Index-1.9B

A SOTA lightweight multilingual LLM
Python
877
star
11

vim-vide

Lightest vimrc, while strong enough. 最轻的vim配置,却足够强!
Vim Script
792
star
12

drawee-text-view

Simple drawee spannable text view based on Fresco
Java
762
star
13

join-us

大B站需要更多的小伙伴~ ( ゜- ゜)つロ 乾杯~
720
star
14

LastOrder-Dota2

Dota2 AI bot
Python
398
star
15

NativeBitmapFactory

DFM jni
C++
321
star
16

jni4android

JNI Generater for Android
C
309
star
17

BRouter

Kotlin
302
star
18

biliobs

C++
241
star
19

UnityBVA

C#
198
star
20

quiche

C++
198
star
21

bas

Bilibili Animation Script
HTML
187
star
22

kratos-demo

a project that use kratos
Go
187
star
23

libyuv

mirror of https://chromium.googlesource.com/external/libyuv/
C++
151
star
24

apk-channelization

[不支持v2签名]Android多渠道输出脚本
Python
147
star
25

BurstLinker

🚀 A simple GIF encoder for Android.
C++
140
star
26

xpref

A SharedPreferences' wrapper that truly supported sharing data across multi-process
Kotlin
84
star
27

soundtouch

SoundTouch library compiled for ijkplayer/Android http://www.surina.net/soundtouch/sourcecode.html
C++
84
star
28

mahimahi

C++
74
star
29

muzei-bilibili

Muzei 插件,bilibili 壁纸源
Java
72
star
30

vlc-android-macbuild

Deprecated, official site is recommended http://git.videolan.org
Java
70
star
31

redis_sdk_cxx

C++
65
star
32

LastOrder

StarCraft AI bot
C++
62
star
33

vlc-ports-android

fork of git://git.videolan.org/vlc-ports/android.git
C++
57
star
34

bbip

Bilibili High Performance IP Resolve Library
C++
55
star
35

oresty

Lua
52
star
36

SMGo

Shang-Mi cipher library. Constant-time implementation of SM2 and SM4.
Go
42
star
37

http-service

TypeScript
40
star
38

bilibili.github.io

35
star
39

adaptation

too painful to work with PagerAdapter
Java
32
star
40

vlc

fork of git://git.videolan.org/vlc.git
C
30
star
41

openssl

mirror of https://www.openssl.org
C
30
star
42

twirp

twirp rpc
Go
27
star
43

ci-ijk-ffmpeg-ios

Prebuilt FFmpeg for ijkplayer on iOS
Shell
17
star
44

abtest

A layered experiment framework for C++/java/go
15
star
45

gas-preprocessor

personal fork of git://git.libav.org/gas-preprocessor.git
Perl
13
star
46

flvbind

A tools to combine multiple flv file to a single flv file.
C
13
star
47

uptool

JavaScript
12
star
48

nginx_quic_module

C
12
star
49

nginx_quic_stack

C++
11
star
50

jmp_logpipe

A tool for jumper analyize log and send the command to remove UDP server for aduit. And this tool will denied for lszrz protocol to prevent a large log for storage.
C
11
star
51

ci-ijk-ffmpeg-android

Continuous integration for ijkplayer on Android
8
star
52

vim

B站Vim会所
5
star
53

arrange-play

Java
5
star
54

bilibili_wtm

Python
1
star