├── .gitignore ├── AndroidMultiTheme ├── .gitignore ├── android-multi-theme │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── leonhover │ │ │ └── theme │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── leonhover │ │ │ │ └── theme │ │ │ │ ├── ActivityTheme.java │ │ │ │ ├── DarkMode.java │ │ │ │ ├── IThemeObserver.java │ │ │ │ ├── MultiTheme.java │ │ │ │ ├── ThemeManager.java │ │ │ │ ├── ThemePreferences.java │ │ │ │ ├── ThemeUtils.java │ │ │ │ ├── ThemeViewCreator.java │ │ │ │ ├── ThemeViewEntities.java │ │ │ │ ├── annotation │ │ │ │ └── MultiThemeAttrs.java │ │ │ │ ├── base │ │ │ │ ├── BaseThemeActivity.java │ │ │ │ ├── BaseThemeFragment.java │ │ │ │ └── widget │ │ │ │ │ └── CoverImageView.java │ │ │ │ ├── custom │ │ │ │ └── CoverImageWidget.java │ │ │ │ └── widget │ │ │ │ ├── AbsListViewWidget.java │ │ │ │ ├── AbstractThemeWidget.java │ │ │ │ ├── CompoundButtonWidget.java │ │ │ │ ├── IThemeWidget.java │ │ │ │ ├── ImageViewWidget.java │ │ │ │ ├── LinearLayoutWidget.java │ │ │ │ ├── ListViewWidget.java │ │ │ │ ├── ProgressBarWidget.java │ │ │ │ ├── SeekBarWidget.java │ │ │ │ ├── TextViewWidget.java │ │ │ │ ├── ToolBarWidget.java │ │ │ │ └── ViewWidget.java │ │ └── res │ │ │ └── values │ │ │ ├── attrs.xml │ │ │ ├── ids.xml │ │ │ └── strings.xml │ │ └── test │ │ └── java │ │ └── io │ │ └── github │ │ └── leonhover │ │ └── theme │ │ └── ExampleUnitTest.java ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── leonhover │ │ │ └── theme │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── leonhover │ │ │ │ └── theme │ │ │ │ └── samples │ │ │ │ ├── MainActivity.java │ │ │ │ ├── SampleFragment.java │ │ │ │ ├── ThemeAdapter.java │ │ │ │ ├── ThemeApplication.java │ │ │ │ └── ThemeInfo.java │ │ └── res │ │ │ ├── drawable │ │ │ ├── black_button_bg_selector.xml │ │ │ ├── black_ic_theme.xml │ │ │ ├── black_progress_horizontal.xml │ │ │ ├── black_progress_rotate.xml │ │ │ ├── blue_button_bg_selector.xml │ │ │ ├── blue_ic_theme.xml │ │ │ ├── blue_progress_rotate.xml │ │ │ ├── green_button_bg_selector.xml │ │ │ ├── green_ic_theme.xml │ │ │ ├── green_progress_rotate.xml │ │ │ ├── red_button_bg_selector.xml │ │ │ ├── red_ic_theme.xml │ │ │ ├── red_progress_horizontal.xml │ │ │ ├── red_progress_rotate.xml │ │ │ ├── yellow_button_bg_selector.xml │ │ │ ├── yellow_ic_theme.xml │ │ │ └── yellow_progress_rotate.xml │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ ├── frag_sample.xml │ │ │ └── item_theme_adpater.xml │ │ │ ├── menu │ │ │ └── dark_theme_setings.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-v21 │ │ │ └── styles.xml │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ └── values │ │ │ ├── attrs.xml │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ ├── styles.xml │ │ │ └── themes.xml │ │ └── test │ │ └── java │ │ └── io │ │ └── github │ │ └── leonhover │ │ └── theme │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── License ├── README.md └── assets └── 2016-11-09 11_57_38.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /AndroidMultiTheme/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.jfrog.bintray' 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | 5 | android { 6 | compileSdkVersion 29 7 | defaultConfig { 8 | minSdkVersion 19 9 | targetSdkVersion 29 10 | versionCode 201 11 | versionName "2.0.0" 12 | 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | implementation fileTree(include: ['*.jar'], dir: 'libs') 26 | //noinspection GradleCompatible 27 | implementation 'androidx.appcompat:appcompat:1.1.0' 28 | androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { 29 | exclude group: 'com.android.support', module: 'support-annotations' 30 | }) 31 | testImplementation 'junit:junit:4.12' 32 | } 33 | 34 | def siteUrl = 'https://github.com/LeonHover/Android-Multi-Theme' 35 | def gitUrl = 'https://github.com/LeonHover/Android-Multi-Theme.git' 36 | 37 | group = "io.github.leonhover" 38 | 39 | install { 40 | repositories.mavenInstaller { 41 | pom { 42 | project { 43 | packaging 'aar' 44 | name 'Android Multi Theme' 45 | url siteUrl 46 | licenses { 47 | license { 48 | name 'The Apache Software License, Version 2.0' 49 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 50 | } 51 | } 52 | developers { 53 | developer { 54 | id 'leonhover' 55 | name '王宗亮' 56 | email 'leonhover@gmail.com' 57 | } 58 | } 59 | scm { 60 | connection gitUrl 61 | developerConnection gitUrl 62 | url siteUrl 63 | } 64 | } 65 | } 66 | } 67 | } 68 | 69 | task sourcesJar(type: Jar) { 70 | from android.sourceSets.main.java.srcDirs 71 | classifier = 'sources' 72 | } 73 | 74 | task javadoc(type: Javadoc) { 75 | source = android.sourceSets.main.java.srcDirs 76 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 77 | } 78 | 79 | task javadocJar(type: Jar, dependsOn: javadoc) { 80 | classifier = 'javadoc' 81 | from javadoc.destinationDir 82 | } 83 | 84 | artifacts { 85 | archives javadocJar 86 | archives sourcesJar 87 | } 88 | 89 | Properties properties = new Properties() 90 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 91 | 92 | bintray { 93 | user = properties.getProperty("bintray.user") 94 | key = properties.getProperty("bintray.apikey") 95 | configurations = ['archives'] 96 | pkg { 97 | repo = "android" 98 | name = "Android-Multi-Theme" 99 | version "2.0.0" 100 | websiteUrl = siteUrl 101 | vcsUrl = gitUrl 102 | licenses = ["Apache-2.0"] 103 | publish = true 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/wangzongliang/Development/Tools/Android/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/androidTest/java/io/github/leonhover/theme/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.github.leonhover.theme.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ActivityTheme.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.annotation.TargetApi; 4 | import android.os.Build; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.ColorInt; 7 | import androidx.annotation.StyleRes; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.view.Window; 13 | import android.view.WindowManager; 14 | 15 | import static io.github.leonhover.theme.ThemeUtils.IS_KITKAT; 16 | import static io.github.leonhover.theme.ThemeUtils.IS_LOLLIPOP; 17 | import static io.github.leonhover.theme.ThemeUtils.getStatusBarHeight; 18 | 19 | /** 20 | * Created by wangzongliang on 2016/10/28. 21 | */ 22 | 23 | public class ActivityTheme implements IThemeObserver { 24 | 25 | private AppCompatActivity activity; 26 | 27 | @StyleRes 28 | private int[] themes; 29 | 30 | private int darkThemeIndex = -1; 31 | 32 | private boolean isSupportMenuItemEnable = false; 33 | 34 | @AttrRes 35 | private int statusBarColorAttrResId = 0; 36 | 37 | private View statusBarPlaceHolder = null; 38 | 39 | public ActivityTheme(AppCompatActivity activity) { 40 | this.activity = activity; 41 | } 42 | 43 | /** 44 | * 设置主题资源数组 45 | * 46 | * @param themes 主题资源数组 47 | */ 48 | public final void setThemes(@StyleRes int[] themes) { 49 | this.themes = themes; 50 | } 51 | 52 | /** 53 | * 设置支持暗黑模式的主题资源数组 54 | * 55 | * @param darkThemeIndex 暗黑主题在主题资源数组中的位置 56 | * @param themes 主题资源数组 57 | */ 58 | public final void setThemes(int darkThemeIndex, @StyleRes int[] themes) { 59 | this.themes = themes; 60 | this.darkThemeIndex = darkThemeIndex; 61 | if (this.darkThemeIndex < 0 || this.darkThemeIndex >= this.themes.length) { 62 | throw new IllegalArgumentException("please check you param,there ara some error."); 63 | } 64 | } 65 | 66 | /** 67 | * 开启主题切换支持菜单功能 68 | * 69 | * @param enable 70 | */ 71 | public final void setSupportMenuItemThemeEnable(boolean enable) { 72 | this.isSupportMenuItemEnable = enable; 73 | } 74 | 75 | /** 76 | * 设置通知栏的颜色,4.4以下的设备无效 77 | * 78 | * @param statusBarColorAttrResId 指向颜色值的AttrRes 79 | */ 80 | public final void setStatusBarColorAttrRes(@AttrRes int statusBarColorAttrResId) { 81 | this.statusBarColorAttrResId = statusBarColorAttrResId; 82 | } 83 | 84 | public final void setStatusBarColor(@ColorInt int statusBarColor) { 85 | if (!IS_KITKAT) { 86 | return; 87 | } 88 | 89 | if (IS_LOLLIPOP) { 90 | setStatusBarColorOnLollipop(statusBarColor); 91 | } else if (IS_KITKAT) { 92 | setStatusBarColorKitKat(statusBarColor); 93 | } 94 | } 95 | 96 | public void assembleThemeBeforeInflate() { 97 | MultiTheme.addObserver(this); 98 | int whichTheme = MultiTheme.getAppTheme(); 99 | int theme = getTheme(whichTheme); 100 | if (theme > 0) { 101 | activity.setTheme(theme); 102 | } 103 | MultiTheme.assembleThemeBeforeInflate(activity); 104 | initializeStatusBarTheme(); 105 | } 106 | 107 | private void initializeStatusBarTheme() { 108 | MultiTheme.d(MultiTheme.TAG, "initializeStatusBarTheme sdk version:" + Build.VERSION.SDK_INT); 109 | if (!IS_KITKAT || statusBarColorAttrResId == 0) { 110 | return; 111 | } 112 | 113 | int statusColor = ThemeUtils.getColor(this.activity, statusBarColorAttrResId); 114 | if (IS_LOLLIPOP) { 115 | initializeStatusBarColorOnLollipop(statusColor); 116 | } else { 117 | initializeStatusBarColorKitKat(statusColor); 118 | } 119 | } 120 | 121 | @TargetApi(Build.VERSION_CODES.KITKAT) 122 | private void initializeStatusBarColorKitKat(int statusBarColor) { 123 | MultiTheme.d(MultiTheme.TAG, "setStatusBarColorKitkat"); 124 | int statusBarHeight = getStatusBarHeight(this.activity); 125 | Window window = activity.getWindow(); 126 | ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT); 127 | 128 | //First translucent status bar. 129 | window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 130 | 131 | ViewGroup actionBarRoot = (ViewGroup) mContentView.getParent(); 132 | 133 | statusBarPlaceHolder = new View(activity); 134 | ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight); 135 | //向 ContentView 中添加假 View 136 | actionBarRoot.addView(statusBarPlaceHolder, 0, lp); 137 | setStatusBarColorKitKat(statusBarColor); 138 | } 139 | 140 | private void setStatusBarColorKitKat(int statusBarColor) { 141 | if (statusBarPlaceHolder != null) { 142 | statusBarPlaceHolder.setBackgroundColor(statusBarColor); 143 | } 144 | } 145 | 146 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 147 | private void initializeStatusBarColorOnLollipop(int statusBarColor) { 148 | MultiTheme.d(MultiTheme.TAG, "setStatusBarColorOnLollipop"); 149 | Window window = activity.getWindow(); 150 | //设置透明状态栏,这样才能让 ContentView 向上 151 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 152 | 153 | //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色 154 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 155 | //设置状态栏颜色 156 | window.setStatusBarColor(statusBarColor); 157 | } 158 | 159 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 160 | private void setStatusBarColorOnLollipop(int statusBarColor) { 161 | 162 | Window window = activity.getWindow(); 163 | window.setStatusBarColor(statusBarColor); 164 | } 165 | 166 | public void destroy() { 167 | MultiTheme.removeObserver(this); 168 | this.activity = null; 169 | } 170 | 171 | @Override 172 | public int getPriority() { 173 | return PRIORITY_ACTIVITY; 174 | } 175 | 176 | @Override 177 | public final void onThemeChanged(int whichTheme) { 178 | MultiTheme.applyTheme(this.activity, getTheme(whichTheme)); 179 | changeStatusBarColor(); 180 | if (isSupportMenuItemEnable) { 181 | this.activity.supportInvalidateOptionsMenu(); 182 | } 183 | } 184 | 185 | private void changeStatusBarColor() { 186 | if (statusBarColorAttrResId != 0) { 187 | int statusBarColor = ThemeUtils.getColor(this.activity, statusBarColorAttrResId); 188 | setStatusBarColor(statusBarColor); 189 | } 190 | } 191 | 192 | private int getTheme(int index) { 193 | if (this.themes == null) { 194 | MultiTheme.e(MultiTheme.TAG, "There is no theme array."); 195 | return -1; 196 | } 197 | 198 | if (index == MultiTheme.DARK_THEME) { 199 | return getDarkTheme(); 200 | } else { 201 | return getNormalTheme(index); 202 | } 203 | } 204 | 205 | private int getNormalTheme(int index) { 206 | MultiTheme.d(MultiTheme.TAG, "getNormalTheme index:" + index); 207 | if (index > this.themes.length || index < 0) { 208 | MultiTheme.e(MultiTheme.TAG, "OutOfBound. we use the first theme."); 209 | return this.themes[0]; 210 | } 211 | return this.themes[index]; 212 | } 213 | 214 | private int getDarkTheme() { 215 | MultiTheme.d(MultiTheme.TAG, "getDarkTheme"); 216 | if (darkThemeIndex < 0 || darkThemeIndex >= this.themes.length) { 217 | MultiTheme.e(MultiTheme.TAG, "You forgot setup a darkThemeIndex, we use the first theme indeed."); 218 | return this.themes[0]; 219 | } 220 | return this.themes[this.darkThemeIndex]; 221 | } 222 | 223 | @Override 224 | public int compareTo(IThemeObserver o) { 225 | return getPriority() > o.getPriority() ? 1 : -1; 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/DarkMode.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | /** 4 | * 暗黑模式设置类型 5 | */ 6 | public enum DarkMode { 7 | /** 8 | * 未开启 9 | */ 10 | off, 11 | /** 12 | * 开启 13 | */ 14 | on, 15 | /** 16 | * 如果支持的话,跟随系统设置,不支持就相当于打开 17 | */ 18 | followSystem 19 | } 20 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/IThemeObserver.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | /** 4 | * Created by leonhover on 16-9-27. 5 | */ 6 | 7 | public interface IThemeObserver extends Comparable { 8 | 9 | int PRIORITY_ACTIVITY = 1; 10 | int PRIORITY_VIEW = 2; 11 | 12 | int getPriority(); 13 | 14 | void onThemeChanged(int whichTheme); 15 | } 16 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/MultiTheme.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.StyleRes; 7 | import androidx.appcompat.app.AppCompatActivity; 8 | import android.util.Log; 9 | import android.view.View; 10 | 11 | import io.github.leonhover.theme.widget.AbstractThemeWidget; 12 | 13 | /** 14 | * Created by wangzongliang on 2016/10/28. 15 | */ 16 | 17 | public class MultiTheme { 18 | 19 | static final String TAG = MultiTheme.class.getSimpleName(); 20 | public static final int DARK_THEME = 2016; 21 | public static int sDefaultThemeIndex = 0; 22 | private static ThemeManager sThemeManager = null; 23 | private static boolean debugMode = false; 24 | 25 | /** 26 | * 初始化多主题模块,必须在其他方法调用前调用 27 | */ 28 | public static void init(Application application) { 29 | sThemeManager = new ThemeManager(application); 30 | } 31 | 32 | /** 33 | * 设置调试模式 34 | * 35 | * @param debugMode 36 | * @deprecated 37 | */ 38 | public static void setDebugMode(boolean debugMode) { 39 | MultiTheme.debugMode = debugMode; 40 | } 41 | 42 | public static void enableDebug() { 43 | debugMode = true; 44 | } 45 | 46 | /** 47 | * 添加自定义的AbstractThemeWidget,通常紧随init方法后调用。 48 | * 49 | * @param widgetKey 类型索引,指定themeWidget对应的View的Class,如TextViewWidget对应TextView.class. 50 | * @param themeWidget 自定义的AbstractThemeWidget 51 | */ 52 | public static void addThemeWidget(Class widgetKey, AbstractThemeWidget themeWidget) { 53 | checkInstance(); 54 | sThemeManager.addThemeWidget(widgetKey, themeWidget); 55 | } 56 | 57 | /** 58 | * 获取view对应的AbstractThemeWidget。 59 | * 60 | * @param view View. 61 | * @return AbstractThemeWidget 62 | */ 63 | public static AbstractThemeWidget getThemeWidget(View view) { 64 | checkInstance(); 65 | return sThemeManager.getThemeWidget(view.getClass()); 66 | } 67 | 68 | /** 69 | * 获取Class对应的AbstractThemeWidget。 70 | * 71 | * @param clazz class of View. 72 | * @return AbstractThemeWidget 73 | */ 74 | public static AbstractThemeWidget getThemeWidget(Class clazz) { 75 | checkInstance(); 76 | return sThemeManager.getThemeWidget(clazz); 77 | } 78 | 79 | /** 80 | * 添加主题改变的观察器 81 | * 82 | * @param themeObserver 83 | */ 84 | public static void addObserver(IThemeObserver themeObserver) { 85 | checkInstance(); 86 | sThemeManager.addObserver(themeObserver); 87 | } 88 | 89 | /** 90 | * 移除主题改变的观察器 91 | * 92 | * @param themeObserver 93 | */ 94 | public static void removeObserver(IThemeObserver themeObserver) { 95 | checkInstance(); 96 | sThemeManager.removeObserver(themeObserver); 97 | } 98 | 99 | /** 100 | * 组装主题相关元素,注意在Activity的setContentView之前调用。 101 | * 102 | * @param activity AppCompatActivity 103 | */ 104 | public static void assembleThemeBeforeInflate(AppCompatActivity activity) { 105 | checkInstance(); 106 | sThemeManager.assembleThemeBeforeInflate(activity); 107 | } 108 | 109 | /** 110 | * 动态地为View添加主题控件的WidgetKey。 111 | * 112 | * @param view 113 | * @return AbstractThemeWidget 114 | */ 115 | public static AbstractThemeWidget addViewThemeWidgetKeyTag(View view) { 116 | checkInstance(); 117 | return sThemeManager.addViewThemeWidgetKeyTag(view); 118 | } 119 | 120 | /** 121 | * 判断是否开启了暗黑模式 122 | * @return true or false 123 | */ 124 | public static DarkMode getDarkMode() { 125 | checkInstance(); 126 | return sThemeManager.getDarkMode(); 127 | } 128 | 129 | public static void setDarkMode(DarkMode mode) { 130 | checkInstance(); 131 | sThemeManager.setDarkMode(mode); 132 | } 133 | 134 | /** 135 | * 判断系统是否开启了暗黑模式 136 | * @return true or false 137 | */ 138 | public static boolean isSystemDarkMode() { 139 | checkInstance(); 140 | return sThemeManager.isSystemDarkMode(); 141 | } 142 | 143 | /** 144 | * 改变主题 145 | * 146 | * @param whichTheme 应用主题,这里对应的是ActivityTheme中的theme[]的索引值 147 | * @return true 表示切换主题成功,反之为false. 148 | */ 149 | public static boolean setAppTheme(int whichTheme) { 150 | checkInstance(); 151 | return sThemeManager.setAppTheme(whichTheme); 152 | } 153 | 154 | /** 155 | * 设置应用的默认主题,修改后就会这个值就不再生效了,一般在Application的子类中设置 156 | * 157 | * @param defaultThemeIndex 默认主题资源数组所索引 158 | */ 159 | public static void setDefaultAppTheme(int defaultThemeIndex) { 160 | checkInstance(); 161 | sDefaultThemeIndex = defaultThemeIndex; 162 | sThemeManager.setDefaultTheme(sDefaultThemeIndex); 163 | } 164 | 165 | /** 166 | * 获取应用的当前主题 167 | * 168 | * @return int 当前应用的theme[]的索引值 169 | */ 170 | public static int getAppTheme() { 171 | checkInstance(); 172 | return sThemeManager.getAppTheme(); 173 | } 174 | 175 | /** 176 | * 改变Activity主题 177 | * 178 | * @param activity 179 | * @param theme 180 | */ 181 | static void applyTheme(Activity activity, @StyleRes int theme) { 182 | checkInstance(); 183 | sThemeManager.applyTheme(activity, theme); 184 | } 185 | 186 | /** 187 | * 改变主题后,动态的应用View的主题改变,通常情况只在DecorView中找不到这些view的情况下使用。 188 | * 189 | * @param view 190 | */ 191 | public static void applyTheme(View view) { 192 | checkInstance(); 193 | sThemeManager.applyTheme(view); 194 | } 195 | 196 | /** 197 | * 对View的单个Attr属性设定主题,可用来动态修改主题元素的索引值 198 | * 199 | * @param view view 200 | * @param themeElementKey Attr属性值 201 | * @param themeElementValue Attr属性值 202 | */ 203 | public static void applySingleElementTheme(View view, @AttrRes int themeElementKey, @AttrRes int themeElementValue) { 204 | checkInstance(); 205 | AbstractThemeWidget themeWidget; 206 | if (sThemeManager.getThemeWidgetKey(view) == null) { 207 | themeWidget = addViewThemeWidgetKeyTag(view); 208 | } else { 209 | themeWidget = sThemeManager.getThemeWidget(view.getClass()); 210 | } 211 | themeWidget.applySingleElementTheme(view, themeElementKey, themeElementValue); 212 | } 213 | 214 | /** 215 | * 移除View的单个Attr属性设定的主题元素值,移除此Attr属性将不再跟随主题而改变 216 | * 217 | * @param view view 218 | * @param themeElementKey Attr属性值 219 | */ 220 | public static void removeSingleElementTheme(View view, @AttrRes int themeElementKey) { 221 | checkInstance(); 222 | AbstractThemeWidget themeWidget; 223 | if (sThemeManager.getThemeWidgetKey(view) == null) { 224 | themeWidget = addViewThemeWidgetKeyTag(view); 225 | } else { 226 | themeWidget = sThemeManager.getThemeWidget(view.getClass()); 227 | } 228 | themeWidget.removeSingleElementTheme(view, themeElementKey); 229 | } 230 | 231 | public static void release() { 232 | sThemeManager = null; 233 | } 234 | 235 | private static void checkInstance() throws IllegalStateException { 236 | if (sThemeManager == null) { 237 | throw new IllegalStateException("Do you initialize MultiTheme in creation of your app's application."); 238 | } 239 | } 240 | 241 | public static int d(String tag, String msg) { 242 | if (debugMode) { 243 | return Log.d(tag, msg); 244 | } else { 245 | return -1; 246 | } 247 | } 248 | 249 | public static int e(String tag, String msg) { 250 | if (debugMode) { 251 | return Log.e(tag, msg); 252 | } else { 253 | return -1; 254 | } 255 | } 256 | 257 | public static int i(String tag, String msg) { 258 | if (debugMode) { 259 | return Log.i(tag, msg); 260 | } else { 261 | return -1; 262 | } 263 | } 264 | } 265 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemeManager.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import androidx.annotation.StyleRes; 7 | import androidx.core.view.LayoutInflaterCompat; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | import androidx.appcompat.app.AppCompatDelegate; 10 | import androidx.appcompat.widget.Toolbar; 11 | import android.util.AttributeSet; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.AbsListView; 16 | import android.widget.CompoundButton; 17 | import android.widget.ImageView; 18 | import android.widget.LinearLayout; 19 | import android.widget.ListView; 20 | import android.widget.ProgressBar; 21 | import android.widget.SeekBar; 22 | import android.widget.TextView; 23 | 24 | import java.util.HashMap; 25 | import java.util.Iterator; 26 | import java.util.Map; 27 | import java.util.Set; 28 | import java.util.TreeSet; 29 | 30 | import io.github.leonhover.theme.base.widget.CoverImageView; 31 | import io.github.leonhover.theme.custom.CoverImageWidget; 32 | import io.github.leonhover.theme.widget.AbsListViewWidget; 33 | import io.github.leonhover.theme.widget.AbstractThemeWidget; 34 | import io.github.leonhover.theme.widget.CompoundButtonWidget; 35 | import io.github.leonhover.theme.widget.IThemeWidget; 36 | import io.github.leonhover.theme.widget.ImageViewWidget; 37 | import io.github.leonhover.theme.widget.LinearLayoutWidget; 38 | import io.github.leonhover.theme.widget.ListViewWidget; 39 | import io.github.leonhover.theme.widget.ProgressBarWidget; 40 | import io.github.leonhover.theme.widget.SeekBarWidget; 41 | import io.github.leonhover.theme.widget.TextViewWidget; 42 | import io.github.leonhover.theme.widget.ToolBarWidget; 43 | import io.github.leonhover.theme.widget.ViewWidget; 44 | 45 | 46 | /** 47 | * Created by leonhover on 16-9-26. 48 | */ 49 | 50 | class ThemeManager { 51 | 52 | private static final String TAG = ThemeManager.class.getSimpleName(); 53 | 54 | private Map, AbstractThemeWidget> themeWidgetMap; 55 | 56 | private ThemeViewCreator themeViewCreator; 57 | 58 | private int appTheme = -1; 59 | 60 | private Application application; 61 | 62 | private Set themeObserverSet; 63 | 64 | ThemeManager(Application application) { 65 | this.application = application; 66 | this.themeWidgetMap = new HashMap<>(); 67 | this.themeWidgetMap.put(View.class, new ViewWidget()); 68 | this.themeWidgetMap.put(TextView.class, new TextViewWidget()); 69 | this.themeWidgetMap.put(ImageView.class, new ImageViewWidget()); 70 | this.themeWidgetMap.put(CompoundButton.class, new CompoundButtonWidget()); 71 | this.themeWidgetMap.put(ProgressBar.class, new ProgressBarWidget()); 72 | this.themeWidgetMap.put(ListView.class, new ListViewWidget()); 73 | this.themeWidgetMap.put(SeekBar.class, new SeekBarWidget()); 74 | this.themeWidgetMap.put(LinearLayout.class, new LinearLayoutWidget()); 75 | this.themeWidgetMap.put(AbsListView.class, new AbsListViewWidget()); 76 | this.themeWidgetMap.put(Toolbar.class, new ToolBarWidget()); 77 | this.themeWidgetMap.put(CoverImageView.class, new CoverImageWidget()); 78 | 79 | this.themeObserverSet = new TreeSet<>(); 80 | this.themeViewCreator = new ThemeViewCreator(); 81 | this.appTheme = ThemePreferences.getAppTheme(this.application); 82 | 83 | } 84 | 85 | /** 86 | * 判断系统是否是暗黑模式 87 | * 88 | * @return true or false 89 | */ 90 | boolean isSystemDarkMode() { 91 | return ThemeUtils.isSystemDarkMode(this.application); 92 | } 93 | 94 | /** 95 | * 获取设置的暗黑模式 96 | * 97 | * @return true or false 98 | */ 99 | DarkMode getDarkMode() { 100 | return ThemePreferences.getDarkMode(this.application); 101 | } 102 | 103 | /** 104 | * 设置跟跟随系统颜色模式调整主题 105 | * 106 | * @param mode 跟随与否 107 | */ 108 | void setDarkMode(DarkMode mode) { 109 | switch (mode) { 110 | case on: 111 | setAppTheme(MultiTheme.DARK_THEME); 112 | break; 113 | case followSystem: 114 | if (isSystemDarkMode()) { 115 | setAppTheme(MultiTheme.DARK_THEME); 116 | } else { 117 | setAppTheme(MultiTheme.sDefaultThemeIndex); 118 | } 119 | break; 120 | default: 121 | setAppTheme(MultiTheme.sDefaultThemeIndex); 122 | } 123 | ThemePreferences.setDarkMode(this.application, mode); 124 | } 125 | 126 | void addThemeWidget(Class widgetKey, AbstractThemeWidget themeWidget) { 127 | this.themeWidgetMap.put(widgetKey, themeWidget); 128 | } 129 | 130 | AbstractThemeWidget getThemeWidget(Class clazz) { 131 | Class widgetKey = findProperThemeWidgetKey(clazz); 132 | return this.themeWidgetMap.get(widgetKey); 133 | } 134 | 135 | void addObserver(IThemeObserver observer) { 136 | this.themeObserverSet.add(observer); 137 | } 138 | 139 | void removeObserver(IThemeObserver observer) { 140 | Iterator obsIterator = this.themeObserverSet.iterator(); 141 | while (obsIterator.hasNext()) { 142 | IThemeObserver obs = obsIterator.next(); 143 | if (observer.equals(obs)) { 144 | obsIterator.remove(); 145 | return; 146 | } 147 | } 148 | } 149 | 150 | void assembleThemeBeforeInflate(final AppCompatActivity activity) { 151 | 152 | if (activity == null) { 153 | throw new NullPointerException(); 154 | } 155 | LayoutInflaterCompat.setFactory2(activity.getLayoutInflater(), new LayoutInflater.Factory2() { 156 | @Override 157 | public View onCreateView(String name, Context context, AttributeSet attributeSet) { 158 | MultiTheme.d(TAG, "onCreateView name:" + name); 159 | return onCreateView(null, name, context, attributeSet); 160 | } 161 | 162 | @Override 163 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 164 | MultiTheme.d(TAG, "onCreateView parent:" + parent + ",name:" + name); 165 | AppCompatDelegate appCompatDelegate = activity.getDelegate(); 166 | View view = appCompatDelegate.createView(parent, name, context, attrs); 167 | 168 | 169 | if (view == null) { 170 | view = themeViewCreator.createView(parent, name, context, attrs); 171 | } 172 | 173 | if (view == null) { 174 | return null; 175 | } 176 | 177 | Class widgetKey = findProperThemeWidgetKey(view.getClass()); 178 | assembleViewThemeElement(view, attrs, widgetKey); 179 | 180 | return view; 181 | } 182 | }); 183 | } 184 | 185 | private Class findProperThemeWidgetKey(Class clazz) { 186 | 187 | Class tmpKey = null; 188 | for (Class widgetKey : this.themeWidgetMap.keySet()) { 189 | 190 | if (clazz.equals(widgetKey)) { 191 | return clazz; 192 | } 193 | 194 | if (widgetKey.isAssignableFrom(clazz)) { 195 | if (tmpKey == null) { 196 | tmpKey = widgetKey; 197 | } else { 198 | if (tmpKey.isAssignableFrom(widgetKey)) { 199 | tmpKey = widgetKey; 200 | } 201 | } 202 | } 203 | } 204 | 205 | if (tmpKey == null) { 206 | return null; 207 | } else { 208 | return tmpKey; 209 | } 210 | 211 | } 212 | 213 | /** 214 | * 指定View的主题WidgetKey 215 | * 216 | * @param view 217 | * @return AbstractThemeWidget 218 | */ 219 | AbstractThemeWidget addViewThemeWidgetKeyTag(View view) { 220 | if (view != null) { 221 | Class widgetKey = findProperThemeWidgetKey(view.getClass()); 222 | view.setTag(R.id.amt_tag_widget_key, widgetKey); 223 | view.setTag(R.id.amt_tag_view_current_theme, getAppTheme()); 224 | return this.themeWidgetMap.get(widgetKey); 225 | } 226 | return null; 227 | } 228 | 229 | /** 230 | * 组装每个View的主题元素 231 | */ 232 | private void assembleViewThemeElement(View view, AttributeSet attributeSet, Class widgetKey) { 233 | 234 | MultiTheme.d(TAG, "assembleViewThemeElement theme widget type:" + widgetKey + ", view:" + view); 235 | if (view == null) { 236 | return; 237 | } 238 | 239 | IThemeWidget themeWidget = this.themeWidgetMap.get(widgetKey); 240 | if (themeWidget != null) { 241 | view.setTag(R.id.amt_tag_widget_key, widgetKey); 242 | view.setTag(R.id.amt_tag_view_current_theme, getAppTheme()); 243 | if (attributeSet != null) { 244 | int styleResId = attributeSet.getStyleAttribute(); 245 | if (styleResId != 0) { 246 | view.setTag(R.id.amt_tag_widget_style, styleResId); 247 | } 248 | themeWidget.assemble(view, attributeSet); 249 | } 250 | MultiTheme.d(TAG, "assembleViewThemeElement theme widget type: " + widgetKey + " themeWidget:" + themeWidget.getClass().getSimpleName()); 251 | } else { 252 | view.setTag(R.id.amt_tag_widget_key, null); 253 | MultiTheme.i(TAG, "unsupported theme widget type " + widgetKey + ",is your custom theme widget?"); 254 | } 255 | } 256 | 257 | /** 258 | * 改变主题,在默认主题与夜间主题之间进行切换 259 | * 260 | * @return true 改变为夜间主题,false 改变为默认主题 261 | */ 262 | boolean setAppTheme(int whichTheme) { 263 | MultiTheme.d(MultiTheme.TAG, "setAppTheme whichTheme=" + whichTheme); 264 | 265 | if (whichTheme > -1 && whichTheme != appTheme) { 266 | ThemePreferences.setAppTheme(this.application, whichTheme); 267 | appTheme = whichTheme; 268 | for (IThemeObserver themeObserver : themeObserverSet) { 269 | themeObserver.onThemeChanged(whichTheme); 270 | } 271 | return true; 272 | } else { 273 | return false; 274 | } 275 | 276 | } 277 | 278 | int getAppTheme() { 279 | return this.appTheme; 280 | } 281 | 282 | void setDefaultTheme(int defaultThemeIndex) { 283 | if (ThemePreferences.getAppTheme(this.application) == -1) { 284 | setAppTheme(defaultThemeIndex); 285 | } 286 | } 287 | 288 | /** 289 | * 改变主题,在默认主题与夜间主题之间进行切换 290 | * 291 | * @param activity Activity 292 | * @param themeResId 主题资源id 293 | */ 294 | void applyTheme(Activity activity, @StyleRes int themeResId) { 295 | activity.setTheme(themeResId); 296 | applyTheme(activity); 297 | } 298 | 299 | private void applyTheme(Activity activity) { 300 | MultiTheme.d(TAG, "applyThemeForActivity"); 301 | if (activity == null) { 302 | throw new IllegalArgumentException("activity is null!"); 303 | } 304 | 305 | View decorView = activity.getWindow().getDecorView(); 306 | applyTheme(decorView); 307 | 308 | } 309 | 310 | /** 311 | * 对单个View应用主题 312 | * 313 | * @param view View 314 | */ 315 | void applyTheme(View view) { 316 | 317 | int themeOfView = -1; 318 | int styleResOfView = -1; 319 | try { 320 | themeOfView = ThemeUtils.getViewTag(view, R.id.amt_tag_view_current_theme); 321 | styleResOfView = ThemeUtils.getViewTag(view, R.id.amt_tag_widget_style); 322 | } catch (ClassCastException | NullPointerException e) { 323 | } 324 | 325 | if (themeOfView == appTheme) { 326 | return; 327 | } 328 | 329 | Class widgetKey = getThemeWidgetKey(view); 330 | 331 | MultiTheme.d(TAG, "applyTheme theme widget type:" + widgetKey + " ,view:" + view); 332 | IThemeWidget themeWidget = this.themeWidgetMap.get(widgetKey); 333 | if (themeWidget != null) { 334 | if (styleResOfView != -1) { 335 | themeWidget.applyStyle(view, styleResOfView); 336 | } 337 | themeWidget.applyTheme(view); 338 | MultiTheme.d(TAG, "applyTheme theme widget type: " + widgetKey + " ,view:" + view + " themeWidget:" + themeWidget); 339 | } else { 340 | MultiTheme.i(TAG, "applyTheme unsupport theme widget type:" + widgetKey + ", view:" + view); 341 | } 342 | 343 | if (view instanceof ViewGroup) { 344 | ViewGroup viewGroup = (ViewGroup) view; 345 | for (int i = 0; i < viewGroup.getChildCount(); i++) { 346 | View child = viewGroup.getChildAt(i); 347 | applyTheme(child); 348 | } 349 | } 350 | 351 | view.setTag(R.id.amt_tag_view_current_theme, getAppTheme()); 352 | } 353 | 354 | @SuppressWarnings("unchecked") 355 | Class getThemeWidgetKey(View view) { 356 | return (Class) view.getTag(R.id.amt_tag_widget_key); 357 | } 358 | } 359 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemePreferences.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | 6 | /** 7 | * Created by wangzongliang on 2016/11/2. 8 | */ 9 | 10 | public class ThemePreferences { 11 | 12 | private static final String AMT_PREFERENCES_NAME = "amt_preferences"; 13 | 14 | private static final String AMT_APP_THEME = "amt_app_theme"; 15 | private static final String AMT_DARK_MODE = "amt_dark_mode"; 16 | 17 | private static SharedPreferences getPreferences(Context context) { 18 | return context.getSharedPreferences(AMT_PREFERENCES_NAME, Context.MODE_PRIVATE); 19 | } 20 | 21 | static void setAppTheme(Context context, int theme) { 22 | SharedPreferences themePreferences = getPreferences(context); 23 | SharedPreferences.Editor editor = themePreferences.edit(); 24 | editor.putInt(AMT_APP_THEME, theme); 25 | editor.apply(); 26 | } 27 | 28 | static int getAppTheme(Context context) { 29 | return getPreferences(context).getInt(AMT_APP_THEME, -1); 30 | } 31 | 32 | static void setDarkMode(Context context, DarkMode mode) { 33 | SharedPreferences themePreferences = getPreferences(context); 34 | SharedPreferences.Editor editor = themePreferences.edit(); 35 | editor.putInt(AMT_DARK_MODE, mode.ordinal()); 36 | editor.apply(); 37 | } 38 | 39 | static DarkMode getDarkMode(Context context) { 40 | return DarkMode.values()[(getPreferences(context).getInt(AMT_DARK_MODE, DarkMode.off.ordinal()))]; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemeUtils.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.res.ColorStateList; 6 | import android.content.res.Configuration; 7 | import android.content.res.Resources; 8 | import android.graphics.drawable.Drawable; 9 | import android.os.Build; 10 | import androidx.annotation.AttrRes; 11 | import androidx.annotation.ColorRes; 12 | import androidx.annotation.DrawableRes; 13 | import androidx.annotation.IdRes; 14 | import android.text.TextUtils; 15 | import android.util.TypedValue; 16 | import android.view.View; 17 | 18 | import java.lang.reflect.Field; 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.reflect.Method; 21 | 22 | /** 23 | * Created by leonhover on 16-9-26. 24 | */ 25 | 26 | public class ThemeUtils { 27 | 28 | public static final boolean IS_JELLY_BEAN = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; 29 | public static final boolean IS_KITKAT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 30 | public static final boolean IS_LOLLIPOP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; 31 | public static final boolean IS_M = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; 32 | 33 | /** 34 | * 判断是否是以“?attr/**”引用的资源 35 | * 36 | * @param attrValue AttributeValue 37 | * @return true or false 38 | */ 39 | public static boolean isAttrReference(String attrValue) { 40 | if (!TextUtils.isEmpty(attrValue) && attrValue.startsWith("?")) { 41 | return true; 42 | } 43 | 44 | return false; 45 | } 46 | 47 | /** 48 | * 获取Attr的资源ID 49 | * 50 | * @param attrValue AttributeValue 51 | * @return resid 52 | */ 53 | public static int getAttrResId(String attrValue) { 54 | if (TextUtils.isEmpty(attrValue)) { 55 | return -1; 56 | } 57 | 58 | String resIdStr = attrValue.substring(1); 59 | return Integer.valueOf(resIdStr); 60 | } 61 | 62 | /** 63 | * 获取attrResId指向的颜色Color 64 | * 65 | * @param context 66 | * @param attrResId attr资源id 67 | * @return Color 68 | */ 69 | public static int getColor(Context context, @AttrRes int attrResId) { 70 | if (context == null) { 71 | return -1; 72 | } 73 | return getColor(context.getTheme(), context.getResources(), attrResId); 74 | } 75 | 76 | /** 77 | * 获取attrResId指向的颜色Color 78 | * 79 | * @param theme 主题 80 | * @param resources 资源 81 | * @param attrResId attr资源id 82 | * @return Color 83 | */ 84 | @SuppressWarnings("NewApi") 85 | public static int getColor(Resources.Theme theme, Resources resources, @AttrRes int attrResId) { 86 | TypedValue typedValue = new TypedValue(); 87 | theme.resolveAttribute(attrResId, typedValue, true); 88 | MultiTheme.d("a", "Color type:" + typedValue.toString()); 89 | int color; 90 | if (IS_M) { 91 | color = resources.getColor(typedValue.resourceId, theme); 92 | } else { 93 | color = resources.getColor(typedValue.resourceId); 94 | } 95 | return color; 96 | } 97 | 98 | 99 | /** 100 | * 获取colorResId指向的颜色Color 101 | * 102 | * @param context 103 | * @param colorResId 颜色资源id 104 | * @return Color 105 | */ 106 | public static int getColorWithResId(Context context, @ColorRes int colorResId) { 107 | if (context == null) { 108 | return -1; 109 | } 110 | return getColorWithResId(context.getTheme(), context.getResources(), colorResId); 111 | } 112 | 113 | /** 114 | * 获取colorResId指向的颜色Color 115 | * 116 | * @param theme 主题 117 | * @param resources 资源 118 | * @param colorResId 颜色资源id 119 | * @return Color 120 | */ 121 | @SuppressWarnings("NewApi") 122 | public static int getColorWithResId(Resources.Theme theme, Resources resources, @ColorRes int colorResId) { 123 | int color; 124 | if (IS_M) { 125 | color = resources.getColor(colorResId, theme); 126 | } else { 127 | color = resources.getColor(colorResId); 128 | } 129 | return color; 130 | } 131 | 132 | 133 | /** 134 | * 获取attrResId指向的颜色ColorStateList 135 | * 136 | * @param context 137 | * @param attrResId attr资源id 138 | * @return ColorStateList 139 | */ 140 | public static ColorStateList getColorStateList(Context context, @AttrRes int attrResId) { 141 | if (context == null) { 142 | return null; 143 | } 144 | return getColorStateList(context.getTheme(), context.getResources(), attrResId); 145 | } 146 | 147 | /** 148 | * 获取attrResId指向的颜色ColorStateList 149 | * 150 | * @param theme 主题 151 | * @param resources 资源 152 | * @param attrResId attr资源id 153 | * @return ColorStateList 154 | */ 155 | @SuppressWarnings("NewApi") 156 | public static ColorStateList getColorStateList(Resources.Theme theme, Resources resources, @AttrRes int attrResId) { 157 | TypedValue typedValue = new TypedValue(); 158 | theme.resolveAttribute(attrResId, typedValue, true); 159 | 160 | MultiTheme.d("a", "ColorStateList type:" + typedValue.toString()); 161 | ColorStateList colorStateList; 162 | 163 | if (IS_M) { 164 | colorStateList = resources.getColorStateList(typedValue.resourceId, theme); 165 | } else { 166 | colorStateList = resources.getColorStateList(typedValue.resourceId); 167 | } 168 | return colorStateList; 169 | } 170 | 171 | /** 172 | * 获取ColorStateListResId指向的颜色ColorStateList 173 | * 174 | * @param context 175 | * @param colorStateListResId 资源id 176 | * @return ColorStateList 177 | */ 178 | public static ColorStateList getColorStateListWithResId(Context context, @ColorRes int colorStateListResId) { 179 | if (context == null) { 180 | return null; 181 | } 182 | return getColorStateListWithResId(context.getTheme(), context.getResources(), colorStateListResId); 183 | } 184 | 185 | 186 | /** 187 | * 获取ColorStateListResId指向的颜色ColorStateList 188 | * 189 | * @param theme 主题 190 | * @param resources 资源 191 | * @param colorStateListResId 资源id 192 | * @return ColorStateList 193 | */ 194 | @SuppressWarnings("NewApi") 195 | public static ColorStateList getColorStateListWithResId(Resources.Theme theme, Resources resources, @ColorRes int colorStateListResId) { 196 | 197 | ColorStateList colorStateList; 198 | 199 | if (IS_M) { 200 | colorStateList = resources.getColorStateList(colorStateListResId, theme); 201 | } else { 202 | colorStateList = resources.getColorStateList(colorStateListResId); 203 | } 204 | return colorStateList; 205 | } 206 | 207 | /** 208 | * 获取attrResId指向的Drawable 209 | * 210 | * @param context 211 | * @param attrResId attr资源id 212 | * @return Drawable 213 | */ 214 | public static Drawable getDrawable(Context context, @AttrRes int attrResId) { 215 | if (context == null) { 216 | return null; 217 | } 218 | 219 | return getDrawable(context.getTheme(), context.getResources(), attrResId); 220 | } 221 | 222 | /** 223 | * 获取attrResId指向的Drawable 224 | * 225 | * @param theme 主题 226 | * @param resources 资源 227 | * @param attrResId attr资源id 228 | * @return Drawable 229 | */ 230 | @SuppressWarnings("NewApi") 231 | public static Drawable getDrawable(Resources.Theme theme, Resources resources, @AttrRes int attrResId) { 232 | TypedValue typedValue = new TypedValue(); 233 | theme.resolveAttribute(attrResId, typedValue, true); 234 | Drawable drawable; 235 | MultiTheme.d("a", "drawable type:" + typedValue.toString()); 236 | if (IS_M) { 237 | drawable = resources.getDrawable(typedValue.resourceId, theme); 238 | } else { 239 | drawable = resources.getDrawable(typedValue.resourceId); 240 | } 241 | return drawable; 242 | } 243 | 244 | 245 | /** 246 | * 获取DrawableResId指向的Drawable 247 | * 248 | * @param context 249 | * @param drawableResId Drawable资源id 250 | * @return Drawable 251 | */ 252 | public static Drawable getDrawableWithResId(Context context, @DrawableRes int drawableResId) { 253 | if (context == null) { 254 | return null; 255 | } 256 | 257 | return getDrawableWithResId(context.getTheme(), context.getResources(), drawableResId); 258 | } 259 | 260 | /** 261 | * 获取DrawableResId指向的Drawable 262 | * 263 | * @param theme 主题 264 | * @param resources 资源 265 | * @param drawableResId Drawable资源id 266 | * @return Drawable 267 | */ 268 | @SuppressLint("NewApi") 269 | public static Drawable getDrawableWithResId(Resources.Theme theme, Resources resources, @DrawableRes int drawableResId) { 270 | Drawable drawable; 271 | if (IS_M) { 272 | drawable = resources.getDrawable(drawableResId, theme); 273 | } else { 274 | drawable = resources.getDrawable(drawableResId); 275 | } 276 | return drawable; 277 | } 278 | 279 | public static T getViewTag(View view, @IdRes int tagKey) throws ClassCastException, NullPointerException { 280 | try { 281 | return (T) view.getTag(tagKey); 282 | } catch (ClassCastException e) { 283 | throw e; 284 | } catch (NullPointerException e) { 285 | throw e; 286 | } 287 | } 288 | 289 | /** 290 | * 获取StatusBar高度 291 | * 292 | * @param context 293 | * @return 294 | */ 295 | @SuppressLint("PrivateApi") 296 | public static int getStatusBarHeight(Context context) { 297 | Class c = null; 298 | Object obj = null; 299 | Field field = null; 300 | int x = 0, statusBarHeight = 0; 301 | try { 302 | c = Class.forName("com.android.internal.R$dimen"); 303 | obj = c.newInstance(); 304 | field = c.getField("status_bar_height"); 305 | x = Integer.parseInt(field.get(obj).toString()); 306 | statusBarHeight = context.getResources().getDimensionPixelSize(x); 307 | } catch (Exception e1) { 308 | e1.printStackTrace(); 309 | } 310 | return statusBarHeight; 311 | } 312 | 313 | public static Object invokeMethod(Object obj, String method, Object... parameters) { 314 | if (obj == null || TextUtils.isEmpty(method)) { 315 | return null; 316 | } 317 | 318 | Class[] parameterTypes = new Class[parameters.length]; 319 | for (int i = 0; i < parameters.length; i++) { 320 | parameterTypes[i] = parameters[i].getClass(); 321 | } 322 | 323 | try { 324 | Method methodWanted = obj.getClass().getDeclaredMethod(method, parameterTypes); 325 | methodWanted.setAccessible(true); 326 | return methodWanted.invoke(obj, parameters); 327 | } catch (NoSuchMethodException e) { 328 | e.printStackTrace(); 329 | } catch (InvocationTargetException e) { 330 | e.printStackTrace(); 331 | } catch (IllegalAccessException e) { 332 | e.printStackTrace(); 333 | } 334 | 335 | return null; 336 | } 337 | 338 | /** 339 | * 判断是否是暗黑模式开启状态 340 | * 341 | * @param context Context 342 | * @return true or false 343 | */ 344 | public static boolean isSystemDarkMode(Context context) { 345 | int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; 346 | boolean result = mode == Configuration.UI_MODE_NIGHT_YES; 347 | MultiTheme.d(MultiTheme.TAG, "isSystemDarkMode result:" + result); 348 | return result; 349 | } 350 | } 351 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemeViewCreator.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import java.lang.reflect.Constructor; 8 | import java.lang.reflect.InvocationTargetException; 9 | import java.util.Arrays; 10 | 11 | /** 12 | * Created by leonhover on 16-9-28. 13 | */ 14 | 15 | public class ThemeViewCreator { 16 | 17 | private static final Class[] constructorSignature = new Class[]{ 18 | Context.class, AttributeSet.class}; 19 | 20 | private static String[] sClassPrefixs = { 21 | "android.widget.", 22 | // "android.app." 23 | }; 24 | 25 | private static String[] sViewPostfixs = { 26 | "View", 27 | "SurfaceView", 28 | "TextureView", 29 | }; 30 | 31 | private static String[] sExcludeTags = { 32 | "fragment", 33 | "merge", 34 | "include", 35 | "ViewStub", 36 | }; 37 | 38 | 39 | protected View createView(View parent, String name, Context context, AttributeSet attrs) { 40 | View view = null; 41 | if (name.contains(".")) { 42 | view = createSubViewImpl(parent, "", name, context, attrs); 43 | } else { 44 | if (Arrays.asList(sExcludeTags).contains(name)) { 45 | return null; 46 | } else if (Arrays.asList(sViewPostfixs).contains(name)) { 47 | view = createSubViewImpl(parent, "android.view.", name, context, attrs); 48 | } else { 49 | for (String prefix : sClassPrefixs) { 50 | view = createSubViewImpl(parent, prefix, name, context, attrs); 51 | if (view != null) { 52 | break; 53 | } 54 | } 55 | } 56 | } 57 | 58 | return view; 59 | } 60 | 61 | private View createSubViewImpl(View parent, String prefix, String name, Context context, AttributeSet attrs) { 62 | 63 | Constructor constructor = null; 64 | Class clazz = null; 65 | try { 66 | clazz = context.getClassLoader() 67 | .loadClass(prefix + name).asSubclass(View.class); 68 | } catch (ClassNotFoundException e) { 69 | e.printStackTrace(); 70 | } 71 | 72 | if (clazz == null) { 73 | return null; 74 | } 75 | 76 | //获取构造函数 77 | try { 78 | constructor = clazz.getConstructor(constructorSignature); 79 | 80 | } catch (NoSuchMethodException e) { 81 | e.printStackTrace(); 82 | } 83 | try { 84 | if (constructor != null) { 85 | constructor.setAccessible(true); 86 | return (View) constructor.newInstance(context, attrs); 87 | } 88 | } catch (InstantiationException e) { 89 | e.printStackTrace(); 90 | } catch (InvocationTargetException e) { 91 | e.printStackTrace(); 92 | } catch (IllegalAccessException e) { 93 | e.printStackTrace(); 94 | } 95 | 96 | return null; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemeViewEntities.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.view.View; 4 | 5 | import java.util.Collection; 6 | import java.util.HashSet; 7 | 8 | 9 | /** 10 | * Created by leonhover on 16-10-9. 11 | */ 12 | 13 | public class ThemeViewEntities extends HashSet implements IThemeObserver { 14 | 15 | private boolean underObservation = false; 16 | 17 | @Override 18 | public boolean add(View object) { 19 | boolean ret = super.add(object); 20 | checkObserver(); 21 | return ret; 22 | } 23 | 24 | @Override 25 | public boolean addAll(Collection collection) { 26 | boolean ret = super.addAll(collection); 27 | checkObserver(); 28 | return ret; 29 | } 30 | 31 | @Override 32 | public boolean remove(Object object) { 33 | boolean ret = super.remove(object); 34 | checkObserver(); 35 | return ret; 36 | } 37 | 38 | @Override 39 | public boolean removeAll(Collection collection) { 40 | boolean ret = super.removeAll(collection); 41 | checkObserver(); 42 | return ret; 43 | } 44 | 45 | private void checkObserver() { 46 | if (this.size() > 0) { 47 | if (!underObservation) { 48 | MultiTheme.addObserver(this); 49 | underObservation = true; 50 | } 51 | } else { 52 | if (underObservation) { 53 | MultiTheme.removeObserver(this); 54 | underObservation = false; 55 | } 56 | } 57 | } 58 | 59 | @Override 60 | public void clear() { 61 | super.clear(); 62 | checkObserver(); 63 | } 64 | 65 | @Override 66 | public int getPriority() { 67 | return PRIORITY_VIEW; 68 | } 69 | 70 | @Override 71 | public void onThemeChanged(int whichTheme) { 72 | for (View view : this) { 73 | MultiTheme.applyTheme(view); 74 | } 75 | } 76 | 77 | @Override 78 | public int compareTo(IThemeObserver o) { 79 | return getPriority() > o.getPriority() ? 1 : -1; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/annotation/MultiThemeAttrs.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.annotation; 2 | 3 | import androidx.annotation.AttrRes; 4 | 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Inherited; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * MultiThemeAttrs是一个Attribute的数组,用来注解MultiTheme支持的主题控件类。 13 | *

