├── sample ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── 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 │ │ │ ├── strings.xml │ │ │ ├── dimens.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── values-night │ │ │ ├── strings.xml │ │ │ ├── dimens.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ └── layout │ │ │ └── activity_main.xml │ │ ├── java │ │ └── cn │ │ │ └── refactor │ │ │ └── dntogglebutton │ │ │ ├── SampleApplication.java │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── library ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ └── attrs.xml │ │ └── drawable │ │ │ ├── ic_day.png │ │ │ └── ic_night.png │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── cn │ │ └── refactor │ │ └── library │ │ └── DNToggleButton.java ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── art └── DNToggleButton.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | *.iml -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | *.iml -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':library' 2 | -------------------------------------------------------------------------------- /art/DNToggleButton.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/art/DNToggleButton.gif -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /library/src/main/res/drawable/ic_day.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/library/src/main/res/drawable/ic_day.png -------------------------------------------------------------------------------- /library/src/main/res/drawable/ic_night.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/library/src/main/res/drawable/ic_night.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyxialm/DNToggleButton/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DNToggleButton 3 | Theme [Day Mode] 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/values-night/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DNToggleButton 3 | Theme [Night Mode] 4 | 5 | -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values-night/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:34:03 PDT 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #606060 7 | 8 | -------------------------------------------------------------------------------- /sample/src/main/res/values-night/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #FFFFFF 7 | 8 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | *.DS_Store 3 | 4 | # Gradle files 5 | build/ 6 | .gradle/ 7 | */build/ 8 | 9 | # IDEA 10 | *.iml 11 | .idea/ 12 | 13 | # Built application files 14 | *.apk 15 | *.ap_ 16 | 17 | # Files for the Dalvik VM 18 | *.dex 19 | 20 | # Java class files 21 | *.class 22 | 23 | # Local configuration file (sdk path, etc) 24 | local.properties 25 | 26 | # Log Files 27 | *.log -------------------------------------------------------------------------------- /sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/src/main/res/values-night/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/refactor/dntogglebutton/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package cn.refactor.dntogglebutton; 2 | 3 | import android.app.Application; 4 | import android.support.v7.app.AppCompatDelegate; 5 | 6 | /** 7 | * 作者 : andy 8 | * 日期 : 16/3/6 14:48 9 | * 邮箱 : andyxialm@gmail.com 10 | * 描述 : 11 | */ 12 | public class SampleApplication extends Application { 13 | 14 | @Override 15 | public void onCreate() { 16 | super.onCreate(); 17 | AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | minSdkVersion 14 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | testCompile 'junit:junit:4.12' 24 | compile 'com.android.support:appcompat-v7:23.1.1' 25 | } 26 | -------------------------------------------------------------------------------- /sample/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 /Users/andy/Library/Android/sdk/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 | -------------------------------------------------------------------------------- /library/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 /Users/andy/Library/Android/sdk/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 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "cn.refactor.dntogglebutton" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:23.2.0' 25 | compile project(':library') 26 | compile 'com.android.support:design:23.2.0' 27 | } 28 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 22 | 23 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DNToggleButton 2 | 3 | A custom toggle button for changing day/night theme. 4 | 5 | ![](https://github.com/andyxialm/DNToggleButton/blob/master/art/DNToggleButton.gif?raw=true) 6 | 7 | ### Sample 8 | [Download Demo APK](http://fir.im/n57c) 9 | 10 | ### Features 11 | + set animation duration 12 | 13 | `dntogglebtn:duration="250"` 14 | 15 | ### Usage 16 | 17 | ##### 1. Edit your layout XML: 18 | 19 | ~~~ xml 20 | 26 | ~~~ 27 | 28 | ##### 2. setChecked 29 | 30 | mToggleBtn.setChecked(boolean checked); 31 | mToggleBtn.setChecked(boolean checked, boolean ainimate); 32 | 33 | 34 | ##### 3. set Callback 35 | ```java 36 | 37 | mToggleBtn.setOnCheckedChangeListener(new DNToggleButton.OnCheckedChangeListener() { 38 | @Override 39 | public void onCheckedChanged(DNToggleButton toggleButton, boolean isChecked) { 40 | 41 | } 42 | }); 43 | ``` 44 | 45 | 46 | ### License 47 | 48 | Copyright 2015, 2016 andy 49 | 50 | Licensed under the Apache License, Version 2.0 (the "License"); 51 | you may not use this file except in compliance with the License. 52 | You may obtain a copy of the License at 53 | 54 | http://www.apache.org/licenses/LICENSE-2.0 55 | 56 | Unless required by applicable law or agreed to in writing, software 57 | distributed under the License is distributed on an "AS IS" BASIS, 58 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 59 | See the License for the specific language governing permissions and 60 | limitations under the License. 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/refactor/dntogglebutton/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.refactor.dntogglebutton; 2 | 3 | import android.graphics.drawable.Drawable; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.app.AppCompatDelegate; 7 | import android.view.View; 8 | import android.widget.TextView; 9 | 10 | import cn.refactor.library.DNToggleButton; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | private TextView mTextView; 15 | private DNToggleButton mToggleBtn; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | 22 | // TextView 23 | int resId = (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) 24 | ? R.drawable.ic_night : R.drawable.ic_day; 25 | Drawable drawable= getResources().getDrawable(resId); 26 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 27 | 28 | mTextView = (TextView) findViewById(R.id.tv); 29 | mTextView.setCompoundDrawables(drawable, null, null, null); 30 | mTextView.setOnClickListener(new View.OnClickListener() { 31 | @Override 32 | public void onClick(View v) { 33 | mToggleBtn.setChecked(!mToggleBtn.isChecked(), true); 34 | } 35 | }); 36 | 37 | // ToggleButton 38 | mToggleBtn = (DNToggleButton) findViewById(R.id.dnBtn); 39 | mToggleBtn.setChecked(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO); 40 | mToggleBtn.setOnCheckedChangeListener(new DNToggleButton.OnCheckedChangeListener() { 41 | @Override 42 | public void onCheckedChanged(DNToggleButton toggleButton, boolean isChecked) { 43 | 44 | // filter recreate 45 | int curMode = isChecked ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES; 46 | if (AppCompatDelegate.getDefaultNightMode() == curMode) { 47 | return; 48 | } 49 | 50 | getDelegate().setLocalNightMode(isChecked ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES); 51 | AppCompatDelegate.setDefaultNightMode(isChecked ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES); 52 | recreate(); 53 | } 54 | }); 55 | 56 | } 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /library/src/main/java/cn/refactor/library/DNToggleButton.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015, 2016 andy 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cn.refactor.library; 18 | 19 | import android.animation.Animator; 20 | import android.animation.ValueAnimator; 21 | import android.annotation.TargetApi; 22 | import android.content.Context; 23 | import android.content.res.TypedArray; 24 | import android.graphics.Canvas; 25 | import android.graphics.Color; 26 | import android.graphics.Paint; 27 | import android.graphics.Point; 28 | import android.graphics.RectF; 29 | import android.os.Build; 30 | import android.os.Bundle; 31 | import android.os.Parcelable; 32 | import android.util.AttributeSet; 33 | import android.view.View; 34 | import android.view.animation.AccelerateDecelerateInterpolator; 35 | import android.widget.Checkable; 36 | 37 | /* 38 | * 作者 : andy 39 | * 日期 : 16/1/30 16:45 40 | * 邮箱 : andyxialm@gmail.com 41 | * 描述 : Day/Night Toggle Button 42 | */ 43 | public class DNToggleButton extends View implements Checkable { 44 | 45 | private static final String KEY_INSTANCE_STATE = "KEY_INSTANCE_STATE"; 46 | private static final int DEFAULT_DURATION = 350; 47 | 48 | private static final int DEFAULT_WIDTH = 80; 49 | private static final int DEFAULT_HEIGHT = 40; 50 | private static final int DEFAULT_FLOOR_STROKE_WIDTH = 3; 51 | 52 | private static final int COLOR_CHECKED_FLOOR = Color.parseColor("#8EDCFA"); 53 | private static final int COLOR_UNCHECKED_FLOOR = Color.parseColor("#2E3235"); 54 | private static final int COLOR_CHECKED_BALL = Color.parseColor("#FEDA5A"); 55 | private static final int COLOR_UNCHECKED_BALL = Color.parseColor("#FFFFFF"); 56 | private static final int COLOR_CHECKED_BALL_STROKE = Color.parseColor("#D9B939"); 57 | private static final int COLOR_UNCHECKED_BALL_STROKE = Color.parseColor("#DCDEBB"); 58 | private static final int COLOR_CHECKED_BALL_SHADOW = Color.parseColor("#D9B939"); 59 | private static final int COLOR_UNCHECKED_BALL_SHADOW = Color.parseColor("#DCE2BB"); 60 | private static final int COLOR_CHECKED_FLOOR_STROKE = Color.parseColor("#71B6CE"); 61 | private static final int COLOR_UNCHECKED_FLOOR_STROKE = Color.parseColor("#000000"); 62 | 63 | private int mDuration; 64 | private int mBallRadius; 65 | private int mFloorRadius; 66 | 67 | private float mProgress; 68 | private boolean mChecked; 69 | private RectF mFloorRect; 70 | private RectF mBallRect; // The shadow follow with the ball 71 | 72 | private Point mBallCenterPoint; 73 | private Paint mBallPaint, mFloorPaint, mShadowPaint, mCircleStrokePaint; 74 | 75 | private ValueAnimator mAnimator; 76 | private OnCheckedChangeListener mListener; 77 | 78 | public DNToggleButton(Context context) { 79 | this(context, null); 80 | } 81 | 82 | public DNToggleButton(Context context, AttributeSet attrs) { 83 | this(context, attrs, 0); 84 | } 85 | 86 | public DNToggleButton(Context context, AttributeSet attrs, int defStyleAttr) { 87 | super(context, attrs, defStyleAttr); 88 | init(attrs); 89 | } 90 | 91 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 92 | public DNToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 93 | super(context, attrs, defStyleAttr, defStyleRes); 94 | init(attrs); 95 | } 96 | 97 | @Override 98 | protected Parcelable onSaveInstanceState() { 99 | Bundle bundle = new Bundle(); 100 | bundle.putParcelable(KEY_INSTANCE_STATE, super.onSaveInstanceState()); 101 | bundle.putBoolean(KEY_INSTANCE_STATE, isChecked()); 102 | return bundle; 103 | } 104 | 105 | @Override 106 | protected void onRestoreInstanceState(Parcelable state) { 107 | if (state instanceof Bundle) { 108 | Bundle bundle = (Bundle) state; 109 | boolean isChecked = bundle.getBoolean(KEY_INSTANCE_STATE); 110 | setChecked(isChecked); 111 | super.onRestoreInstanceState(bundle.getParcelable(KEY_INSTANCE_STATE)); 112 | return; 113 | } 114 | super.onRestoreInstanceState(state); 115 | } 116 | 117 | private void init(AttributeSet attrs) { 118 | TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.DNToggleButton); 119 | mDuration = ta.getInt(R.styleable.DNToggleButton_duration, DEFAULT_DURATION); 120 | ta.recycle(); 121 | 122 | initValues(); 123 | setOnClickListener(new OnClickListener() { 124 | @Override 125 | public void onClick(View v) { 126 | if (mAnimator != null && mAnimator.isRunning()) { 127 | return; 128 | } 129 | setChecked(!isChecked(), true); 130 | } 131 | }); 132 | } 133 | 134 | private void initValues() { 135 | mBallPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 136 | mBallPaint.setStyle(Paint.Style.FILL_AND_STROKE); 137 | mBallPaint.setStrokeCap(Paint.Cap.ROUND); 138 | mBallPaint.setStrokeWidth(3); 139 | mBallPaint.setColor(COLOR_UNCHECKED_BALL); 140 | 141 | mFloorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 142 | mFloorPaint.setStyle(Paint.Style.FILL_AND_STROKE); 143 | mFloorPaint.setStrokeCap(Paint.Cap.ROUND); 144 | mFloorPaint.setColor(COLOR_UNCHECKED_FLOOR); 145 | 146 | mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 147 | mShadowPaint.setStyle(Paint.Style.FILL); 148 | mShadowPaint.setColor(COLOR_UNCHECKED_BALL_SHADOW); 149 | 150 | mCircleStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 151 | mCircleStrokePaint.setStyle(Paint.Style.STROKE); 152 | mCircleStrokePaint.setColor(COLOR_UNCHECKED_BALL_STROKE); 153 | 154 | mBallCenterPoint = new Point(); 155 | } 156 | 157 | private void startCheckedAnim() { 158 | if (mAnimator != null) { 159 | mAnimator.cancel(); 160 | mAnimator = null; 161 | } 162 | mAnimator = ValueAnimator.ofFloat(0f, 1.0f); 163 | mAnimator.setDuration(mDuration); 164 | mAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); 165 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 166 | @Override 167 | public void onAnimationUpdate(ValueAnimator animation) { 168 | mProgress = (float) animation.getAnimatedValue(); 169 | mBallCenterPoint.x = (int) (((mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH)) 170 | + (getMeasuredWidth() - 2 * (mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH))) * mProgress)); 171 | postInvalidate(); 172 | } 173 | }); 174 | mAnimator.addListener(new Animator.AnimatorListener() { 175 | @Override 176 | public void onAnimationStart(Animator animation) { 177 | } 178 | 179 | @Override 180 | public void onAnimationEnd(Animator animation) { 181 | if (mListener != null) { 182 | mListener.onCheckedChanged(DNToggleButton.this, isChecked()); 183 | } 184 | } 185 | 186 | @Override 187 | public void onAnimationCancel(Animator animation) { 188 | } 189 | 190 | @Override 191 | public void onAnimationRepeat(Animator animation) { 192 | } 193 | }); 194 | mAnimator.start(); 195 | 196 | } 197 | 198 | private void startUnCheckedAnim() { 199 | if (mAnimator != null) { 200 | mAnimator.cancel(); 201 | mAnimator = null; 202 | } 203 | mAnimator = ValueAnimator.ofFloat(1.0f, 0f); 204 | mAnimator.setDuration(mDuration); 205 | mAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); 206 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 207 | @Override 208 | public void onAnimationUpdate(ValueAnimator animation) { 209 | mProgress = (float) animation.getAnimatedValue(); 210 | mBallCenterPoint.x = (int) ((mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH)) 211 | + (getMeasuredWidth() - 2 * (mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH))) * mProgress); 212 | postInvalidate(); 213 | } 214 | }); 215 | mAnimator.addListener(new Animator.AnimatorListener() { 216 | @Override 217 | public void onAnimationStart(Animator animation) { 218 | } 219 | 220 | @Override 221 | public void onAnimationEnd(Animator animation) { 222 | if (mListener != null) { 223 | mListener.onCheckedChanged(DNToggleButton.this, isChecked()); 224 | } 225 | } 226 | 227 | @Override 228 | public void onAnimationCancel(Animator animation) { 229 | } 230 | 231 | @Override 232 | public void onAnimationRepeat(Animator animation) { 233 | } 234 | }); 235 | mAnimator.start(); 236 | } 237 | 238 | @Override 239 | public void setChecked(boolean checked) { 240 | mChecked = checked; 241 | mProgress = checked ? 1.0f : 0.0f; 242 | mBallCenterPoint.x = (int) ((mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH)) 243 | + (getMeasuredWidth() - 2 * (mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH))) * mProgress); 244 | invalidate(); 245 | 246 | if (mListener != null) { 247 | mListener.onCheckedChanged(DNToggleButton.this, isChecked()); 248 | } 249 | } 250 | 251 | public void setChecked(boolean checked, boolean animate) { 252 | if (animate) { 253 | mChecked = checked; 254 | if (checked) { 255 | startCheckedAnim(); 256 | } else { 257 | startUnCheckedAnim(); 258 | } 259 | } else { 260 | this.setChecked(checked); 261 | } 262 | } 263 | 264 | @Override 265 | public boolean isChecked() { 266 | return mChecked; 267 | } 268 | 269 | @Override 270 | public void toggle() { 271 | 272 | } 273 | 274 | @Override 275 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 276 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 277 | int width = measureSize(dp2px(DEFAULT_WIDTH), widthMeasureSpec); 278 | int height = measureSize(dp2px(DEFAULT_HEIGHT), heightMeasureSpec); 279 | setMeasuredDimension(width, height); 280 | } 281 | 282 | @Override 283 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 284 | super.onSizeChanged(w, h, oldw, oldh); 285 | // initialization 286 | mBallRadius = (int) ((getMeasuredHeight() - dp2px(6)) * 0.5) - dp2px(DEFAULT_FLOOR_STROKE_WIDTH); 287 | mFloorRadius = (int) (Math.min(getMeasuredWidth(), getMeasuredHeight()) * 0.5); 288 | 289 | // ball initialization position 290 | mProgress = isChecked() ? 1.0f : 0.0f; 291 | 292 | mBallCenterPoint.x = (int) ((mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH)) 293 | + (getMeasuredWidth() - 2 * (mBallRadius + 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH))) * mProgress); 294 | mBallCenterPoint.y = (int) (getMeasuredHeight() * 0.5); 295 | 296 | mFloorRect = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()); 297 | 298 | float ballRectLeft = 5.0f + dp2px(DEFAULT_FLOOR_STROKE_WIDTH); 299 | float ballRectTop = dp2px(3) + dp2px(DEFAULT_FLOOR_STROKE_WIDTH); 300 | float ballRectRight = 5 + dp2px(DEFAULT_FLOOR_STROKE_WIDTH) + 2 * mBallRadius; 301 | float ballRectBottom = getMeasuredHeight() - dp2px(3) - dp2px(DEFAULT_FLOOR_STROKE_WIDTH); 302 | mBallRect = new RectF(ballRectLeft, ballRectTop, ballRectRight, ballRectBottom); 303 | } 304 | 305 | @Override 306 | protected void onDraw(Canvas canvas) { 307 | drawFloor(canvas); 308 | drawBall(canvas); 309 | } 310 | 311 | /** 312 | * draw floor 313 | * @param canvas canvas 314 | */ 315 | private void drawFloor(Canvas canvas) { 316 | float floorProgress; 317 | if (isChecked()) { 318 | floorProgress = (mProgress * 4) < 1.0f ? (mProgress * 4) : 1.0f; 319 | } else { 320 | floorProgress = mProgress > 0.75f ? (1.0f - (1.0f - mProgress) * 4) : 0f; 321 | } 322 | mFloorPaint.setColor(isChecked() ? COLOR_CHECKED_FLOOR_STROKE : COLOR_UNCHECKED_FLOOR_STROKE); 323 | mFloorRect = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()); 324 | canvas.drawRoundRect(mFloorRect, mFloorRadius, mFloorRadius, mFloorPaint); 325 | 326 | mFloorPaint.setColor(getGradientColor(COLOR_UNCHECKED_FLOOR, COLOR_CHECKED_FLOOR, floorProgress)); 327 | mFloorRect.set(dp2px(DEFAULT_FLOOR_STROKE_WIDTH), dp2px(DEFAULT_FLOOR_STROKE_WIDTH), 328 | getMeasuredWidth() - dp2px(DEFAULT_FLOOR_STROKE_WIDTH), getMeasuredHeight() - dp2px(DEFAULT_FLOOR_STROKE_WIDTH)); 329 | 330 | int radius = (int) (mFloorRadius - dp2px(DEFAULT_FLOOR_STROKE_WIDTH) * 0.5); 331 | canvas.drawRoundRect(mFloorRect, radius, radius, mFloorPaint); 332 | } 333 | 334 | /** 335 | * draw moving ball 336 | * @param canvas canvas 337 | */ 338 | private void drawBall(Canvas canvas) { 339 | 340 | // The shadow follow the ball 341 | float ballRectLeft; 342 | float ballRectRight; 343 | if (isChecked()) { 344 | ballRectLeft = (float) ((mBallCenterPoint.x - mBallRadius) - (1 - 2 * Math.abs(0.5f - mProgress)) * 1.2 * mBallRadius); 345 | ballRectLeft = ballRectLeft < 5.0f + dp2px(DEFAULT_FLOOR_STROKE_WIDTH) ? 5.0f + dp2px(DEFAULT_FLOOR_STROKE_WIDTH) : ballRectLeft; 346 | ballRectRight = mBallCenterPoint.x + mBallRadius; 347 | } else { 348 | ballRectLeft = mBallCenterPoint.x - mBallRadius; 349 | ballRectRight = (float) ((mBallCenterPoint.x + mBallRadius) + (1 - 2 * Math.abs(0.5f - mProgress)) * 1.2 * mBallRadius); 350 | ballRectRight = ballRectRight > (getMeasuredWidth() - 5.0f - dp2px(DEFAULT_FLOOR_STROKE_WIDTH)) ? (getMeasuredWidth() - 5.0f - dp2px(DEFAULT_FLOOR_STROKE_WIDTH)) : ballRectRight; 351 | } 352 | mBallRect.set(ballRectLeft, mBallRect.top, ballRectRight, mBallRect.bottom); 353 | mShadowPaint.setColor(isChecked() ? COLOR_CHECKED_BALL_SHADOW : COLOR_UNCHECKED_BALL_SHADOW); 354 | canvas.drawRoundRect(mBallRect, mBallRadius, mBallRadius, mShadowPaint); 355 | 356 | // ball 357 | // stroke 358 | mCircleStrokePaint.setColor(getGradientColor(COLOR_CHECKED_BALL_STROKE, COLOR_UNCHECKED_BALL_STROKE, mProgress)); 359 | canvas.drawCircle(mBallCenterPoint.x, mBallCenterPoint.y, mBallRadius, mCircleStrokePaint); 360 | 361 | // fill area 362 | mBallPaint.setColor(getGradientColor(COLOR_UNCHECKED_BALL, COLOR_CHECKED_BALL, mProgress)); 363 | canvas.drawCircle(mBallCenterPoint.x, mBallCenterPoint.y, mBallRadius - dp2px(DEFAULT_FLOOR_STROKE_WIDTH), mBallPaint); 364 | } 365 | 366 | private int measureSize(int defaultSize, int measureSpec) { 367 | int result = 0; 368 | int specMode = MeasureSpec.getMode(measureSpec); 369 | int specSize = MeasureSpec.getSize(measureSpec); 370 | switch (specMode) { 371 | case MeasureSpec.EXACTLY: 372 | result = specSize; 373 | break; 374 | case MeasureSpec.UNSPECIFIED: 375 | result = defaultSize; 376 | break; 377 | case MeasureSpec.AT_MOST: 378 | result = Math.min(defaultSize, specSize); 379 | break; 380 | } 381 | return result; 382 | } 383 | 384 | private int dp2px(int dpValue) { 385 | final float scale = getContext().getResources().getDisplayMetrics().density; 386 | return (int) (dpValue * scale + 0.5f); 387 | } 388 | 389 | private static int getGradientColor(int startColor, int endColor, float percent) { 390 | int sr = (startColor & 0xff0000) >> 0x10; 391 | int sg = (startColor & 0xff00) >> 0x8; 392 | int sb = (startColor & 0xff); 393 | 394 | int er = (endColor & 0xff0000) >> 0x10; 395 | int eg = (endColor & 0xff00) >> 0x8; 396 | int eb = (endColor & 0xff); 397 | 398 | int cr = (int) (sr * (1 - percent) + er * percent); 399 | int cg = (int) (sg * (1 - percent) + eg * percent); 400 | int cb = (int) (sb * (1 - percent) + eb * percent); 401 | return Color.argb(0xff, cr, cg, cb); 402 | } 403 | 404 | public void setOnCheckedChangeListener(OnCheckedChangeListener l) { 405 | this.mListener = l; 406 | } 407 | 408 | public interface OnCheckedChangeListener { 409 | void onCheckedChanged(DNToggleButton toggleButton, boolean isChecked); 410 | } 411 | } 412 | --------------------------------------------------------------------------------