├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── sample
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── mazouri
│ │ └── statebutton
│ │ └── sample
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── mazouri
│ │ │ └── statebutton
│ │ │ └── sample
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable
│ │ ├── ic_lock.xml
│ │ ├── ic_lock_disable.xml
│ │ ├── ic_lock_open.xml
│ │ ├── state_button_bg_disable.xml
│ │ ├── state_button_bg_enable.xml
│ │ └── state_button_bg_selected.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── mazouri
│ └── statebutton
│ └── sample
│ └── ExampleUnitTest.java
├── screenshot
└── state_button_demo.gif
├── settings.gradle
└── statebutton
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
├── androidTest
└── java
│ └── mazouri
│ └── statebutton
│ └── ExampleInstrumentedTest.java
├── main
├── AndroidManifest.xml
├── java
│ └── mazouri
│ │ └── statebutton
│ │ ├── CircleTextProgressbar.java
│ │ └── StateButton.java
└── res
│ └── values
│ ├── attrs.xml
│ └── strings.xml
└── test
└── java
└── mazouri
└── statebutton
└── ExampleUnitTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | 1.8
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # android-state-button
2 |
3 | [](https://jitpack.io/#mazouri/android-state-button)
4 |
5 | # About
6 | android-state-button is a custom view for mutil state button.
7 |
8 | # Demo
9 |
10 |
11 |
12 | # How to use?
13 |
14 | 1.Add it in your root build.gradle at the end of repositories:
15 | ```
16 | allprojects {
17 | repositories {
18 | ...
19 | maven { url 'https://jitpack.io' }
20 | }
21 | }
22 | ```
23 | 2.Add the dependency
24 | ```
25 | dependencies {
26 | compile 'com.github.mazouri:android-state-button:1.0'
27 | }
28 | ```
29 |
30 | ```
31 |
52 | ```
53 |
54 |
55 |
56 | enabledBackground |
57 | 正常状态的背景 |
58 |
59 |
60 | enabledText |
61 | 正常状态的文字 |
62 |
63 |
64 | enabledIcon |
65 | 正常状态的图标 |
66 |
67 |
68 | enabledTextColor |
69 | 正常状态的文字颜色 |
70 |
71 |
72 |
73 |
74 | disabledBackground |
75 | disabled状态的背景 |
76 |
77 |
78 | disabledText |
79 | disabled状态的文字 |
80 |
81 |
82 | disabledIcon |
83 | disabled状态的图标 |
84 |
85 |
86 | disabledTextColor |
87 | disabled状态的文字颜色 |
88 |
89 |
90 |
91 |
92 | selectedBackground |
93 | 选择状态的背景 |
94 |
95 |
96 | selectedText |
97 | 选择状态的文字 |
98 |
99 |
100 | selectedIcon |
101 | 选择状态的图标 |
102 |
103 |
104 | selectedTextColor |
105 | 选择状态的文字颜色 |
106 |
107 |
108 |
109 |
110 |
111 | drawablePadding |
112 | 图标与文字间距 |
113 |
114 |
115 | iconTopMargin |
116 | 图标上方间距 |
117 |
118 |
119 | iconWidth |
120 | 图标宽度 |
121 |
122 |
123 | iconHeight |
124 | 图标高度 |
125 |
126 |
127 | state |
128 | StateButton默认状态 |
129 |
130 |
131 |
132 | You can also fork this project to update your own states
133 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Mar 27 18:32:36 CST 2017
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-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.mazouri.statebutton.sample"
8 | minSdkVersion 15
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
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 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.1.1'
28 | compile 'com.android.support.constraint:constraint-layout:1.0.0'
29 | testCompile 'junit:junit:4.12'
30 | compile 'com.jakewharton:butterknife:8.4.0'
31 | compile 'com.google.android:flexbox:0.3.0-alpha2'
32 |
33 | compile project(':statebutton')
34 | }
35 |
--------------------------------------------------------------------------------
/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 /home/wangdongdong/linux-enviroment/android-sdk-all/adt-bundle-linux-x86_64-20140702/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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/sample/src/androidTest/java/com/mazouri/statebutton/sample/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.mazouri.statebutton.sample;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.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("com.mazouri.statebutton.sample", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/mazouri/statebutton/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.mazouri.statebutton.sample;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Toast;
7 |
8 | import java.util.Random;
9 | import java.util.Timer;
10 | import java.util.TimerTask;
11 |
12 | import butterknife.BindView;
13 | import butterknife.OnClick;
14 | import mazouri.statebutton.StateButton;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 | StateButton stateButton;
19 | StateButton stateButton2;
20 | StateButton stateButton3;
21 | StateButton stateButton4;
22 | StateButton stateButton5;
23 | StateButton stateButton6;
24 | StateButton stateButton7;
25 | StateButton stateButton8;
26 | StateButton stateButton9;
27 | StateButton stateButton10;
28 | StateButton stateButton11;
29 | StateButton stateButton12;
30 | StateButton stateButton13;
31 | StateButton stateButton14;
32 | StateButton stateButton15;
33 | StateButton stateButton16;
34 |
35 | private Timer mTimer;
36 | private TimerTask mTimerTask;
37 |
38 | @Override
39 | protected void onCreate(Bundle savedInstanceState) {
40 | super.onCreate(savedInstanceState);
41 | setContentView(R.layout.activity_main);
42 |
43 | stateButton = (StateButton) findViewById(R.id.state_button);
44 | stateButton2 = (StateButton) findViewById(R.id.state_button2);
45 | stateButton3 = (StateButton) findViewById(R.id.state_button3);
46 | stateButton4 = (StateButton) findViewById(R.id.state_button4);
47 | stateButton5 = (StateButton) findViewById(R.id.state_button5);
48 | stateButton6 = (StateButton) findViewById(R.id.state_button6);
49 | stateButton7 = (StateButton) findViewById(R.id.state_button7);
50 | stateButton8 = (StateButton) findViewById(R.id.state_button8);
51 | stateButton9 = (StateButton) findViewById(R.id.state_button9);
52 | stateButton10 = (StateButton) findViewById(R.id.state_button10);
53 | stateButton11 = (StateButton) findViewById(R.id.state_button11);
54 | stateButton12 = (StateButton) findViewById(R.id.state_button12);
55 | stateButton13 = (StateButton) findViewById(R.id.state_button13);
56 | stateButton14 = (StateButton) findViewById(R.id.state_button14);
57 | stateButton15 = (StateButton) findViewById(R.id.state_button15);
58 | stateButton16 = (StateButton) findViewById(R.id.state_button16);
59 | setup();
60 | }
61 |
62 | private void setup() {
63 | final Random random = new Random();
64 | mTimer = new Timer();
65 |
66 | mTimerTask = new TimerTask() {
67 | @Override
68 | public void run() {
69 | runOnUiThread(new Runnable() {
70 | @Override
71 | public void run() {
72 | try {
73 | int nextInt = random.nextInt(3);
74 | StateButton.BUTTON_STATES button_states = StateButton.BUTTON_STATES.fromValue(nextInt);
75 | stateButton.setState(button_states);
76 | stateButton4.setState(button_states);
77 | stateButton7.setState(button_states);
78 | stateButton10.setState(button_states);
79 | stateButton13.setState(button_states);
80 | } catch (Exception e) {
81 | e.printStackTrace();
82 | }
83 | }
84 | });
85 | }
86 | };
87 | mTimer.schedule(mTimerTask, 200, 2000);
88 |
89 | stateButton2.setOnClickListener(new View.OnClickListener() {
90 | @Override
91 | public void onClick(View v) {
92 | changState(stateButton2);
93 | }
94 | });
95 |
96 | stateButton3.setOnClickListener(new View.OnClickListener() {
97 | @Override
98 | public void onClick(View v) {
99 | showDisableToast(stateButton3);
100 | }
101 | });
102 |
103 | stateButton5.setOnClickListener(new View.OnClickListener() {
104 | @Override
105 | public void onClick(View v) {
106 | changState(stateButton5);
107 | }
108 | });
109 | stateButton6.setOnClickListener(new View.OnClickListener() {
110 | @Override
111 | public void onClick(View v) {
112 | showDisableToast(stateButton6);
113 | }
114 | });
115 |
116 | stateButton8.setOnClickListener(new View.OnClickListener() {
117 | @Override
118 | public void onClick(View v) {
119 | changState(stateButton8);
120 | }
121 | });
122 | stateButton9.setOnClickListener(new View.OnClickListener() {
123 | @Override
124 | public void onClick(View v) {
125 | showDisableToast(stateButton9);
126 | }
127 | });
128 |
129 | stateButton11.setOnClickListener(new View.OnClickListener() {
130 | @Override
131 | public void onClick(View v) {
132 | changState(stateButton11);
133 | }
134 | });
135 | stateButton12.setOnClickListener(new View.OnClickListener() {
136 | @Override
137 | public void onClick(View v) {
138 | showDisableToast(stateButton12);
139 | }
140 | });
141 |
142 | stateButton14.setOnClickListener(new View.OnClickListener() {
143 | @Override
144 | public void onClick(View v) {
145 | changState(stateButton14);
146 | }
147 | });
148 | stateButton15.setOnClickListener(new View.OnClickListener() {
149 | @Override
150 | public void onClick(View v) {
151 | showDisableToast(stateButton15);
152 | }
153 | });
154 | stateButton16.setOnClickListener(new View.OnClickListener() {
155 | @Override
156 | public void onClick(View v) {
157 | stateButton16.setState(StateButton.BUTTON_STATES.LOADING);
158 | }
159 | });
160 | stateButton16.setCountdownProgressListener(new StateButton.OnCountdownListener() {
161 | @Override
162 | public void onProgress(int progress) {
163 | stateButton16.setProgressText(progress + "");
164 | }
165 | });
166 | }
167 |
168 | private void changState(StateButton stateButton) {
169 | if (stateButton.getState() == StateButton.BUTTON_STATES.ENABLED) {
170 | stateButton.setState(StateButton.BUTTON_STATES.SELECTED);
171 | } else if (stateButton.getState() == StateButton.BUTTON_STATES.SELECTED) {
172 | stateButton.setState(StateButton.BUTTON_STATES.ENABLED);
173 | }
174 | }
175 |
176 | private void showDisableToast(StateButton stateButton) {
177 | Toast.makeText(MainActivity.this, "current state is " + stateButton.getState().name(), Toast.LENGTH_SHORT).show();
178 | }
179 |
180 | @Override
181 | protected void onDestroy() {
182 | super.onDestroy();
183 | cancelTimer();
184 | }
185 |
186 | private void cancelTimer(){
187 | if (mTimer != null) {
188 | mTimer.cancel();
189 | mTimer = null;
190 | }
191 | if (mTimerTask != null) {
192 | mTimerTask.cancel();
193 | mTimerTask = null;
194 | }
195 | }
196 | }
197 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/ic_lock.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/ic_lock_disable.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/ic_lock_open.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/state_button_bg_disable.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/state_button_bg_enable.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/state_button_bg_selected.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
17 |
38 |
39 |
60 |
61 |
82 |
83 |
84 |
89 |
90 |
108 |
109 |
127 |
128 |
146 |
147 |
148 |
152 |
153 |
167 |
168 |
182 |
183 |
197 |
198 |
199 |
200 |
205 |
206 |
217 |
218 |
229 |
230 |
241 |
242 |
243 |
248 |
249 |
263 |
264 |
278 |
279 |
293 |
294 |
295 |
300 |
301 |
329 |
330 |
358 |
359 |
360 |
361 |
362 |
363 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #fff
8 |
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | android-state-button
3 |
4 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/sample/src/test/java/com/mazouri/statebutton/sample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.mazouri.statebutton.sample;
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 | }
--------------------------------------------------------------------------------
/screenshot/state_button_demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mazouri/android-state-button/f4f3dd2e3e20b642b6f04d2f71bae23afab687f0/screenshot/state_button_demo.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':statebutton'
2 |
--------------------------------------------------------------------------------
/statebutton/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/statebutton/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.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 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:25.1.1'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
--------------------------------------------------------------------------------
/statebutton/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/wangdongdong/linux-enviroment/android-sdk-all/adt-bundle-linux-x86_64-20140702/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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/statebutton/src/androidTest/java/mazouri/statebutton/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package mazouri.statebutton;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.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("mazouri.statebutton.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/statebutton/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/statebutton/src/main/java/mazouri/statebutton/CircleTextProgressbar.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Yan Zhenjie
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 | package mazouri.statebutton;
17 |
18 | import android.content.Context;
19 | import android.content.res.ColorStateList;
20 | import android.content.res.TypedArray;
21 | import android.graphics.Canvas;
22 | import android.graphics.Color;
23 | import android.graphics.Paint;
24 | import android.graphics.Rect;
25 | import android.graphics.RectF;
26 | import android.support.annotation.ColorInt;
27 | import android.util.AttributeSet;
28 | import android.util.Log;
29 |
30 | public class CircleTextProgressbar extends android.support.v7.widget.AppCompatTextView {
31 |
32 | /**
33 | * 外部轮廓的颜色。
34 | */
35 | private int outLineColor = Color.BLACK;
36 |
37 | /**
38 | * 外部轮廓的宽度。
39 | */
40 | private int outLineWidth = 2;
41 |
42 | /**
43 | * 内部圆的颜色。
44 | */
45 | private ColorStateList inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT);
46 | /**
47 | * 中心圆的颜色。
48 | */
49 | private int circleColor;
50 |
51 | /**
52 | * 进度条的颜色。
53 | */
54 | private int progressLineColor = Color.BLUE;
55 |
56 | private int progressLineBgColor = Color.GRAY;
57 |
58 | /**
59 | * 进度条的宽度。
60 | */
61 | private int progressLineWidth = 8;
62 |
63 | /**
64 | * 进度更新时间间隔
65 | */
66 | private int progressChangeDuration = 0;
67 |
68 | /**
69 | * 画笔。
70 | */
71 | private Paint mPaint = new Paint();
72 |
73 | /**
74 | * 进度条的矩形区域。
75 | */
76 | private RectF mArcRect = new RectF();
77 |
78 | /**
79 | * 进度。
80 | */
81 | private int progress = 100;
82 |
83 | /**
84 | * 最大进度值
85 | */
86 | private int maxProgress = 100;
87 | /**
88 | * 进度条类型。
89 | */
90 | private ProgressType mProgressType = ProgressType.COUNT_BACK;
91 | /**
92 | * 进度倒计时时间。
93 | */
94 | private long timeMillis = 3000;
95 |
96 | /**
97 | * View的显示区域。
98 | */
99 | final Rect bounds = new Rect();
100 | /**
101 | * 进度条通知。
102 | */
103 | private OnCountdownProgressListener mCountdownProgressListener;
104 | /**
105 | * Listener what。
106 | */
107 | private int listenerWhat = 0;
108 |
109 | public CircleTextProgressbar(Context context) {
110 | this(context, null);
111 | }
112 |
113 | public CircleTextProgressbar(Context context, AttributeSet attrs) {
114 | this(context, attrs, 0);
115 | }
116 |
117 | public CircleTextProgressbar(Context context, AttributeSet attrs, int defStyleAttr) {
118 | super(context, attrs, defStyleAttr);
119 | initialize(context, attrs);
120 | }
121 |
122 | /**
123 | * 初始化。
124 | *
125 | * @param context 上下文。
126 | * @param attributeSet 属性。
127 | */
128 | private void initialize(Context context, AttributeSet attributeSet) {
129 | mPaint.setAntiAlias(true);
130 | TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CircleTextProgressbar);
131 | ColorStateList colorStateList = typedArray.getColorStateList(R.styleable.CircleTextProgressbar_inCircleColor);
132 | inCircleColors = colorStateList == null ? ColorStateList.valueOf(Color.TRANSPARENT) : colorStateList;
133 | circleColor = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT);
134 |
135 | progressLineColor = typedArray.getColor(R.styleable.CircleTextProgressbar_progressLineColor, Color.TRANSPARENT);
136 | progressLineBgColor = typedArray.getColor(R.styleable.CircleTextProgressbar_progressLineBgColor, Color.GRAY);
137 | progressLineWidth = typedArray.getDimensionPixelSize(R.styleable.CircleTextProgressbar_progressLineWidth, 8);
138 | progressChangeDuration = typedArray.getInt(R.styleable.CircleTextProgressbar_progressChangeDuration, 0);
139 | maxProgress = typedArray.getInt(R.styleable.CircleTextProgressbar_maxProgress, 100);
140 | maxProgress ++; //for count_back
141 |
142 | typedArray.recycle();
143 | }
144 |
145 | /**
146 | * 设置外部轮廓的颜色。
147 | *
148 | * @param outLineColor 颜色值。
149 | */
150 | public void setOutLineColor(@ColorInt int outLineColor) {
151 | this.outLineColor = outLineColor;
152 | invalidate();
153 | }
154 |
155 | /**
156 | * 设置外部轮廓的颜色。
157 | *
158 | * @param outLineWidth 颜色值。
159 | */
160 | public void setOutLineWidth(@ColorInt int outLineWidth) {
161 | this.outLineWidth = outLineWidth;
162 | invalidate();
163 | }
164 |
165 | /**
166 | * 设置圆形的填充颜色。
167 | *
168 | * @param inCircleColor 颜色值。
169 | */
170 | public void setInCircleColor(@ColorInt int inCircleColor) {
171 | this.inCircleColors = ColorStateList.valueOf(inCircleColor);
172 | invalidate();
173 | }
174 |
175 | /**
176 | * 是否需要更新圆的颜色。
177 | */
178 | private void validateCircleColor() {
179 | int circleColorTemp = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT);
180 | if (circleColor != circleColorTemp) {
181 | circleColor = circleColorTemp;
182 | invalidate();
183 | }
184 | }
185 |
186 | /**
187 | * 设置进度条颜色。
188 | *
189 | * @param progressLineColor 颜色值。
190 | */
191 | public void setProgressColor(@ColorInt int progressLineColor) {
192 | this.progressLineColor = progressLineColor;
193 | invalidate();
194 | }
195 |
196 | /**
197 | * 设置进度条线的宽度。
198 | *
199 | * @param progressLineWidth 宽度值。
200 | */
201 | public void setProgressLineWidth(int progressLineWidth) {
202 | this.progressLineWidth = progressLineWidth;
203 | invalidate();
204 | }
205 |
206 | public void setProgressChangeDuration(int progressChangeDuration) {
207 | this.progressChangeDuration = progressChangeDuration;
208 | }
209 |
210 | public void setProgressLineColor(int progressLineColor) {
211 | this.progressLineColor = progressLineColor;
212 | }
213 |
214 | public void setProgressLineBgColor(int progressLineBgColor) {
215 | this.progressLineBgColor = progressLineBgColor;
216 | }
217 |
218 | public void setMaxProgress(int maxProgress) {
219 | this.maxProgress = ++maxProgress;
220 | }
221 |
222 | /**
223 | * 设置进度。
224 | *
225 | * @param progress 进度。
226 | */
227 | public void setProgress(int progress) {
228 | this.progress = validateProgress(progress);
229 | invalidate();
230 | }
231 |
232 | /**
233 | * 验证进度。
234 | *
235 | * @param progress 你要验证的进度值。
236 | * @return 返回真正的进度值。
237 | */
238 | private int validateProgress(int progress) {
239 | if (progress > maxProgress) {
240 | progress = maxProgress;
241 | } else if (progress < 0)
242 | progress = 0;
243 | return progress;
244 | }
245 |
246 | /**
247 | * 拿到此时的进度。
248 | *
249 | * @return 进度值,最大100,最小0。
250 | */
251 | public int getProgress() {
252 | return progress;
253 | }
254 |
255 | /**
256 | * 设置倒计时总时间。
257 | *
258 | * @param timeMillis 毫秒。
259 | */
260 | public void setTimeMillis(long timeMillis) {
261 | this.timeMillis = timeMillis;
262 | invalidate();
263 | }
264 |
265 | /**
266 | * 拿到进度条计时时间。
267 | *
268 | * @return 毫秒。
269 | */
270 | public long getTimeMillis() {
271 | return this.timeMillis;
272 | }
273 |
274 | /**
275 | * 设置进度条类型。
276 | *
277 | * @param progressType {@link ProgressType}.
278 | */
279 | public void setProgressType(ProgressType progressType) {
280 | this.mProgressType = progressType;
281 | resetProgress();
282 | invalidate();
283 | }
284 |
285 | /**
286 | * 重置进度。
287 | */
288 | private void resetProgress() {
289 | switch (mProgressType) {
290 | case COUNT:
291 | progress = 0;
292 | break;
293 | case COUNT_BACK:
294 | progress = maxProgress;
295 | break;
296 | }
297 | }
298 |
299 | /**
300 | * 拿到进度条类型。
301 | *
302 | * @return
303 | */
304 | public ProgressType getProgressType() {
305 | return mProgressType;
306 | }
307 |
308 | /**
309 | * 设置进度监听。
310 | *
311 | * @param mCountdownProgressListener 监听器。
312 | */
313 | public void setCountdownProgressListener(int what, OnCountdownProgressListener mCountdownProgressListener) {
314 | this.listenerWhat = what;
315 | this.mCountdownProgressListener = mCountdownProgressListener;
316 | }
317 |
318 | /**
319 | * 开始。
320 | */
321 | public void start() {
322 | stop();
323 | post(progressChangeTask);
324 | }
325 |
326 | /**
327 | * 重新开始。
328 | */
329 | public void reStart() {
330 | resetProgress();
331 | start();
332 | }
333 |
334 | /**
335 | * 停止。
336 | */
337 | public void stop() {
338 | removeCallbacks(progressChangeTask);
339 | }
340 |
341 | @Override
342 | protected void onDraw(Canvas canvas) {
343 | //获取view的边界
344 | getDrawingRect(bounds);
345 |
346 | int size = bounds.height() > bounds.width() ? bounds.width() : bounds.height();
347 | float outerRadius = size / 2;
348 |
349 | //画内部背景
350 | int circleColor = inCircleColors.getColorForState(getDrawableState(), 0);
351 | mPaint.setStyle(Paint.Style.FILL);
352 | mPaint.setColor(circleColor);
353 | canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth, mPaint);
354 |
355 | //画边框圆
356 | // mPaint.setStyle(Paint.Style.STROKE);
357 | // mPaint.setStrokeWidth(outLineWidth);
358 | // mPaint.setColor(outLineColor);
359 | // canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth / 2, mPaint);
360 |
361 | //画字
362 | Paint paint = getPaint();
363 | paint.setColor(getCurrentTextColor());
364 | paint.setAntiAlias(true);
365 | paint.setTextAlign(Paint.Align.CENTER);
366 | float textY = bounds.centerY() - (paint.descent() + paint.ascent()) / 2;
367 | canvas.drawText(getText().toString(), bounds.centerX(), textY, paint);
368 |
369 | //画进度条
370 | mPaint.setStyle(Paint.Style.STROKE);
371 | mPaint.setStrokeWidth(progressLineWidth);
372 | mPaint.setStrokeCap(Paint.Cap.ROUND);
373 | int deleteWidth = progressLineWidth + outLineWidth;
374 | mArcRect.set(bounds.left + deleteWidth / 2, bounds.top + deleteWidth / 2, bounds.right - deleteWidth / 2, bounds.bottom - deleteWidth / 2);
375 |
376 | //画进度条背景
377 | mPaint.setColor(progressLineBgColor);
378 | canvas.drawArc(mArcRect, 0, 360, false, mPaint);
379 |
380 | mPaint.setColor(progressLineColor);
381 | canvas.drawArc(mArcRect, 0, 360 * progress / maxProgress, false, mPaint);
382 | }
383 |
384 | @Override
385 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
386 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
387 | // int lineWidth = 4 * (outLineWidth + progressLineWidth);
388 | int width = getMeasuredWidth();
389 | int height = getMeasuredHeight();
390 | // int size = (width > height ? width : height) + lineWidth;
391 | // Log.d("ttttt", "width" + width + ", height : " + height + ", size : " + size);
392 | // setMeasuredDimension(size, size);
393 | setMeasuredDimension(width, height);
394 | }
395 |
396 | @Override
397 | protected void drawableStateChanged() {
398 | super.drawableStateChanged();
399 | validateCircleColor();
400 | }
401 |
402 | /**
403 | * 进度更新task。
404 | */
405 | private Runnable progressChangeTask = new Runnable() {
406 | @Override
407 | public void run() {
408 | removeCallbacks(this);
409 | switch (mProgressType) {
410 | case COUNT:
411 | progress += 1;
412 | break;
413 | case COUNT_BACK:
414 | progress -= 1;
415 | break;
416 | }
417 | if (progress >= 0 && progress <= maxProgress) {
418 | if (mCountdownProgressListener != null) {
419 | mCountdownProgressListener.onProgress(listenerWhat, progress);
420 | }
421 | invalidate();
422 | postDelayed(progressChangeTask, progressChangeDuration == 0 ? timeMillis / 100 : progressChangeDuration);
423 | } else
424 | progress = validateProgress(progress);
425 | }
426 | };
427 |
428 | /**
429 | * 进度条类型。
430 | */
431 | public enum ProgressType {
432 | /**
433 | * 顺数进度条,从0-100;
434 | */
435 | COUNT,
436 |
437 | /**
438 | * 倒数进度条,从100-0;
439 | */
440 | COUNT_BACK;
441 | }
442 |
443 | /**
444 | * 进度监听。
445 | */
446 | public interface OnCountdownProgressListener {
447 |
448 | /**
449 | * 进度通知。
450 | *
451 | * @param progress 进度值。
452 | */
453 | void onProgress(int what, int progress);
454 | }
455 | }
456 |
--------------------------------------------------------------------------------
/statebutton/src/main/java/mazouri/statebutton/StateButton.java:
--------------------------------------------------------------------------------
1 | package mazouri.statebutton;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Color;
6 | import android.graphics.Typeface;
7 | import android.text.TextUtils;
8 | import android.util.AttributeSet;
9 | import android.util.Log;
10 | import android.util.TypedValue;
11 | import android.view.ViewGroup;
12 | import android.widget.FrameLayout;
13 | import android.widget.ImageView;
14 | import android.widget.RelativeLayout;
15 | import android.widget.TextView;
16 |
17 | /**
18 | * Created by wangdongdong on 17-3-27.
19 | */
20 |
21 | public class StateButton extends RelativeLayout {
22 |
23 | public static final int ID_ICON =0x1000001;
24 | public static final int ID_PROGRESS =0x1000002;
25 | public static final int ID_FRAME = 0x1000003;
26 | private TextView mTextView;
27 | private ImageView mIconView;
28 |
29 | /**
30 | * Texts for the different states
31 | */
32 | private String disabledText = "";
33 | private String enabledText = "";
34 | private String selectedText = "";
35 | private String loadingText = "";
36 |
37 | /**
38 | * Texts color for the different states
39 | */
40 | private int disabledTextColor;
41 | private int enabledTextColor;
42 | private int selectedTextColor;
43 | private int loadingTextColor;
44 |
45 | /**
46 | * Backgrounds for different states
47 | */
48 | private int disabledBackground;
49 | private int enabledBackground;
50 | private int selectedBackground;
51 | private int loadingBackground;
52 |
53 | /**
54 | * Icons color for different states
55 | */
56 | private int disabledIcon;
57 | private int enabledIcon;
58 | private int selectedIcon;
59 |
60 | /**
61 | * Icons visibility for different states
62 | */
63 | private boolean disabledIconVisibility;
64 | private boolean enabledIconVisibility;
65 | private boolean selectedIconVisibility;
66 |
67 | /**
68 | * 进度条的颜色。
69 | */
70 | private int progressLineColor = Color.BLUE;
71 |
72 | private int progressLineBgColor = Color.GRAY;
73 |
74 | private int progressTextColor;
75 |
76 | /**
77 | * 进度条的宽度。
78 | */
79 | private int progressLineWidth = 8;
80 |
81 | /**
82 | * 进度更新时间间隔
83 | */
84 | private int progressChangeDuration = 0;
85 |
86 | private int maxProgress = 100;
87 |
88 | /**
89 | * Previous state of button
90 | */
91 | private BUTTON_STATES oldState;
92 |
93 | /**
94 | * Current state of the button
95 | */
96 | private BUTTON_STATES currentState;
97 | private CircleTextProgressbar mCircleTextProgressbar;
98 | private FrameLayout mFrameLayout;
99 |
100 | public StateButton(Context context) {
101 | this(context, null);
102 | }
103 |
104 | public StateButton(Context context, AttributeSet attrs) {
105 | this(context, attrs, 0);
106 | }
107 |
108 | public StateButton(Context context, AttributeSet attrs, int defStyleAttr) {
109 | super(context, attrs, defStyleAttr);
110 | TypedArray a = context.obtainStyledAttributes(
111 | attrs, R.styleable.StateButton, defStyleAttr, 0);
112 |
113 | currentState = BUTTON_STATES.fromValue(a.getInt(R.styleable.StateButton_state, 1));
114 | // Texts
115 | disabledText = a.getString(R.styleable.StateButton_disabledText);
116 | enabledText = a.getString(R.styleable.StateButton_enabledText);
117 | selectedText = a.getString(R.styleable.StateButton_selectedText);
118 | loadingText = a.getString(R.styleable.StateButton_loadingText);
119 |
120 | // Texts color
121 | disabledTextColor = a.getColor(R.styleable.StateButton_disabledTextColor, Color.WHITE);
122 | enabledTextColor = a.getColor(R.styleable.StateButton_enabledTextColor, Color.WHITE);
123 | selectedTextColor = a.getColor(R.styleable.StateButton_selectedTextColor, Color.WHITE);
124 | loadingTextColor = a.getColor(R.styleable.StateButton_loadingTextColor, Color.WHITE);
125 |
126 | // Backgrounds
127 | disabledBackground = a.getResourceId(R.styleable.StateButton_disabledBackground, 0);
128 | enabledBackground = a.getResourceId(R.styleable.StateButton_enabledBackground, 0);
129 | selectedBackground = a.getResourceId(R.styleable.StateButton_selectedBackground, 0);
130 | loadingBackground = a.getResourceId(R.styleable.StateButton_loadingBackground, 0);
131 |
132 | // Icons
133 | disabledIcon = a.getResourceId(R.styleable.StateButton_disabledIcon, 0);
134 | enabledIcon = a.getResourceId(R.styleable.StateButton_enabledIcon, 0);
135 | selectedIcon = a.getResourceId(R.styleable.StateButton_selectedIcon, 0);
136 | // Icons visibility
137 | disabledIconVisibility = a.getBoolean(R.styleable.StateButton_disabledIconVisible, true);
138 | enabledIconVisibility = a.getBoolean(R.styleable.StateButton_enabledIconVisible, true);
139 | selectedIconVisibility = a.getBoolean(R.styleable.StateButton_selectedIconVisible, true);
140 |
141 | int textSize = a.getDimensionPixelSize(R.styleable.StateButton_textSize, 0);
142 | int textStyle = a.getInt(R.styleable.StateButton_textStyle, 0);
143 |
144 | int iconTopMargin = a.getDimensionPixelSize(R.styleable.StateButton_iconTopMargin, dip2px(18f));
145 | int iconWidth = a.getDimensionPixelSize(R.styleable.StateButton_iconWidth, dip2px(30f));
146 | int iconHeight = a.getDimensionPixelSize(R.styleable.StateButton_iconHeight, dip2px(30f));
147 | int drawablePadding = a.getDimensionPixelSize(R.styleable.StateButton_drawablePadding, dip2px(10f));
148 |
149 | progressLineColor = a.getColor(R.styleable.StateButton_barProgressLineColor, Color.TRANSPARENT);
150 | progressLineBgColor = a.getColor(R.styleable.StateButton_barProgressLineBgColor, Color.GRAY);
151 | progressTextColor = a.getColor(R.styleable.StateButton_barProgressTextColor, Color.WHITE);
152 | progressLineWidth = a.getDimensionPixelSize(R.styleable.StateButton_barProgressLineWidth, 8);
153 | progressChangeDuration = a.getInt(R.styleable.StateButton_barProgressChangeDuration, 0);
154 | maxProgress = a.getInt(R.styleable.StateButton_barMaxProgress, 100);
155 |
156 | a.recycle();
157 |
158 | oldState = currentState;
159 |
160 | mIconView = new ImageView(context, attrs);
161 | LayoutParams lpIcon = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
162 | lpIcon.width = iconWidth;
163 | lpIcon.height = iconHeight;
164 | mIconView.setScaleType(ImageView.ScaleType.FIT_CENTER);
165 | mIconView.setId(ID_ICON);
166 | // addView(mIconView, lpIcon);
167 |
168 | mCircleTextProgressbar = new CircleTextProgressbar(context, attrs);
169 | LayoutParams lpProgressbar = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
170 |
171 | lpProgressbar.width = iconWidth;
172 | lpProgressbar.height = iconHeight;
173 | mCircleTextProgressbar.setId(ID_PROGRESS);
174 | mCircleTextProgressbar.setProgressColor(progressLineColor);
175 | mCircleTextProgressbar.setProgressLineBgColor(progressLineBgColor);
176 | mCircleTextProgressbar.setProgressLineWidth(progressLineWidth);
177 | mCircleTextProgressbar.setProgressChangeDuration(progressChangeDuration);
178 | mCircleTextProgressbar.setMaxProgress(maxProgress);
179 | mCircleTextProgressbar.setProgressType(CircleTextProgressbar.ProgressType.COUNT_BACK);
180 | mCircleTextProgressbar.setVisibility(GONE);
181 |
182 | mFrameLayout = new FrameLayout(context, attrs);
183 | LayoutParams lpFrame = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
184 | lpFrame.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
185 | lpFrame.topMargin = iconTopMargin;
186 | mFrameLayout.addView(mIconView, lpIcon);
187 | mFrameLayout.addView(mCircleTextProgressbar, lpProgressbar);
188 | mFrameLayout.setId(ID_FRAME);
189 | addView(mFrameLayout, lpFrame);
190 |
191 | mTextView = new TextView(context, attrs);
192 | LayoutParams lpText = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
193 | lpText.topMargin = drawablePadding;
194 | lpText.addRule(RelativeLayout.BELOW, ID_FRAME);
195 | lpText.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
196 |
197 | mTextView.setDuplicateParentStateEnabled(true);
198 | mTextView.setClickable(false);
199 | if (textSize > 0) {
200 | mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
201 | }
202 |
203 | Typeface tf = Typeface.defaultFromStyle(textStyle);
204 | mTextView.setTypeface(tf);
205 | addView(mTextView, lpText);
206 |
207 | setup();
208 | }
209 |
210 | private class AttrWrapper {
211 | String text;
212 | int textColor;
213 | int background;
214 | int icon;
215 | int iconVisibility;
216 | boolean clickable;
217 | }
218 |
219 | private void setup() {
220 | final AttrWrapper attrWrapper = new AttrWrapper();
221 |
222 | switch (currentState.getValue()) {
223 | case 0: // disabled
224 | attrWrapper.text = disabledText;
225 | attrWrapper.textColor = disabledTextColor;
226 | attrWrapper.background = disabledBackground;
227 | attrWrapper.icon = disabledIcon;
228 | attrWrapper.iconVisibility = disabledIconVisibility ? VISIBLE : INVISIBLE;
229 | attrWrapper.clickable = false;
230 | break;
231 | case 1: // enabled
232 | attrWrapper.text = enabledText;
233 | attrWrapper.textColor = enabledTextColor;
234 | attrWrapper.background = enabledBackground;
235 | attrWrapper.icon = enabledIcon;
236 | attrWrapper.iconVisibility = enabledIconVisibility ? VISIBLE : INVISIBLE;
237 | attrWrapper.clickable = true;
238 | break;
239 | case 2: // selected
240 | attrWrapper.text = selectedText;
241 | attrWrapper.textColor = selectedTextColor;
242 | attrWrapper.background = selectedBackground;
243 | attrWrapper.icon = selectedIcon;
244 | attrWrapper.iconVisibility = selectedIconVisibility ? VISIBLE : INVISIBLE;
245 | attrWrapper.clickable = true;
246 | break;
247 | case 3: //loading
248 | attrWrapper.text = loadingText;
249 | attrWrapper.textColor = loadingTextColor;
250 | attrWrapper.background = loadingBackground;
251 | attrWrapper.icon = 0;
252 | attrWrapper.iconVisibility = GONE;
253 | attrWrapper.clickable = false;
254 | break;
255 | }
256 |
257 | if (attrWrapper.background != 0) {
258 | setBackgroundResource(attrWrapper.background);
259 | }
260 |
261 | if (attrWrapper.icon != 0) {
262 | mFrameLayout.setVisibility(VISIBLE);
263 | mIconView.setImageResource(attrWrapper.icon);
264 | } else {
265 | mFrameLayout.setVisibility(GONE);
266 | }
267 |
268 | if (!TextUtils.isEmpty(attrWrapper.text)) {
269 | mTextView.setVisibility(VISIBLE);
270 | mTextView.setTextColor(attrWrapper.textColor);
271 | mTextView.setText(attrWrapper.text);
272 | } else {
273 | mTextView.setVisibility(GONE);
274 | }
275 |
276 | setClickable(attrWrapper.clickable);
277 | invalidate();
278 | }
279 |
280 | public String getDisabledText() {
281 | return disabledText;
282 | }
283 |
284 | public StateButton setDisabledText(String disabledText) {
285 | this.disabledText = disabledText;
286 | return this;
287 | }
288 |
289 | public String getEnabledText() {
290 | return enabledText;
291 | }
292 |
293 | public StateButton setEnabledText(String enabledText) {
294 | this.enabledText = enabledText;
295 | return this;
296 | }
297 |
298 | public String getSelectedText() {
299 | return selectedText;
300 | }
301 |
302 | public StateButton setSelectedText(String selectedText) {
303 | this.selectedText = selectedText;
304 | return this;
305 | }
306 |
307 | public int getDisabledTextColor() {
308 | return disabledTextColor;
309 | }
310 |
311 | public StateButton setDisabledTextColor(int disabledTextColor) {
312 | this.disabledTextColor = disabledTextColor;
313 | return this;
314 | }
315 |
316 | public int getEnabledTextColor() {
317 | return enabledTextColor;
318 | }
319 |
320 | public StateButton setEnabledTextColor(int enabledTextColor) {
321 | this.enabledTextColor = enabledTextColor;
322 | return this;
323 | }
324 |
325 | public int getSelectedTextColor() {
326 | return selectedTextColor;
327 | }
328 |
329 | public StateButton setSelectedTextColor(int selectedTextColor) {
330 | this.selectedTextColor = selectedTextColor;
331 | return this;
332 | }
333 |
334 | public int getDisabledBackground() {
335 | return disabledBackground;
336 | }
337 |
338 | public StateButton setDisabledBackground(int disabledBackground) {
339 | this.disabledBackground = disabledBackground;
340 | return this;
341 | }
342 |
343 | public int getEnabledBackground() {
344 | return enabledBackground;
345 | }
346 |
347 | public StateButton setEnabledBackground(int enabledBackground) {
348 | this.enabledBackground = enabledBackground;
349 | return this;
350 | }
351 |
352 | public int getSelectedBackground() {
353 | return selectedBackground;
354 | }
355 |
356 | public StateButton setSelectedBackground(int selectedBackground) {
357 | this.selectedBackground = selectedBackground;
358 | return this;
359 | }
360 |
361 | public int getDisabledIcon() {
362 | return disabledIcon;
363 | }
364 |
365 | public StateButton setDisabledIcon(int disabledIcon) {
366 | this.disabledIcon = disabledIcon;
367 | return this;
368 | }
369 |
370 | public int getEnabledIcon() {
371 | return enabledIcon;
372 | }
373 |
374 | public StateButton setEnabledIcon(int enabledIcon) {
375 | this.enabledIcon = enabledIcon;
376 | return this;
377 | }
378 |
379 | public int getSelectedIcon() {
380 | return selectedIcon;
381 | }
382 |
383 | public StateButton setSelectedIcon(int selectedIcon) {
384 | this.selectedIcon = selectedIcon;
385 | return this;
386 | }
387 |
388 | public boolean isDisabledIconVisibility() {
389 | return disabledIconVisibility;
390 | }
391 |
392 | public StateButton setDisabledIconVisibility(boolean disabledIconVisibility) {
393 | this.disabledIconVisibility = disabledIconVisibility;
394 | return this;
395 | }
396 |
397 | public boolean isEnabledIconVisibility() {
398 | return enabledIconVisibility;
399 | }
400 |
401 | public StateButton setEnabledIconVisibility(boolean enabledIconVisibility) {
402 | this.enabledIconVisibility = enabledIconVisibility;
403 | return this;
404 | }
405 |
406 | public boolean isSelectedIconVisibility() {
407 | return selectedIconVisibility;
408 | }
409 |
410 | public StateButton setSelectedIconVisibility(boolean selectedIconVisibility) {
411 | this.selectedIconVisibility = selectedIconVisibility;
412 | return this;
413 | }
414 |
415 | public void refresh() {
416 | invalidate();
417 | }
418 |
419 | public int dip2px(float dipValue) {
420 | final float scale = getContext().getResources().getDisplayMetrics().density;
421 | return (int) (dipValue * scale + 0.5f);
422 | }
423 |
424 | /**
425 | * Set the state of the button
426 | *
427 | * @param state
428 | */
429 | public void setState(BUTTON_STATES state) {
430 | if (state == BUTTON_STATES.LOADING && currentState == BUTTON_STATES.LOADING) {
431 | //not reStart loading while loading
432 | return;
433 | }
434 |
435 | this.oldState = this.currentState;
436 | this.currentState = state;
437 |
438 | if (state == BUTTON_STATES.LOADING) {
439 | mCircleTextProgressbar.setVisibility(VISIBLE);
440 | mIconView.setVisibility(GONE);
441 | mCircleTextProgressbar.reStart();
442 | } else {
443 | mCircleTextProgressbar.setVisibility(GONE);
444 | mIconView.setVisibility(VISIBLE);
445 | setup();
446 | }
447 | }
448 |
449 | public CircleTextProgressbar getProgressbar() {
450 | return mCircleTextProgressbar;
451 | }
452 |
453 | public void stopProgress() {
454 | mCircleTextProgressbar.stop();
455 | }
456 |
457 | public void resetStateFromLoading() {
458 | setState(oldState);
459 | }
460 |
461 | public void resetStateFromDisabled() {
462 | setState(oldState);
463 | }
464 |
465 | public void setProgressText(String progressText) {
466 | mCircleTextProgressbar.setText(progressText);
467 | mCircleTextProgressbar.setTextColor(progressTextColor);
468 | }
469 |
470 | public void setCountdownProgressListener(final OnCountdownListener onCountdownListener) {
471 | mCircleTextProgressbar.setCountdownProgressListener(0, new CircleTextProgressbar.OnCountdownProgressListener() {
472 | @Override
473 | public void onProgress(int what, int progress) {
474 | onCountdownListener.onProgress(progress);
475 | if (progress == 0) {
476 | resetStateFromLoading();
477 | }
478 | }
479 | });
480 | }
481 |
482 | /**
483 | * 进度监听。
484 | */
485 | public interface OnCountdownListener {
486 |
487 | /**
488 | * 进度通知。
489 | *
490 | * @param progress 进度值。
491 | */
492 | void onProgress(int progress);
493 | }
494 |
495 | /**
496 | * Get the buttons state
497 | *
498 | * @return
499 | */
500 | public BUTTON_STATES getState() {
501 | return currentState;
502 | }
503 |
504 | /**
505 | * Enum for states
506 | */
507 | public enum BUTTON_STATES {
508 |
509 | DISABLED(0),
510 | ENABLED(1),
511 | SELECTED(2),
512 | LOADING(3);
513 |
514 | private final int value;
515 |
516 | BUTTON_STATES(final int newValue) {
517 | value = newValue;
518 | }
519 |
520 | public int getValue() {
521 | return value;
522 | }
523 |
524 | public static BUTTON_STATES fromValue(int value) {
525 | for (BUTTON_STATES state : BUTTON_STATES.values()) {
526 | if (state.value == value) {
527 | return state;
528 | }
529 | }
530 | return null;
531 | }
532 | }
533 | }
534 |
--------------------------------------------------------------------------------
/statebutton/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/statebutton/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | statebutton
3 |
4 |
--------------------------------------------------------------------------------
/statebutton/src/test/java/mazouri/statebutton/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package mazouri.statebutton;
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 | }
--------------------------------------------------------------------------------