14 | * 这些Attribute是具体View的对应Styleable中的值 15 | * 这些值不但是作为MultiTheme中用来索引的Key,也是通过其从XML解析出的Attribute中取出主题元素的值的关键。 16 | *

17 | * 例: 18 | * android:background="?attr/theme_background" 19 | *

20 | * View中都有'android.R.attr.background'的标签,添加其作为View的MultiThemeAttrs,那么MultiTheme 21 | * 就会将'background'标签下的'theme_background'对应attrRes取出,作为主题元素的值来存储。当主题改变 22 | * 的时候,通过'android.R.attr.background'找到attrRes,然后分发到对应的 23 | * {@link io.github.leonhover.theme.widget.ViewWidget}的'applyElementTheme'。 24 | *

25 | * Created by wangzongliang on 2017/2/14. 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.TYPE) 29 | @Inherited 30 | public @interface MultiThemeAttrs { 31 | @AttrRes int[] value(); 32 | } 33 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/base/BaseThemeActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.base; 2 | 3 | import android.content.res.Configuration; 4 | import android.os.Bundle; 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | import io.github.leonhover.theme.ActivityTheme; 8 | import io.github.leonhover.theme.DarkMode; 9 | import io.github.leonhover.theme.MultiTheme; 10 | import io.github.leonhover.theme.ThemeViewEntities; 11 | 12 | /** 13 | * Created by leonhover on 16-10-9. 14 | */ 15 | 16 | public abstract class BaseThemeActivity extends AppCompatActivity { 17 | 18 | private final static String TAG = BaseThemeActivity.class.getSimpleName(); 19 | private ActivityTheme activityTheme = new ActivityTheme(this); 20 | private ThemeViewEntities themeViewEntities = new ThemeViewEntities(); 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | initTheme(); 25 | super.onCreate(savedInstanceState); 26 | } 27 | 28 | private void initTheme() { 29 | configTheme(activityTheme); 30 | activityTheme.assembleThemeBeforeInflate(); 31 | } 32 | 33 | /** 34 | * 配置Activity的主题 35 | * 36 | * @param activityTheme Activity主题 37 | */ 38 | protected abstract void configTheme(ActivityTheme activityTheme); 39 | 40 | public final ThemeViewEntities getThemeViewEntities() { 41 | return this.themeViewEntities; 42 | } 43 | 44 | /** 45 | * 设置StatusBar的背景色 46 | * 47 | * @param color Color 48 | */ 49 | public void setStatusBarColor(int color) { 50 | this.activityTheme.setStatusBarColor(color); 51 | } 52 | 53 | @Override 54 | public void onConfigurationChanged(Configuration newConfig) { 55 | super.onConfigurationChanged(newConfig); 56 | DarkMode mode = MultiTheme.getDarkMode(); 57 | if (mode == DarkMode.followSystem) { 58 | int uiMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK; 59 | if (uiMode == Configuration.UI_MODE_NIGHT_YES) { 60 | MultiTheme.d("MultiTheme", "onConfigurationChanged mode:UI_MODE_NIGHT_YES"); 61 | MultiTheme.setAppTheme(MultiTheme.DARK_THEME); 62 | } else { 63 | MultiTheme.d("MultiTheme", "onConfigurationChanged mode:UI_MODE_NIGHT_NO"); 64 | MultiTheme.setAppTheme(MultiTheme.sDefaultThemeIndex); 65 | } 66 | } 67 | } 68 | 69 | @Override 70 | protected void onDestroy() { 71 | super.onDestroy(); 72 | if (activityTheme != null) { 73 | activityTheme.destroy(); 74 | activityTheme = null; 75 | } 76 | 77 | themeViewEntities.clear(); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/base/BaseThemeFragment.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.base; 2 | 3 | import androidx.fragment.app.Fragment; 4 | 5 | import io.github.leonhover.theme.ThemeViewEntities; 6 | 7 | /** 8 | * Created by leonhover on 16-10-9. 9 | */ 10 | 11 | public class BaseThemeFragment extends Fragment { 12 | 13 | private ThemeViewEntities themeViewEntities = new ThemeViewEntities(); 14 | 15 | public ThemeViewEntities getThemeViewEntities() { 16 | return themeViewEntities; 17 | } 18 | 19 | @Override 20 | public void onDestroyView() { 21 | super.onDestroyView(); 22 | themeViewEntities.clear(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/base/widget/CoverImageView.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.base.widget; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Bitmap; 6 | import android.graphics.Canvas; 7 | import android.graphics.Color; 8 | import android.graphics.Paint; 9 | import android.graphics.PorterDuff; 10 | import androidx.appcompat.widget.AppCompatImageView; 11 | import android.util.AttributeSet; 12 | 13 | import java.lang.ref.SoftReference; 14 | 15 | import io.github.leonhover.theme.R; 16 | 17 | /** 18 | * Created by wangzongliang on 2016/11/8. 19 | */ 20 | 21 | public class CoverImageView extends AppCompatImageView { 22 | 23 | public static final String TAG = "CoverImageView"; 24 | 25 | private Paint mPaint; 26 | private SoftReference tmpBitmapReference = null; 27 | private SoftReference tmpCanvasReference = null; 28 | private int canvasWidth = -1; 29 | private int canvasHeight = -1; 30 | private int[] pixels; 31 | private int coverColor = -1; 32 | 33 | public CoverImageView(Context context) { 34 | super(context); 35 | } 36 | 37 | public CoverImageView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | init(context, attrs, 0); 40 | } 41 | 42 | public CoverImageView(Context context, AttributeSet attrs, int defStyleAttr) { 43 | super(context, attrs, defStyleAttr); 44 | init(context, attrs, defStyleAttr); 45 | } 46 | 47 | private void init(Context context, AttributeSet attrs, int defStyleAttr) { 48 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 49 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CoverImageView); 50 | coverColor = ta.getColor(R.styleable.CoverImageView_coverColor, -1); 51 | ta.recycle(); 52 | } 53 | 54 | public void setCoverColor(int color) { 55 | this.coverColor = color; 56 | invalidate(); 57 | } 58 | 59 | @Override 60 | protected void onDraw(Canvas canvas) { 61 | super.onDraw(canvas); 62 | 63 | if (coverColor < 0) { 64 | return; 65 | } 66 | 67 | int coverAlpha = coverColor & 0xff000000; 68 | 69 | if (coverAlpha == 0x00000000) { 70 | return; 71 | } 72 | 73 | if (getHeight() + getWidth() == 0) { 74 | return; 75 | } 76 | 77 | if (canvasHeight != getHeight() || canvasWidth != getWidth()) { 78 | canvasHeight = getHeight(); 79 | canvasWidth = getWidth(); 80 | pixels = new int[canvasWidth * canvasHeight]; 81 | } 82 | 83 | Bitmap tmpBitmap = createCoverBitmap(canvasWidth, canvasHeight); 84 | 85 | Canvas tmpCanvas = createCoverCanvas(tmpBitmap); 86 | 87 | tmpCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 88 | super.onDraw(tmpCanvas); 89 | 90 | 91 | tmpBitmap.getPixels(pixels, 0, canvasWidth, 0, 0, canvasWidth, canvasHeight); 92 | for (int i = 0; i < pixels.length; i++) { 93 | if (pixels[i] != 0x00000000) { 94 | pixels[i] = coverColor; 95 | } 96 | } 97 | tmpBitmap.setPixels(pixels, 0, canvasWidth, 0, 0, canvasWidth, canvasHeight); 98 | canvas.drawBitmap(tmpBitmap, 0, 0, mPaint); 99 | } 100 | 101 | private Bitmap createCoverBitmap(int width, int height) { 102 | Bitmap coverBitmap = null; 103 | 104 | if (tmpBitmapReference != null && tmpBitmapReference.get() != null) { 105 | coverBitmap = tmpBitmapReference.get(); 106 | if (coverBitmap.getWidth() != width || coverBitmap.getHeight() != height || coverBitmap.isRecycled()) { 107 | coverBitmap = null; 108 | } 109 | } 110 | 111 | if (coverBitmap == null) { 112 | coverBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 113 | tmpBitmapReference = new SoftReference(coverBitmap); 114 | } 115 | 116 | return coverBitmap; 117 | } 118 | 119 | private Canvas createCoverCanvas(Bitmap bitmap) { 120 | Canvas coverCanvas = null; 121 | 122 | if (tmpCanvasReference != null && tmpCanvasReference.get() != null) { 123 | coverCanvas = tmpCanvasReference.get(); 124 | } 125 | 126 | if (coverCanvas == null) { 127 | coverCanvas = new Canvas(bitmap); 128 | tmpCanvasReference = new SoftReference(coverCanvas); 129 | } 130 | 131 | return coverCanvas; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/custom/CoverImageWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.custom; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.ColorRes; 6 | import android.view.View; 7 | 8 | import io.github.leonhover.theme.R; 9 | import io.github.leonhover.theme.base.widget.CoverImageView; 10 | import io.github.leonhover.theme.widget.ImageViewWidget; 11 | 12 | /** 13 | * Created by wangzongliang on 2016/11/8. 14 | */ 15 | 16 | public class CoverImageWidget extends ImageViewWidget { 17 | 18 | @Override 19 | protected void initializeLibraryElements() { 20 | super.initializeLibraryElements(); 21 | addThemeElementKey(R.attr.coverColor); 22 | } 23 | 24 | @Override 25 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 26 | super.applyElementTheme(view, themeElementKey, resId); 27 | CoverImageView coverImageView = (CoverImageView) view; 28 | if (R.attr.coverColor == themeElementKey) { 29 | setCoverColor(coverImageView, resId); 30 | } 31 | } 32 | 33 | public static void setCoverColor(CoverImageView coverImageView, @ColorRes int colorResId) { 34 | 35 | if (coverImageView == null) { 36 | return; 37 | } 38 | 39 | coverImageView.setCoverColor(getColor(coverImageView, colorResId)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/AbsListViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.AbsListView; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.listSelector 16 | }) 17 | public class AbsListViewWidget extends ViewWidget { 18 | 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | AbsListView absListView = (AbsListView) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.listSelector: 25 | setSelector(absListView, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setSelector(AbsListView absListView, @DrawableRes int drawableResId) { 31 | if (absListView == null) { 32 | return; 33 | } 34 | 35 | absListView.setSelector(getDrawable(absListView, drawableResId)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/AbstractThemeWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.content.res.ColorStateList; 4 | import android.content.res.TypedArray; 5 | import android.graphics.drawable.Drawable; 6 | import androidx.annotation.AnyRes; 7 | import androidx.annotation.AttrRes; 8 | import androidx.annotation.ColorRes; 9 | import androidx.annotation.DrawableRes; 10 | import androidx.annotation.StyleRes; 11 | import android.util.AttributeSet; 12 | import android.util.SparseIntArray; 13 | import android.util.TypedValue; 14 | import android.view.View; 15 | 16 | import java.util.HashSet; 17 | import java.util.Set; 18 | 19 | import io.github.leonhover.theme.MultiTheme; 20 | import io.github.leonhover.theme.R; 21 | import io.github.leonhover.theme.ThemeUtils; 22 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 23 | 24 | import static io.github.leonhover.theme.ThemeUtils.getAttrResId; 25 | import static io.github.leonhover.theme.ThemeUtils.isAttrReference; 26 | 27 | /** 28 | * Created by leonhover on 16-9-27. 29 | */ 30 | 31 | public abstract class AbstractThemeWidget implements IThemeWidget { 32 | 33 | public static final String TAG = AbstractThemeWidget.class.getSimpleName(); 34 | 35 | /** 36 | * 主题元素集合 37 | */ 38 | private Set themeElementKeySet = new HashSet<>(); 39 | 40 | public AbstractThemeWidget() { 41 | initializeAppThemeElements(this.getClass()); 42 | initializeLibraryElements(); 43 | } 44 | 45 | /** 46 | * 初始化应用或FrameWork的attr属性作为Key的主题元素 47 | */ 48 | private void initializeAppThemeElements(Class themeWidgetClass) { 49 | if (themeWidgetClass != null && AbstractThemeWidget.class.isAssignableFrom(themeWidgetClass)) { 50 | Class superClass = themeWidgetClass.getSuperclass(); 51 | initializeAppThemeElements(superClass); 52 | 53 | MultiThemeAttrs multiThemeAttrs = themeWidgetClass.getAnnotation(MultiThemeAttrs.class); 54 | if (multiThemeAttrs != null) { 55 | for (int attrRes : multiThemeAttrs.value()) { 56 | themeElementKeySet.add(attrRes); 57 | } 58 | } 59 | } 60 | } 61 | 62 | /** 63 | * 仅供MultiThemeLibrary来初始一些不能直接当做常量引用的attr属性主题元素Key。 64 | */ 65 | protected void initializeLibraryElements() { 66 | 67 | } 68 | 69 | protected final void addThemeElementKey(@AttrRes int themeElementKey) { 70 | themeElementKeySet.add(themeElementKey); 71 | } 72 | 73 | /** 74 | * 添加主题支持的Attribute Resource Id; 75 | * 76 | * @param themeElementKey Attribute Ressurce Id 77 | */ 78 | public final void add(@AttrRes int themeElementKey) { 79 | themeElementKeySet.add(themeElementKey); 80 | } 81 | 82 | @Override 83 | public void assemble(View view, AttributeSet attributeSet) { 84 | MultiTheme.d(TAG, "assemble"); 85 | if (themeElementKeySet == null || themeElementKeySet.size() < 1) { 86 | return; 87 | } 88 | 89 | SparseIntArray themeElementPairs = getThemeElementPairs(view); 90 | 91 | int count = attributeSet.getAttributeCount(); 92 | 93 | for (int themeElementKey : themeElementKeySet) { 94 | for (int i = 0; i < count; i++) { 95 | if (themeElementKey == attributeSet.getAttributeNameResource(i)) { 96 | String attrValue = attributeSet.getAttributeValue(i); 97 | int attrId = -1; 98 | if (isAttrReference(attrValue)) { 99 | attrId = getAttrResId(attrValue); 100 | themeElementPairs.put(themeElementKey, attrId); 101 | break; 102 | } 103 | } 104 | } 105 | } 106 | 107 | } 108 | 109 | @Override 110 | public void applyTheme(View view) { 111 | MultiTheme.d(TAG, "applyTheme"); 112 | if (view == null) { 113 | throw new IllegalArgumentException(" view is illegal!!"); 114 | } 115 | 116 | if (themeElementKeySet == null || themeElementKeySet.size() < 1) { 117 | return; 118 | } 119 | 120 | SparseIntArray themeElements = getThemeElementPairs(view); 121 | 122 | for (int i = 0; i < themeElements.size(); i++) { 123 | int attrResSupported = themeElements.keyAt(i); 124 | int attrResId = themeElements.get(attrResSupported); 125 | 126 | if (attrResId > 0) { 127 | applySingleElementTheme(view, attrResSupported, attrResId); 128 | } 129 | } 130 | } 131 | 132 | @Override 133 | public void applyStyle(View view, @StyleRes int styleRes) { 134 | 135 | if (view == null) { 136 | throw new IllegalArgumentException(" view is illegal!!"); 137 | } 138 | 139 | if (themeElementKeySet == null || themeElementKeySet.size() < 1) { 140 | return; 141 | } 142 | 143 | for (Integer themeElementKey : themeElementKeySet) { 144 | 145 | TypedArray typedArray = view.getContext().obtainStyledAttributes(styleRes, new int[]{themeElementKey}); 146 | if (typedArray != null && typedArray.getIndexCount() > 0) { 147 | TypedValue typedValue = new TypedValue(); 148 | typedArray.getValue(0, typedValue); 149 | if (typedValue.resourceId > 0) { 150 | applyElementTheme(view, themeElementKey, typedValue.resourceId); 151 | } 152 | } 153 | 154 | if (typedArray != null) { 155 | typedArray.recycle(); 156 | } 157 | } 158 | } 159 | 160 | /** 161 | * 应用单个主题元素 162 | * 163 | * @param view View 164 | * @param themeElementKey 主题元素Key 165 | * @param themeElementValue Attr资源ID 166 | */ 167 | public void applySingleElementTheme(View view, @AttrRes int themeElementKey, @AttrRes int themeElementValue) { 168 | SparseIntArray themeElementPairs = getThemeElementPairs(view); 169 | themeElementPairs.put(themeElementKey, themeElementValue); 170 | TypedValue typedValue = new TypedValue(); 171 | view.getContext().getTheme().resolveAttribute(themeElementValue, typedValue, true); 172 | if (typedValue.resourceId > 0) { 173 | applyElementTheme(view, themeElementKey, typedValue.resourceId); 174 | } 175 | } 176 | 177 | /** 178 | * 移除View的单个主题元素,移除的主题元素将不再跟随主题变化 179 | * 180 | * @param view View 181 | * @param themeElementKey 主题元素Key 182 | */ 183 | public void removeSingleElementTheme(View view, @AttrRes int themeElementKey) { 184 | SparseIntArray themeElementPairs = getThemeElementPairs(view); 185 | int index = themeElementPairs.indexOfKey(themeElementKey); 186 | if (index >= 0) { 187 | themeElementPairs.removeAt(index); 188 | } 189 | } 190 | 191 | /** 192 | * 应用单个主题元素 193 | * 194 | * @param view View 195 | * @param themeElementKey 主题元素 196 | * @param resId 资源ID 197 | */ 198 | protected void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 199 | 200 | } 201 | 202 | /** 203 | * 获取单个View的所有主题元素键值对 204 | * 205 | * @param view View 206 | * @return SparseIntArray 207 | */ 208 | private static SparseIntArray getThemeElementPairs(View view) { 209 | 210 | SparseIntArray themeElementPairs = (SparseIntArray) view.getTag(R.id.amt_tag_theme_element_pairs); 211 | 212 | if (themeElementPairs == null) { 213 | themeElementPairs = new SparseIntArray(); 214 | view.setTag(R.id.amt_tag_theme_element_pairs, themeElementPairs); 215 | } 216 | 217 | return themeElementPairs; 218 | } 219 | 220 | public static Drawable getDrawable(View view, @DrawableRes int drawableResId) { 221 | return ThemeUtils.getDrawableWithResId(view.getContext(), drawableResId); 222 | } 223 | 224 | public static int getColor(View view, @ColorRes int colorResId) { 225 | return ThemeUtils.getColorWithResId(view.getContext(), colorResId); 226 | } 227 | 228 | public static ColorStateList getColorStateList(View view, @ColorRes int colorStateListResId) { 229 | return ThemeUtils.getColorStateListWithResId(view.getContext(), colorStateListResId); 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/CompoundButtonWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.CompoundButton; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.button 16 | }) 17 | public class CompoundButtonWidget extends TextViewWidget { 18 | 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | CompoundButton compoundButton = (CompoundButton) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.button: 25 | setButtonDrawable(compoundButton, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setButtonDrawable(CompoundButton compoundButton, @DrawableRes int drawableResId) { 31 | 32 | if (compoundButton == null) { 33 | return; 34 | } 35 | 36 | compoundButton.setButtonDrawable(getDrawable(compoundButton, drawableResId)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/IThemeWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.StyleRes; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | /** 8 | * Created by leonhover on 16-9-27. 9 | */ 10 | 11 | public interface IThemeWidget { 12 | 13 | /** 14 | * 组装主题的信息到View的TAG中。 15 | * 16 | * @param view View 17 | * @param attributeSet View在Inflate时的Attribute. 18 | */ 19 | void assemble(View view, AttributeSet attributeSet); 20 | 21 | /** 22 | * view应用主题Theme 23 | * 24 | * @param view View 25 | */ 26 | void applyTheme(View view); 27 | 28 | /** 29 | * View应用主题中的Style 30 | * 31 | * @param view 32 | * @param styleRes Style id 33 | */ 34 | void applyStyle(View view, @StyleRes int styleRes); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/ImageViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | 7 | import android.view.View; 8 | import android.widget.ImageView; 9 | 10 | import io.github.leonhover.theme.R; 11 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 12 | 13 | /** 14 | * Created by leonhover on 16-9-27. 15 | */ 16 | @MultiThemeAttrs({ 17 | android.R.attr.src, 18 | }) 19 | public class ImageViewWidget extends ViewWidget { 20 | 21 | @Override 22 | protected void initializeLibraryElements() { 23 | super.initializeLibraryElements(); 24 | addThemeElementKey(R.attr.srcCompat); 25 | } 26 | 27 | @Override 28 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 29 | super.applyElementTheme(view, themeElementKey, resId); 30 | ImageView imageView = (ImageView) view; 31 | if (themeElementKey == android.R.attr.src) { 32 | setImageDrawable(imageView, resId); 33 | } else if (themeElementKey == R.attr.srcCompat) { 34 | setImageCompatDrawable(imageView, resId); 35 | } 36 | } 37 | 38 | public static void setImageDrawable(ImageView imageView, @DrawableRes int drawableResId) { 39 | if (imageView == null) { 40 | return; 41 | } 42 | 43 | imageView.setImageDrawable(getDrawable(imageView, drawableResId)); 44 | } 45 | 46 | public static void setImageCompatDrawable(ImageView imageView, @DrawableRes int drawableResId) { 47 | if (imageView == null) { 48 | return; 49 | } 50 | 51 | imageView.setImageDrawable(getDrawable(imageView, drawableResId)); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/LinearLayoutWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.LinearLayout; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | 12 | /** 13 | * Created by leonhover on 16-9-27. 14 | */ 15 | @MultiThemeAttrs({ 16 | android.R.attr.divider 17 | }) 18 | public class LinearLayoutWidget extends ViewWidget { 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | LinearLayout linearLayout = (LinearLayout) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.divider: 25 | setDividerDrawable(linearLayout, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setDividerDrawable(LinearLayout linearLayout, @DrawableRes int drawableResId) { 31 | if (linearLayout == null) { 32 | return; 33 | } 34 | 35 | linearLayout.setDividerDrawable(getDrawable(linearLayout,drawableResId)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/ListViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.ListView; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | 15 | @MultiThemeAttrs({ 16 | android.R.attr.divider 17 | }) 18 | public class ListViewWidget extends AbsListViewWidget { 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | ListView listView = (ListView) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.divider: 25 | setDividerDrawable(listView, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setDividerDrawable(ListView listView, @DrawableRes int drawableResId) { 31 | 32 | if (listView == null) { 33 | return; 34 | } 35 | 36 | int dividerHeight = listView.getDividerHeight(); 37 | listView.setDivider(getDrawable(listView, drawableResId)); 38 | listView.setDividerHeight(dividerHeight); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/ProgressBarWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.graphics.Rect; 4 | import android.graphics.drawable.Drawable; 5 | import androidx.annotation.AnyRes; 6 | import androidx.annotation.AttrRes; 7 | import androidx.annotation.DrawableRes; 8 | import android.view.View; 9 | import android.widget.ProgressBar; 10 | 11 | import io.github.leonhover.theme.ThemeUtils; 12 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 13 | 14 | /** 15 | * Created by leonhover on 16-9-27. 16 | */ 17 | @MultiThemeAttrs({ 18 | android.R.attr.progressDrawable, 19 | android.R.attr.indeterminateDrawable, 20 | }) 21 | public class ProgressBarWidget extends ViewWidget { 22 | @Override 23 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 24 | super.applyElementTheme(view, themeElementKey, resId); 25 | ProgressBar progressBar = (ProgressBar) view; 26 | switch (themeElementKey) { 27 | case android.R.attr.progressDrawable: 28 | setProgressDrawable(progressBar, resId); 29 | break; 30 | case android.R.attr.indeterminateDrawable: 31 | setIndeterminateDrawable(progressBar, resId); 32 | break; 33 | } 34 | } 35 | 36 | public static void setProgressDrawable(ProgressBar progressBar, @DrawableRes int drawableResId) { 37 | if (progressBar == null) { 38 | return; 39 | } 40 | 41 | Drawable drawable = getDrawable(progressBar, drawableResId); 42 | 43 | if (progressBar.getIndeterminateDrawable() != null) { 44 | Rect bounds = progressBar.getIndeterminateDrawable().copyBounds(); 45 | drawable.setBounds(bounds); 46 | progressBar.setProgressDrawable(drawable); 47 | } else { 48 | progressBar.setProgressDrawable(drawable); 49 | int width = progressBar.getWidth(); 50 | int height = progressBar.getHeight(); 51 | 52 | ThemeUtils.invokeMethod(progressBar, "updateDrawableBounds", width, height); 53 | } 54 | 55 | ThemeUtils.invokeMethod(progressBar, "startAnimation"); 56 | } 57 | 58 | public static void setIndeterminateDrawable(ProgressBar progressBar, @DrawableRes int drawableResId) { 59 | if (progressBar == null) { 60 | return; 61 | } 62 | 63 | Drawable drawable = getDrawable(progressBar, drawableResId); 64 | 65 | if (progressBar.getIndeterminateDrawable() != null) { 66 | Rect bounds = progressBar.getIndeterminateDrawable().copyBounds(); 67 | drawable.setBounds(bounds); 68 | progressBar.setIndeterminateDrawable(drawable); 69 | } else { 70 | progressBar.setIndeterminateDrawable(drawable); 71 | int width = progressBar.getWidth(); 72 | int height = progressBar.getHeight(); 73 | 74 | ThemeUtils.invokeMethod(progressBar, "updateDrawableBounds", width, height); 75 | } 76 | 77 | ThemeUtils.invokeMethod(progressBar, "startAnimation"); 78 | 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/SeekBarWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.AbsSeekBar; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.thumb 16 | }) 17 | public class SeekBarWidget extends ProgressBarWidget { 18 | @Override 19 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 20 | super.applyElementTheme(view, themeElementKey, resId); 21 | AbsSeekBar seekBar = (AbsSeekBar) view; 22 | switch (themeElementKey) { 23 | case android.R.attr.thumb: 24 | setThumb(seekBar, resId); 25 | break; 26 | } 27 | } 28 | 29 | public static void setThumb(AbsSeekBar seekBar, @DrawableRes int drawableResId) { 30 | if (seekBar == null) { 31 | return; 32 | } 33 | 34 | seekBar.setThumb(getDrawable(seekBar, drawableResId)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/TextViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.graphics.drawable.Drawable; 4 | import androidx.annotation.AnyRes; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.ColorRes; 7 | import androidx.annotation.DrawableRes; 8 | import android.util.Log; 9 | import android.view.View; 10 | import android.widget.TextView; 11 | 12 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 13 | 14 | /** 15 | * Created by leonhover on 16-9-27. 16 | */ 17 | @MultiThemeAttrs({ 18 | android.R.attr.textColor, 19 | android.R.attr.textColorHint, 20 | android.R.attr.textColorLink, 21 | android.R.attr.drawableBottom, 22 | android.R.attr.drawableTop, 23 | android.R.attr.drawableLeft, 24 | android.R.attr.drawableRight, 25 | }) 26 | public class TextViewWidget extends ViewWidget { 27 | 28 | @Override 29 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 30 | super.applyElementTheme(view, themeElementKey, resId); 31 | TextView textView = (TextView) view; 32 | switch (themeElementKey) { 33 | case android.R.attr.textColor: 34 | setTextColor(textView, resId); 35 | break; 36 | case android.R.attr.textColorHint: 37 | setTextColorHint(textView, resId); 38 | break; 39 | case android.R.attr.textColorLink: 40 | setTextColorLink(textView, resId); 41 | break; 42 | case android.R.attr.drawableBottom: 43 | setDrawableBottom(textView, resId); 44 | break; 45 | case android.R.attr.drawableTop: 46 | setDrawableTop(textView, resId); 47 | break; 48 | case android.R.attr.drawableLeft: 49 | setDrawableLeft(textView, resId); 50 | break; 51 | case android.R.attr.drawableRight: 52 | setDrawableRight(textView, resId); 53 | break; 54 | default: 55 | } 56 | } 57 | 58 | public static void setTextColor(TextView textView, @ColorRes int colorResId) { 59 | if (textView == null) { 60 | return; 61 | } 62 | Log.d(TAG, "setTextColor textColor:"+colorResId); 63 | textView.setTextColor(getColorStateList(textView, colorResId)); 64 | } 65 | 66 | public static void setTextColorLink(TextView textView, @ColorRes int colorResId) { 67 | if (textView == null) { 68 | return; 69 | } 70 | 71 | textView.setLinkTextColor(getColorStateList(textView, colorResId)); 72 | } 73 | 74 | public static void setTextColorHint(TextView textView, @ColorRes int colorResId) { 75 | if (textView == null) { 76 | return; 77 | } 78 | 79 | textView.setHintTextColor(getColorStateList(textView, colorResId)); 80 | } 81 | 82 | public static void setDrawableBottom(TextView textView, @DrawableRes int drawableResId) { 83 | if (textView == null) { 84 | return; 85 | } 86 | 87 | Drawable drawable = getDrawable(textView, drawableResId); 88 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 89 | textView.setCompoundDrawables(null, null, null, drawable); 90 | } 91 | 92 | public static void setDrawableLeft(TextView textView, @DrawableRes int drawableResId) { 93 | if (textView == null) { 94 | return; 95 | } 96 | 97 | Drawable drawable = getDrawable(textView, drawableResId); 98 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 99 | textView.setCompoundDrawables(drawable, null, null, null); 100 | } 101 | 102 | public static void setDrawableRight(TextView textView, @DrawableRes int drawableResId) { 103 | if (textView == null) { 104 | return; 105 | } 106 | 107 | Drawable drawable = getDrawable(textView, drawableResId); 108 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 109 | textView.setCompoundDrawables(null, null, drawable, null); 110 | } 111 | 112 | public static void setDrawableTop(TextView textView, @DrawableRes int drawableResId) { 113 | if (textView == null) { 114 | return; 115 | } 116 | 117 | Drawable drawable = getDrawable(textView, drawableResId); 118 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 119 | textView.setCompoundDrawables(null, drawable, null, null); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/ToolBarWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.graphics.drawable.Drawable; 4 | import androidx.annotation.AnyRes; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.ColorRes; 7 | import androidx.annotation.DrawableRes; 8 | import androidx.appcompat.widget.Toolbar; 9 | import android.view.View; 10 | 11 | import io.github.leonhover.theme.R; 12 | 13 | /** 14 | * Created by wangzongliang on 2016/10/22. 15 | */ 16 | public class ToolBarWidget extends ViewWidget { 17 | 18 | @Override 19 | protected void initializeLibraryElements() { 20 | super.initializeLibraryElements(); 21 | addThemeElementKey(R.attr.logo); 22 | addThemeElementKey(R.attr.navigationIcon); 23 | addThemeElementKey(R.attr.subtitleTextColor); 24 | addThemeElementKey(R.attr.titleTextColor); 25 | } 26 | 27 | @Override 28 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 29 | super.applyElementTheme(view, themeElementKey, resId); 30 | Toolbar toolbar = (Toolbar) view; 31 | if (android.R.attr.logo == themeElementKey) { 32 | setLogo(toolbar, resId); 33 | } else if (R.attr.subtitleTextColor == themeElementKey) { 34 | setSubTitleTextColor(toolbar, resId); 35 | } else if (R.attr.navigationIcon == themeElementKey) { 36 | setNavigationIcon(toolbar, resId); 37 | } else if (R.attr.titleTextColor == themeElementKey) { 38 | setTitleTextColor(toolbar, resId); 39 | } 40 | } 41 | 42 | public static void setLogo(Toolbar toolBar, @DrawableRes int drawableResId) { 43 | if (toolBar == null) { 44 | return; 45 | } 46 | 47 | Drawable logoDrawable = getDrawable(toolBar, drawableResId); 48 | toolBar.setLogo(logoDrawable); 49 | } 50 | 51 | public static void setNavigationIcon(Toolbar toolBar, @DrawableRes int drawableResId) { 52 | if (toolBar == null) { 53 | return; 54 | } 55 | 56 | Drawable iconDrawable = getDrawable(toolBar, drawableResId); 57 | toolBar.setNavigationIcon(iconDrawable); 58 | } 59 | 60 | public static void setTitleTextColor(Toolbar toolBar, @ColorRes int colorResId) { 61 | 62 | if (toolBar == null) { 63 | return; 64 | } 65 | 66 | toolBar.setTitleTextColor(getColor(toolBar, colorResId)); 67 | } 68 | 69 | public static void setSubTitleTextColor(Toolbar toolBar, @ColorRes int colorResId) { 70 | 71 | if (toolBar == null) { 72 | return; 73 | } 74 | 75 | toolBar.setSubtitleTextColor(getColor(toolBar, colorResId)); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/ViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.graphics.drawable.Drawable; 4 | import androidx.annotation.AnyRes; 5 | import androidx.annotation.AttrRes; 6 | import android.view.View; 7 | 8 | import io.github.leonhover.theme.ThemeUtils; 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.background 16 | }) 17 | public class ViewWidget extends AbstractThemeWidget { 18 | 19 | @Override 20 | protected void initializeLibraryElements() { 21 | super.initializeLibraryElements(); 22 | } 23 | 24 | @Override 25 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 26 | super.applyElementTheme(view, themeElementKey, resId); 27 | switch (themeElementKey) { 28 | case android.R.attr.background: 29 | setBackground(view, resId); 30 | break; 31 | } 32 | } 33 | 34 | @SuppressWarnings("NewApi") 35 | public static void setBackground(View view, @AnyRes int resId) { 36 | if (view == null) { 37 | return; 38 | } 39 | 40 | Drawable background = getDrawable(view, resId); 41 | 42 | if (ThemeUtils.IS_JELLY_BEAN) { 43 | view.setBackground(background); 44 | } else { 45 | view.setBackgroundDrawable(background); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/test/java/io/github/leonhover/theme/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /AndroidMultiTheme/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | defaultConfig { 6 | applicationId "io.github.leonhover.theme.samples" 7 | minSdkVersion 19 8 | targetSdkVersion 29 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { 24 | exclude group: 'com.android.support', module: 'support-annotations' 25 | }) 26 | implementation project(':android-multi-theme') 27 | implementation 'androidx.appcompat:appcompat:1.1.0' 28 | implementation 'com.google.android.material:material:1.1.0' 29 | testImplementation 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/wangzongliang/Development/Tools/Android/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/androidTest/java/io/github/leonhover/theme/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.github.leonhover.theme", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/MainActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.content.res.Configuration; 4 | import android.os.Bundle; 5 | import androidx.annotation.NonNull; 6 | import androidx.appcompat.app.AppCompatDelegate; 7 | import androidx.recyclerview.widget.LinearLayoutManager; 8 | import androidx.recyclerview.widget.RecyclerView; 9 | import androidx.appcompat.widget.Toolbar; 10 | import android.util.Log; 11 | import android.view.Menu; 12 | import android.view.MenuItem; 13 | import android.view.View; 14 | import android.widget.ImageView; 15 | import android.widget.LinearLayout; 16 | import android.widget.TextView; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import io.github.leonhover.theme.ActivityTheme; 22 | import io.github.leonhover.theme.DarkMode; 23 | import io.github.leonhover.theme.MultiTheme; 24 | import io.github.leonhover.theme.ThemeUtils; 25 | import io.github.leonhover.theme.base.BaseThemeActivity; 26 | 27 | public class MainActivity extends BaseThemeActivity { 28 | 29 | public static final String TAG = MainActivity.class.getSimpleName(); 30 | 31 | private TextView appDescriptionTextView; 32 | private boolean attrChanged = false; 33 | 34 | private RecyclerView recyclerView; 35 | ImageView mBackground; 36 | 37 | static { 38 | AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 39 | } 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | setContentView(R.layout.activity_main); 45 | Log.d(TAG, "isNightMode:" + ThemeUtils.isSystemDarkMode(this)); 46 | initActionBar(); 47 | 48 | appDescriptionTextView = (TextView) findViewById(R.id.app_description_text); 49 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 50 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 51 | 52 | LinearLayout linearLayout = findViewById(R.id.contaier); 53 | 54 | mBackground = new ImageView(this); 55 | MultiTheme.applySingleElementTheme(mBackground, android.R.attr.background, R.attr.sub_title_text_color); 56 | MultiTheme.applySingleElementTheme(mBackground, android.R.attr.src, R.attr.icon_launcher); 57 | linearLayout.addView(mBackground); 58 | 59 | List themeInfoList = new ArrayList<>(); 60 | themeInfoList.add(new ThemeInfo(R.drawable.blue_ic_theme, 61 | R.string.theme_blue_title, 62 | R.string.theme_blue_description 63 | )); 64 | themeInfoList.add(new ThemeInfo(R.drawable.red_ic_theme, 65 | R.string.theme_red_title, 66 | R.string.theme_red_description 67 | )); 68 | themeInfoList.add(new ThemeInfo(R.drawable.green_ic_theme, 69 | R.string.theme_green_title, 70 | R.string.theme_green_description 71 | )); 72 | themeInfoList.add(new ThemeInfo(R.drawable.yellow_ic_theme, 73 | R.string.theme_yellow_title, 74 | R.string.theme_yellow_description 75 | )); 76 | themeInfoList.add(new ThemeInfo(R.drawable.black_ic_theme, 77 | R.string.theme_Black_title, 78 | R.string.theme_Black_description 79 | )); 80 | 81 | 82 | ThemeAdapter themeAdapter = new ThemeAdapter(this); 83 | themeAdapter.setThemeInfoList(themeInfoList); 84 | recyclerView.setAdapter(themeAdapter); 85 | 86 | } 87 | 88 | private void initActionBar() { 89 | Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); 90 | this.setSupportActionBar(toolbar); 91 | } 92 | 93 | @Override 94 | public boolean onCreateOptionsMenu(Menu menu) { 95 | getMenuInflater().inflate(R.menu.dark_theme_setings, menu); 96 | return super.onCreateOptionsMenu(menu); 97 | } 98 | 99 | @Override 100 | public boolean onPrepareOptionsMenu(Menu menu) { 101 | DarkMode mode = MultiTheme.getDarkMode(); 102 | switch (mode) { 103 | case off: 104 | menu.findItem(R.id.amt_dark_theme_on).setChecked(false); 105 | menu.findItem(R.id.amt_dark_theme_off).setChecked(true); 106 | menu.findItem(R.id.amt_dark_theme_follow_system).setChecked(false); 107 | return true; 108 | case on: 109 | menu.findItem(R.id.amt_dark_theme_on).setChecked(true); 110 | menu.findItem(R.id.amt_dark_theme_off).setChecked(false); 111 | menu.findItem(R.id.amt_dark_theme_follow_system).setChecked(false); 112 | return true; 113 | case followSystem: 114 | menu.findItem(R.id.amt_dark_theme_on).setChecked(false); 115 | menu.findItem(R.id.amt_dark_theme_off).setChecked(false); 116 | menu.findItem(R.id.amt_dark_theme_follow_system).setChecked(true); 117 | return true; 118 | } 119 | return super.onPrepareOptionsMenu(menu); 120 | } 121 | 122 | @Override 123 | public boolean onOptionsItemSelected(@NonNull MenuItem item) { 124 | switch (item.getItemId()) { 125 | case R.id.amt_dark_theme_off: 126 | MultiTheme.setDarkMode(DarkMode.off); 127 | item.setChecked(true); 128 | return true; 129 | case R.id.amt_dark_theme_on: 130 | MultiTheme.setDarkMode(DarkMode.on); 131 | item.setChecked(true); 132 | return true; 133 | case R.id.amt_dark_theme_follow_system: 134 | MultiTheme.setDarkMode(DarkMode.followSystem); 135 | item.setChecked(true); 136 | return true; 137 | } 138 | return super.onOptionsItemSelected(item); 139 | } 140 | 141 | @Override 142 | protected void configTheme(ActivityTheme activityTheme) { 143 | activityTheme.setThemes(4, new int[]{R.style.AppTheme_Blue, R.style.AppTheme_Red, R.style.AppTheme_Green, R.style.AppTheme_Yellow, R.style.AppTheme_Black}); 144 | activityTheme.setStatusBarColorAttrRes(R.attr.colorPrimary); 145 | activityTheme.setSupportMenuItemThemeEnable(true); 146 | } 147 | 148 | public void clickButton(View view) { 149 | attrChanged = !attrChanged; 150 | int textColorAttrId = attrChanged ? R.attr.sub_title_text_color : R.attr.title_text_color; 151 | MultiTheme.applySingleElementTheme(appDescriptionTextView, android.R.attr.textColor, textColorAttrId); 152 | } 153 | 154 | @Override 155 | public void onConfigurationChanged(Configuration newConfig) { 156 | super.onConfigurationChanged(newConfig); 157 | Log.d(TAG, "onConfigurationChanged uiMode:" + newConfig.uiMode); 158 | switch (newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK) { 159 | case Configuration.UI_MODE_NIGHT_YES: 160 | Log.d(TAG, "UI_MODE_NIGHT_YES"); 161 | break; 162 | case Configuration.UI_MODE_NIGHT_NO: 163 | Log.d(TAG, "UI_MODE_NIGHT_NO"); 164 | break; 165 | case Configuration.UI_MODE_NIGHT_UNDEFINED: 166 | Log.d(TAG, "UI_MODE_NIGHT_UNDEFINED"); 167 | break; 168 | } 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/SampleFragment.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.os.Bundle; 4 | import androidx.annotation.NonNull; 5 | import androidx.annotation.Nullable; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import io.github.leonhover.theme.base.BaseThemeFragment; 11 | 12 | /** 13 | * Created by wangzongliang on 18-1-26. 14 | */ 15 | 16 | public class SampleFragment extends BaseThemeFragment { 17 | 18 | @Nullable 19 | @Override 20 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 21 | return inflater.inflate(R.layout.frag_sample, container, false); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/ThemeAdapter.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.content.Context; 4 | import androidx.recyclerview.widget.RecyclerView; 5 | import android.util.Log; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | 11 | import java.util.List; 12 | 13 | import io.github.leonhover.theme.MultiTheme; 14 | import io.github.leonhover.theme.base.widget.CoverImageView; 15 | 16 | /** 17 | * Created by wangzongliang on 2016/11/8. 18 | */ 19 | 20 | public class ThemeAdapter extends RecyclerView.Adapter { 21 | 22 | private static final String TAG = "ThemeAdapter"; 23 | 24 | private Context context; 25 | 26 | private List themeInfoList; 27 | 28 | public ThemeAdapter(Context context) { 29 | this.context = context; 30 | } 31 | 32 | public void setThemeInfoList(List themeInfoList) { 33 | this.themeInfoList = themeInfoList; 34 | } 35 | 36 | @Override 37 | public ThemeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 38 | View itemView = LayoutInflater.from(context).inflate(R.layout.item_theme_adpater, null); 39 | return new ThemeViewHolder(itemView); 40 | } 41 | 42 | @Override 43 | public void onBindViewHolder(ThemeViewHolder holder, int position) { 44 | Log.d(TAG, "onBindViewHolder position=" + position); 45 | ThemeInfo themeInfo = this.themeInfoList.get(position); 46 | holder.coverImageView.setImageResource(themeInfo.imgRes); 47 | holder.themeTitle.setText(themeInfo.titleRes); 48 | holder.themeDescription.setText(themeInfo.descriptionRes); 49 | } 50 | 51 | @Override 52 | public void onViewAttachedToWindow(ThemeViewHolder holder) { 53 | super.onViewAttachedToWindow(holder); 54 | MultiTheme.applyTheme(holder.itemView); 55 | } 56 | 57 | @Override 58 | public int getItemCount() { 59 | return this.themeInfoList == null ? 0 : this.themeInfoList.size(); 60 | } 61 | 62 | protected static class ThemeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 63 | 64 | public CoverImageView coverImageView; 65 | public TextView themeTitle; 66 | public TextView themeDescription; 67 | 68 | public ThemeViewHolder(View itemView) { 69 | super(itemView); 70 | coverImageView = (CoverImageView) itemView.findViewById(R.id.theme_img); 71 | themeTitle = (TextView) itemView.findViewById(R.id.theme_title); 72 | themeDescription = (TextView) itemView.findViewById(R.id.theme_description); 73 | coverImageView.setOnClickListener(this); 74 | themeTitle.setOnClickListener(this); 75 | themeDescription.setOnClickListener(this); 76 | } 77 | 78 | 79 | @Override 80 | public void onClick(View v) { 81 | //按照列表顺序的主题Index= position 82 | MultiTheme.setAppTheme(this.getAdapterPosition()); 83 | } 84 | } 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/ThemeApplication.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.app.Application; 4 | 5 | import io.github.leonhover.theme.DarkMode; 6 | import io.github.leonhover.theme.MultiTheme; 7 | 8 | /** 9 | * Created by wangzongliang on 2016/10/19. 10 | */ 11 | 12 | public class ThemeApplication extends Application { 13 | 14 | static Application sApp; 15 | 16 | @Override 17 | public void onCreate() { 18 | super.onCreate(); 19 | sApp = this; 20 | initTheme(); 21 | } 22 | 23 | private void initTheme() { 24 | MultiTheme.init(this); 25 | MultiTheme.enableDebug(); 26 | MultiTheme.setDefaultAppTheme(0); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/ThemeInfo.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | /** 4 | * Created by wangzongliang on 2016/11/8. 5 | */ 6 | 7 | public class ThemeInfo { 8 | public int imgRes; 9 | public int titleRes; 10 | public int descriptionRes; 11 | 12 | public ThemeInfo(int imgRes, int titleRes, int descriptionRes) { 13 | this.imgRes = imgRes; 14 | this.titleRes = titleRes; 15 | this.descriptionRes = descriptionRes; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_progress_horizontal.xml: -------------------------------------------------------------------------------- 1 | 15 | 16 | 17 | 20 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 34 | 35 | 36 | 37 | 38 | 39 | 42 | 43 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/blue_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/blue_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/blue_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/green_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/green_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/green_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_progress_horizontal.xml: -------------------------------------------------------------------------------- 1 | 15 | 16 | 17 | 20 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 34 | 35 | 36 | 37 | 38 | 39 | 42 | 43 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/yellow_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/yellow_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/yellow_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 19 | 20 | 27 | 28 | 33 | 34 | 39 | 40 | 45 | 46 | 54 | 55 | 56 | 57 | 65 | 66 |