Scoopsdemo
Multi-Theme Style code demo
This article belongs to the code GG original, not by my consent, prohibited reprint.
Need to communicate, contact: Code_gg_boy
More exciting, always pay attention to the public number Code_gg_home
Basic usage
Brief description
Scoops is an Android application library that mainly addresses the multi-theme implementation scenario. By configuring multiple R.style.theme, the code dynamically sets the theme and restarts the current interface implementation.
Principle Analysis
This is done primarily by setting the theme (store settings), restarting the activity, and setting the style by Settheme.
How to use
1 Items of Build.gradle inside
Build.gradle inside of the 2 module
3 Values inside Add Themes.xml
Configure some topics for multi-theme replacement use
4 Androidmanifest.xml Inside
Configuration on the default theme, it is important to note that the configuration and 5 written in the default one to be consistent
5 Mainapp Inside
Complete the initialization, mainly set a series of topics. Where addtoppings is used as a comment, it is followed by its usage. Setsharedpreferences Configure the default storage key value Adddaynightflavor set the Daynight theme style, which will be discussed later.
6 inside the mainacitvity.
1: Need to inherit appcompatactivity
2: You need to call Scoop.getinstance () in front of Setcontentview. Apply (this); set the theme in.
3: Here is why you need to configure the default theme in Androidmanifest.xml, because the scoops design, in the case that the theme is not changed, do not set the theme, so the first time come in will not be set. So we're going to configure the default theme. and is the same as the default value set inside the Mainapp.
Here's true the third parameter description is set to the default theme, which is consistent with the theme we set in Androidmanifest.xml.
7 effects are as follows
8 Code Address
Official Address Https://github.com/52inc/Scoops
Demo Address Https://github.com/luxiaoming/ScoopsDemo
Annotation usage
The multi-theme effect is changed by the annotation method. You can reduce some code to make the code clearer.
1 First write a comment class
2 in Mainapp
When initializing, set the comment in addtoppings mode
3 in Mainactivity
Use the comment @bindtoppingstatus flag to follow the color of the status bar
Use the comment @bindtopping tag to follow the color of a control
@BindTopping tag can also have parameters
1 value corresponds to one of the values in the toppings
2 adapter keep up with an adapter, primarily to implement a custom set color scheme for some controls
3 Interpolator can configure the effect of color switching process, such as Accelerateinterpolator.class
The code can see there is also a comment, called @BindView (R.id.fab) This is the previous butter knife, do not know the end of the article can see the address
The specific effect is as follows
4 Use
In Butterknife.bind (this), when the interface view is bound, use Scoop.sugarcone (). bind (this), set the view, adapter, and animation toggle effects in
Where you need to update the color, use Scoop.sugarcone (). Update, the parameter is the first value for the updated annotation type, such as the Toppings.primary_dark parameter two is the color value.
5 Coloradapter Adapter
If you need to customize the view color transform action after the theme is changed (mainly sometimes does not meet our needs, such as we want to dynamically change the text color, high brightness, etc.), we need to customize a implementation of Coloradapter class, such as Buttoncoloradapter interface Coloradapter defines two methods
Set a new color value
void Applycolor (T view, @ColorInt int color);
Get Current color value
int GetColor (T view);
6 topic Bugs
1 when using the @BindTopping note, you must add Adapter=xxx.class if you use the default, add adapter = Defaultcoloradapter.class
2 The default settings Scoopsettingsactivity interface, when set as the default theme, there will be a bug. The reason is that this interface specifies the theme android:theme= "@style/theme.appcompat.light" If our default interface specifies that this is not the same as this, then this setting interface will be wrong on the default theme, and the effect of setting is inconsistent. Solution: Write yourself a setup interface.
7 Demo Address
Https://github.com/luxiaoming/ScoopsDemo
Butter Knife Note programming Read address:
http://mp.weixin.qq.com/s?__biz=MzI1MjMyOTU2Ng==&mid=2247483660&idx=1&sn= 9fc37fdb8f84763896b2696c03070db0&scene=4#wechat_redirect
Daytime Night Theme
Many applications have this function, can choose day and night mode, give users a better experience. For this reason, the system also provides a mechanism to implement this function.
The pattern is divided into four types of nighttime daytime follow system and automatic. Concrete implementation can go to see com.android.support:appcompat-v7:23.0.0 source
The nighttime and daytime patterns are well understood. automatically selects the corresponding theme according to the time of day and night. The following system uses the value of the system and relies on the system.
Implementation principle
Through two sets of themes, respectively placed under values and Values-night, the system according to the mode you set, corresponding to find the topic configuration values, and then set up.
Instructions for use
do not use the Scoops method
1 Customizing an attribute Attrs.xml
Here you can see that two properties are defined TextColor and TEXTSIZE formats are color and dimension, respectively
2 Creating a directory Values-night
3 below the values and Values-night
Create a new file Themes.xml, configure the same theme at the same time, add our custom attribute TextColor, and configure the value for it.
Properties under Values
Properties under Values-night
4 Places to use
Here the value of Mmode can take the following several
Mode_night_follow_system
Mode_night_yes
Mode_night_no
Mode_night_auto
5 places to use under our configuration
Here's android:textcolor= "? Attr/textcolor" Remember using the? attr to use custom properties
6 specific results, see Primary color change
7 Code Address
Https://github.com/luxiaoming/ScoopsDemo
using the Scoops method
1:mainapp
Use Adddaynightflavor to set a theme to Daynight mode
2: Configure the text color on the corresponding theme
Values inside
Values-night inside
3: Enter the app, in the settings inside set into Daynight mode, click the button scoops way to see the effect.
4: The effect is as follows
5: Code Address
Https://github.com/luxiaoming/ScoopsDemo
Principle Analysis
Core Code Location
Https://github.com/52inc/Scoops/tree/master/scoops/src/main/java/com/ftinc/scoop can see the main file
Flavor.java Store each topic information
Scoop.java main file, mainly provides interface, as external call.
Sugarcone.java parse the main file of comments, by using the Bind method, to find the compiled toppings class, through this to associate.
Topping.java the custom annotation method. tag on the property, and then compile, using the plug-in Scoops-compiler, generate a binding class with the suffix toppings, and then we use the BIND function to associate these classes with the interface.
Key methods
scoop.apply function
Let's take a look at the specific code flow for the public void apply (activity activity) to find the current topic Getcurrentflavor
Determine if the theme is Daytime night mode, if so, set the mode and then use
Another apply method, the parameters are the activity and theme ID settings theme, find the background color set, set the background of the form, and then go back to our own recreate restart the current interface, to achieve the switch.
Compiling plugins
Https://github.com/52inc/Scoops/tree/master/scoops-compiler
The main purpose is to parse our custom annotations. The main parsing is our annotations that we use addtoppings to join at the time of initialization. Once found, parse out the tagged values of the annotations, toggle the transition effects, and adapters. The purpose of the tag value is to update the value, using this to make a distinction.
Code logic: Find all the bindings in this tag view, then call the corresponding adapter one after the other to make the corresponding color update. Let's take a look at the core bind function
We look at the bind code, the main look at Getviewbinder this method can be. Go ahead and see the Findviewbinderforclass function.
The main process of code is: from the cache can be found, directly returned. Not found, then find Class.forName (Clsname + "_toppingbinder") class, construct one, and then return this and cache it down. Then call the inside of bind to associate view we can see Mainactivity_toppingbinder.smali this Smali file, this is the plugin generated code. We decompile and look at what this file does.
See, all of the comments are categorized in and used for later updates. This is how the annotation works.
An app multi-theme Architecture complete analysis