Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/Bilibili/MagicaSakura

MagicaSakura 是 Android 多主题框架。~ is an Android multi theme library which supporting both daily colorful theme and night theme.
https://github.com/Bilibili/MagicaSakura

android-library colorful multi-theme multicolor svg theme vector

Last synced: 2 months ago
JSON representation

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

Lists

README

        

![cover](https://raw.githubusercontent.com/xyczero/MagicaSakura/master/screenshot/magicasakura_cover.png)

## MagicaSakura
[ ![Download](https://api.bintray.com/packages/xyczero/maven/MagicaSakura/images/download.svg?version=0.1.9-beta3) ](https://bintray.com/xyczero/maven/MagicaSakura/0.1.9-beta3/link)

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.

>2. Switch different theme without recreating activities.

>3. Provide TintXXX Widgets for adapting multi theme to be more convenient and fast.

>4. Just writie a drawable.xml or layout.xml can be automatically adapted to different theme styles.

>5. Offer backward-compatible versions of the Android system that can be used with 4.0.3 or higher.

>6. Support Vector Drawable with appcompat-v7.

>7. Easy to integrate to your app.

## Demo

![ScreenShot.gif](https://github.com/xyczero/MagicaSakura/blob/master/screenshot/magicasakura.gif)

You can download the lastest sample apk from Google Play.


## Gradle Dependency
```
compile 'com.bilibili:magicasakura:0.1.9-beta3@aar'
```

## Maven Dependency
```

com.bilibili
magicasakura
0.1.9-beta3
aar

```

## Usage


- **STEP1** :

Define your app global theme color variates in values/color.xml, like as:
```xml
#fb7299
#b85671
#99f0486c
```
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.
```java
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:

``` java
//drawable xml
//tint directly





```


```java
// set color alpha in color




















```

- 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:

```java
// shape_lock.xml






// TintTextView
// The selector_lock and selector_text is a ColorStateList
// The shape_lock is a shape drawable.

```

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:

```java
//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.

```java
// 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.

```java
// in value/color.xml
#2EA200
#057748
#992EA200
#2EA200 // special used for night theme primary color

// in values-night/color.xml
#2d2d2d
#242424
#992d2d2d
#057748 // 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](http://7xr9x5.com1.z0.glb.clouddn.com/magicasrkura_sample-release.apk)

## 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.