├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── LICENSE ├── README.md ├── README_CHINESE.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── deadline │ │ └── statebutton │ │ └── MainActivity.kt │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── demo.apk ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image.png ├── libary ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── deadline │ │ └── statebutton │ │ ├── StateButton.java │ │ ├── StateConfig.java │ │ ├── StateImageView.java │ │ └── ViewExtend.kt │ └── res │ └── values │ ├── attrs.xml │ └── strings.xml ├── settings.gradle └── stateButton.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.ap_ 3 | 4 | # Files for the ART/Dalvik VM 5 | *.dex 6 | 7 | # Java class files 8 | *.class 9 | 10 | # Generated files 11 | bin/ 12 | gen/ 13 | out/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | 34 | # Intellij 35 | *.iml 36 | .idea/workspace.xml 37 | 38 | # Keystore files 39 | *.jks 40 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 deadline 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StateButton 2 | 3 | ###中文版这里: https://github.com/niniloveyou/StateButton/blob/master/README_CHINESE.md 4 | 5 | Do you hate to write a background for each button? 6 | 7 | Do you hate to write a bunch of selector? 8 | 9 | That's why I'm writing this view. 10 | 11 | 12 | Here to download demo 13 | ---------- 14 | https://github.com/niniloveyou/StateButton/blob/master/demo.apk 15 | 16 | Screenshot 17 | ---------- 18 | 19 | ![](https://github.com/niniloveyou/StateButton/blob/master/image.png) 20 | 21 | ![](https://github.com/niniloveyou/StateButton/blob/master/stateButton.gif) 22 | 23 | ### Download 24 | ``` 25 | allprojects { 26 | repositories { 27 | ...... 28 | maven { url 'https://jitpack.io' } 29 | } 30 | } 31 | implementation 'com.github.niniloveyou:StateButton:v1.0.0' 32 | 33 | ``` 34 | 35 | Customizable attributes 36 | ----------------------- 37 | 38 | | Attribute | default value | xml | java | 39 | |------------------------|------------------------|--------------------------|-------------------------------------| 40 | | normalTextColor | original text color | normalTextColor | setNormalTextColor(int color) | 41 | | pressedTextColor | original text color | pressedTextColor | setPressedTextColor(int color) | 42 | | unableTextColor | original text color | unableTextColor | setUnableTextColor(int color) | 43 | | strokeDashWidth | 0 | strokeDashWidth | setStrokeDash(int dashWidth, int dashGap) | 44 | | strokeDashGap | 0 | strokeDashGap | setStrokeDash(int dashWidth, int dashGap) | 45 | | normalStrokeWidth | 0 | normalStrokeWidth | setNormalStrokeWidth(int widht) | 46 | | pressedStrokeWidth | 0 | pressedStrokeWidth | setPressedStrokeWidth(int widht) | 47 | | unableStrokeWidth | 0 | unableStrokeWidth | setUnableStrokeWidth(int widht) | 48 | | normalStrokeColor | 0 | normalStrokeColor | setNormalStrokeColor(int color) | 49 | | pressedStrokeColor | 0 | pressedStrokeColor | setPressedStrokeColor(int color) | 50 | | unableStrokeColor | 0 | unableStrokeColor | setUnableStrokeColor(int color) | 51 | | normalBackgroundColor | 0 | normalBackgroundColor | setNormalBackgroundColor(int color) | 52 | | pressedBackgroundColor | 0 | pressedBackgroundColor | setPressedBackgroundColor(int color) | 53 | | unableBackgroundColor | 0 | unableBackgroundColor | setUnableBackgroundColor(int color) | 54 | | radius | 0 | radius | setRadius(int radius) / setRadius(float[] radii) | 55 | | round | false | round | setRound(boolean round) | 56 | | animationDuration | 0ms | animationDuration | setAnimationDuration(int duration) | 57 | 58 | Usage 59 | ----- 60 | If the default values of custom attribues did not meet your requirement, you can easily re-config that attributes. This is sample code that you can refer. you can also browse demo app for more details. 61 | 62 | ### via xml (sample) 63 | - Define `xmlns:app="http://schemas.android.com/apk/res-auto"` on root of your xml file 64 | 65 | ```xml 66 | 94 | ``` 95 | 96 | 97 | 98 | Developed By 99 | ------- 100 | deadline 101 | 102 | Blog : http://www.jianshu.com/users/25e80ace21b8/latest_articles 103 | 104 | MIT License 105 | ------- 106 | 107 | Copyright (c) 2016 deadline 108 | 109 | Permission is hereby granted, free of charge, to any person obtaining a copy 110 | of this software and associated documentation files (the "Software"), to deal 111 | in the Software without restriction, including without limitation the rights 112 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 113 | copies of the Software, and to permit persons to whom the Software is 114 | furnished to do so, subject to the following conditions: 115 | 116 | The above copyright notice and this permission notice shall be included in all 117 | copies or substantial portions of the Software. 118 | 119 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 120 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 121 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 122 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 123 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 124 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 125 | SOFTWARE. 126 | 127 | -------------------------------------------------------------------------------- /README_CHINESE.md: -------------------------------------------------------------------------------- 1 | # StateButton 2 | 3 | 你有没有讨厌给每种类型的button写一个drawable(selector) 4 | 5 | 我是写的够够的, 这也是为什么我要写这个小控件的原因。 6 | 7 | demo 下载地址: 8 | ---------- 9 | https://github.com/niniloveyou/StateButton/blob/master/demo.apk 10 | 11 | 截图: 12 | ---------- 13 | 14 | ![](https://github.com/niniloveyou/StateButton/blob/master/image.png) 15 | 16 | ![stateButton](https://github.com/niniloveyou/StateButton/blob/master/stateButton.gif) 17 | 18 | ### Download 19 | ``` 20 | allprojects { 21 | repositories { 22 | ...... 23 | maven { url 'https://jitpack.io' } 24 | } 25 | } 26 | implementation 'com.github.niniloveyou:StateButton:v1.0.0' 27 | 28 | ``` 29 | Customizable attributes 30 | ----------------------- 31 | 32 | | Attribute | default value | xml | java | 33 | |------------------------|------------------------|--------------------------|-------------------------------------| 34 | | normalTextColor | original text color | normalTextColor | setNormalTextColor(int color) | 35 | | pressedTextColor | original text color | pressedTextColor | setPressedTextColor(int color) | 36 | | unableTextColor | original text color | unableTextColor | setUnableTextColor(int color) | 37 | | strokeDashWidth | 0 | strokeDashWidth | setStrokeDash(int dashWidth, int dashGap) | 38 | | strokeDashGap | 0 | strokeDashGap | setStrokeDash(int dashWidth, int dashGap) | 39 | | normalStrokeWidth | 0 | normalStrokeWidth | setNormalStrokeWidth(int widht) | 40 | | pressedStrokeWidth | 0 | pressedStrokeWidth | setPressedStrokeWidth(int widht) | 41 | | unableStrokeWidth | 0 | unableStrokeWidth | setUnableStrokeWidth(int widht) | 42 | | normalStrokeColor | 0 | normalStrokeColor | setNormalStrokeColor(int color) | 43 | | pressedStrokeColor | 0 | pressedStrokeColor | setPressedStrokeColor(int color) | 44 | | unableStrokeColor | 0 | unableStrokeColor | setUnableStrokeColor(int color) | 45 | | normalBackgroundColor | 0 | normalBackgroundColor | setNormalBackgroundColor(int color) | 46 | | pressedBackgroundColor | 0 | pressedBackgroundColor | setPressedBackgroundColor(int color) | 47 | | unableBackgroundColor | 0 | unableBackgroundColor | setUnableBackgroundColor(int color) | 48 | | radius | 0 | radius | setRadius(int radius) / setRadius(float[] radii) | 49 | | round | false | round | setRound(boolean round) | 50 | | animationDuration | 0ms | animationDuration | setAnimationDuration(int duration) | 51 | 52 | 53 | ### xml中定义了以下属性 54 | - Define `xmlns:app="http://schemas.android.com/apk/res-auto"` on root of your xml file 55 | 56 | ```xml 57 | 85 | ``` 86 | 87 | 88 | 89 | Developed By 90 | ------- 91 | deadline 92 | 93 | Blog : http://www.jianshu.com/users/25e80ace21b8/latest_articles 94 | 95 | MIT License 96 | ------- 97 | 98 | Copyright (c) 2016 deadline 99 | 100 | Permission is hereby granted, free of charge, to any person obtaining a copy 101 | of this software and associated documentation files (the "Software"), to deal 102 | in the Software without restriction, including without limitation the rights 103 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 104 | copies of the Software, and to permit persons to whom the Software is 105 | furnished to do so, subject to the following conditions: 106 | 107 | The above copyright notice and this permission notice shall be included in all 108 | copies or substantial portions of the Software. 109 | 110 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 111 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 112 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 113 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 114 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 115 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 116 | SOFTWARE. 117 | 118 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | buildToolsVersion "28.0.3" 8 | 9 | defaultConfig { 10 | applicationId "deadline.statebutton" 11 | minSdkVersion 15 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 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 | implementation project(':libary') 27 | compile 'com.android.support:appcompat-v7:28.0.0' 28 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 29 | } 30 | repositories { 31 | mavenCentral() 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\AndroidSDK/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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/deadline/statebutton/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package deadline.statebutton 2 | 3 | import android.graphics.Color 4 | import android.support.v7.app.AppCompatActivity 5 | import android.os.Bundle 6 | import android.view.View 7 | import android.widget.TextView 8 | import android.widget.Toast 9 | import com.deadline.statebutton.StateButton 10 | import com.deadline.statebutton.StateConfig 11 | import com.deadline.statebutton.setState 12 | 13 | class MainActivity : AppCompatActivity() { 14 | 15 | lateinit var text: StateButton 16 | 17 | lateinit var background: StateButton 18 | 19 | lateinit var radius: StateButton 20 | 21 | lateinit var stroke: StateButton 22 | 23 | lateinit var dash: StateButton 24 | 25 | lateinit var tvExtend: TextView 26 | 27 | override fun onCreate(savedInstanceState: Bundle?) { 28 | super.onCreate(savedInstanceState) 29 | setContentView(R.layout.activity_main) 30 | 31 | 32 | //设置不同状态下文字变色 33 | text = findViewById(R.id.text_test) as StateButton 34 | text.setOnClickListener { text.isEnabled = false } 35 | 36 | //最常用的设置不同背景 37 | background = findViewById(R.id.background_test) as StateButton 38 | background.setOnClickListener { background.isEnabled = false } 39 | 40 | //设置四个角不同的圆角 41 | radius = findViewById(R.id.different_radius_test) as StateButton 42 | radius.setRadius(floatArrayOf(0f, 0f, 20f, 20f, 40f, 40f, 60f, 60f)) 43 | 44 | 45 | //设置不同状态下边框颜色,宽度 46 | stroke = findViewById(R.id.stroke_test) as StateButton 47 | stroke.setOnClickListener { stroke.isEnabled = false } 48 | 49 | //设置间断 50 | dash = findViewById(R.id.dash_test) as StateButton 51 | dash.setOnClickListener { dash.isEnabled = false } 52 | 53 | tvExtend = findViewById(R.id.tv_extend) 54 | tvExtend.setOnClickListener { Toast.makeText(this@MainActivity, "lalala", Toast.LENGTH_SHORT).show() } 55 | 56 | tvExtend.setState(StateConfig.newBuilder() 57 | .setNormalBackgroundColor(Color.GRAY) 58 | .setPressedBackgroundColor(Color.DKGRAY) 59 | .setNormalTextColor(Color.WHITE) 60 | .setPressedTextColor(Color.parseColor("#80ffffff")) 61 | .setRadius(20f) 62 | .build()) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 14 | 15 | 25 | 26 | 40 | 41 | 42 | 54 | 55 | 56 | 69 | 70 | 84 | 85 | 97 | 98 | 118 | 119 | 137 | 138 | 154 | 155 | 169 | 170 | 185 | 186 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | StateButton 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.2.51' 5 | repositories { 6 | jcenter() 7 | google() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.2.1' 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" 13 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | jcenter() 22 | google() 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /demo.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/demo.apk -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jan 23 09:38:23 CST 2019 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-4.6-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 | -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/image.png -------------------------------------------------------------------------------- /libary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /libary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'com.github.dcendents.android-maven' 5 | 6 | group = 'com.github.niniloveyou' 7 | 8 | android { 9 | compileSdkVersion 28 10 | 11 | 12 | 13 | defaultConfig { 14 | minSdkVersion 14 15 | targetSdkVersion 28 16 | versionCode 1 17 | versionName "1.0" 18 | 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | implementation 'com.android.support:appcompat-v7:28.0.0' 34 | } 35 | -------------------------------------------------------------------------------- /libary/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /libary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /libary/src/main/java/com/deadline/statebutton/StateButton.java: -------------------------------------------------------------------------------- 1 | package com.deadline.statebutton; 2 | 3 | import android.content.Context; 4 | import android.content.res.ColorStateList; 5 | import android.content.res.TypedArray; 6 | import android.graphics.drawable.Drawable; 7 | import android.graphics.drawable.GradientDrawable; 8 | import android.graphics.drawable.StateListDrawable; 9 | import android.support.annotation.ColorInt; 10 | import android.support.annotation.FloatRange; 11 | import android.support.annotation.IntRange; 12 | import android.support.v7.widget.AppCompatButton; 13 | import android.util.AttributeSet; 14 | 15 | /** 16 | * @author deadline 17 | * @time 2016-11-07 18 | */ 19 | 20 | public class StateButton extends AppCompatButton { 21 | 22 | //text color 23 | private int mNormalTextColor = 0; 24 | private int mPressedTextColor = 0; 25 | private int mUnableTextColor = 0; 26 | ColorStateList mTextColorStateList; 27 | 28 | //animation duration 29 | private int mDuration = 0; 30 | 31 | //radius 32 | private float mRadius = 0; 33 | private boolean mRound; 34 | 35 | //stroke 36 | private float mStrokeDashWidth = 0; 37 | private float mStrokeDashGap = 0; 38 | private int mNormalStrokeWidth = 0; 39 | private int mPressedStrokeWidth = 0; 40 | private int mUnableStrokeWidth = 0; 41 | private int mNormalStrokeColor = 0; 42 | private int mPressedStrokeColor = 0; 43 | private int mUnableStrokeColor = 0; 44 | 45 | //background color 46 | private int mNormalBackgroundColor = 0; 47 | private int mPressedBackgroundColor = 0; 48 | private int mUnableBackgroundColor = 0; 49 | 50 | private GradientDrawable mNormalBackground; 51 | private GradientDrawable mPressedBackground; 52 | private GradientDrawable mUnableBackground; 53 | 54 | private int[][] states; 55 | 56 | StateListDrawable mStateBackground; 57 | 58 | public StateButton(Context context) { 59 | this(context, null); 60 | } 61 | 62 | public StateButton(Context context, AttributeSet attrs) { 63 | this(context, attrs, android.support.v7.appcompat.R.attr.buttonStyle); 64 | } 65 | 66 | public StateButton(Context context, AttributeSet attrs, int defStyleAttr) { 67 | super(context, attrs, defStyleAttr); 68 | setup(attrs); 69 | } 70 | 71 | private void setup(AttributeSet attrs) { 72 | 73 | states = new int[4][]; 74 | 75 | Drawable drawable = getBackground(); 76 | if(drawable != null && drawable instanceof StateListDrawable){ 77 | mStateBackground = (StateListDrawable) drawable; 78 | }else{ 79 | mStateBackground = new StateListDrawable(); 80 | } 81 | 82 | mNormalBackground = new GradientDrawable(); 83 | mPressedBackground = new GradientDrawable(); 84 | mUnableBackground = new GradientDrawable(); 85 | 86 | //pressed, focused, normal, unable 87 | states[0] = new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed }; 88 | states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }; 89 | states[3] = new int[] { -android.R.attr.state_enabled}; 90 | states[2] = new int[] { android.R.attr.state_enabled }; 91 | 92 | TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.StateButton); 93 | 94 | //get original text color as default 95 | //set text color 96 | mTextColorStateList = getTextColors(); 97 | int mDefaultNormalTextColor = mTextColorStateList.getColorForState(states[2], getCurrentTextColor()); 98 | int mDefaultPressedTextColor = mTextColorStateList.getColorForState(states[0], getCurrentTextColor()); 99 | int mDefaultUnableTextColor = mTextColorStateList.getColorForState(states[3], getCurrentTextColor()); 100 | mNormalTextColor = a.getColor(R.styleable.StateButton_normalTextColor, mDefaultNormalTextColor); 101 | mPressedTextColor = a.getColor(R.styleable.StateButton_pressedTextColor, mDefaultPressedTextColor); 102 | mUnableTextColor = a.getColor(R.styleable.StateButton_unableTextColor, mDefaultUnableTextColor); 103 | setTextColor(); 104 | 105 | //set animation duration 106 | mDuration = a.getInteger(R.styleable.StateButton_animationDuration, mDuration); 107 | mStateBackground.setEnterFadeDuration(mDuration); 108 | mStateBackground.setExitFadeDuration(mDuration); 109 | 110 | //set background color 111 | mNormalBackgroundColor = a.getColor(R.styleable.StateButton_normalBackgroundColor, 0); 112 | mPressedBackgroundColor = a.getColor(R.styleable.StateButton_pressedBackgroundColor, 0); 113 | mUnableBackgroundColor = a.getColor(R.styleable.StateButton_unableBackgroundColor, 0); 114 | mNormalBackground.setColor(mNormalBackgroundColor); 115 | mPressedBackground.setColor(mPressedBackgroundColor); 116 | mUnableBackground.setColor(mUnableBackgroundColor); 117 | 118 | //set radius 119 | mRadius = a.getDimensionPixelSize(R.styleable.StateButton_radius, 0); 120 | mRound = a.getBoolean(R.styleable.StateButton_round, false); 121 | mNormalBackground.setCornerRadius(mRadius); 122 | mPressedBackground.setCornerRadius(mRadius); 123 | mUnableBackground.setCornerRadius(mRadius); 124 | 125 | //set stroke 126 | mStrokeDashWidth = a.getDimensionPixelSize(R.styleable.StateButton_strokeDashWidth, 0); 127 | mStrokeDashGap = a.getDimensionPixelSize(R.styleable.StateButton_strokeDashWidth, 0); 128 | mNormalStrokeWidth = a.getDimensionPixelSize(R.styleable.StateButton_normalStrokeWidth, 0); 129 | mPressedStrokeWidth = a.getDimensionPixelSize(R.styleable.StateButton_pressedStrokeWidth, 0); 130 | mUnableStrokeWidth = a.getDimensionPixelSize(R.styleable.StateButton_unableStrokeWidth, 0); 131 | mNormalStrokeColor = a.getColor(R.styleable.StateButton_normalStrokeColor, 0); 132 | mPressedStrokeColor = a.getColor(R.styleable.StateButton_pressedStrokeColor, 0); 133 | mUnableStrokeColor = a.getColor(R.styleable.StateButton_unableStrokeColor, 0); 134 | setStroke(); 135 | 136 | //set background 137 | mStateBackground.addState(states[0], mPressedBackground); 138 | mStateBackground.addState(states[1], mPressedBackground); 139 | mStateBackground.addState(states[3], mUnableBackground); 140 | mStateBackground.addState(states[2], mNormalBackground); 141 | setBackgroundDrawable(mStateBackground); 142 | a.recycle(); 143 | } 144 | 145 | 146 | @Override 147 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 148 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 149 | setRound(mRound); 150 | } 151 | 152 | /****************** stroke color *********************/ 153 | 154 | public void setNormalStrokeColor(@ColorInt int normalStrokeColor) { 155 | this.mNormalStrokeColor = normalStrokeColor; 156 | setStroke(mNormalBackground, mNormalStrokeColor, mNormalStrokeWidth); 157 | } 158 | 159 | public void setPressedStrokeColor(@ColorInt int pressedStrokeColor) { 160 | this.mPressedStrokeColor = pressedStrokeColor; 161 | setStroke(mPressedBackground, mPressedStrokeColor, mPressedStrokeWidth); 162 | } 163 | 164 | public void setUnableStrokeColor(@ColorInt int unableStrokeColor) { 165 | this.mUnableStrokeColor = unableStrokeColor; 166 | setStroke(mUnableBackground, mUnableStrokeColor, mUnableStrokeWidth); 167 | } 168 | 169 | public void setStateStrokeColor(@ColorInt int normal, @ColorInt int pressed, @ColorInt int unable){ 170 | mNormalStrokeColor = normal; 171 | mPressedStrokeColor = pressed; 172 | mUnableStrokeColor = unable; 173 | setStroke(); 174 | } 175 | 176 | /****************** stroke width *********************/ 177 | 178 | public void setNormalStrokeWidth(int normalStrokeWidth) { 179 | this.mNormalStrokeWidth = normalStrokeWidth; 180 | setStroke(mNormalBackground, mNormalStrokeColor, mNormalStrokeWidth); 181 | } 182 | 183 | public void setPressedStrokeWidth(int pressedStrokeWidth) { 184 | this.mPressedStrokeWidth = pressedStrokeWidth; 185 | setStroke(mPressedBackground, mPressedStrokeColor, mPressedStrokeWidth); 186 | } 187 | 188 | public void setUnableStrokeWidth(int unableStrokeWidth) { 189 | this.mUnableStrokeWidth = unableStrokeWidth; 190 | setStroke(mUnableBackground, mUnableStrokeColor, mUnableStrokeWidth); 191 | } 192 | 193 | public void setStateStrokeWidth(int normal, int pressed, int unable){ 194 | mNormalStrokeWidth = normal; 195 | mPressedStrokeWidth = pressed; 196 | mUnableStrokeWidth= unable; 197 | setStroke(); 198 | } 199 | 200 | public void setStrokeDash(float strokeDashWidth, float strokeDashGap) { 201 | this.mStrokeDashWidth = strokeDashWidth; 202 | this.mStrokeDashGap = strokeDashWidth; 203 | setStroke(); 204 | } 205 | 206 | private void setStroke(){ 207 | setStroke(mNormalBackground, mNormalStrokeColor, mNormalStrokeWidth); 208 | setStroke(mPressedBackground, mPressedStrokeColor, mPressedStrokeWidth); 209 | setStroke(mUnableBackground, mUnableStrokeColor, mUnableStrokeWidth); 210 | } 211 | 212 | private void setStroke(GradientDrawable mBackground, int mStrokeColor, int mStrokeWidth) { 213 | mBackground.setStroke(mStrokeWidth, mStrokeColor, mStrokeDashWidth, mStrokeDashGap); 214 | } 215 | 216 | /******************** radius *******************************/ 217 | 218 | public void setRadius(@FloatRange(from = 0) float radius) { 219 | this.mRadius = radius; 220 | mNormalBackground.setCornerRadius(mRadius); 221 | mPressedBackground.setCornerRadius(mRadius); 222 | mUnableBackground.setCornerRadius(mRadius); 223 | } 224 | 225 | public void setRound(boolean round){ 226 | this.mRound = round; 227 | int height = getMeasuredHeight(); 228 | if(mRound){ 229 | setRadius(height / 2f); 230 | } 231 | } 232 | 233 | public void setRadius(float[] radii){ 234 | mNormalBackground.setCornerRadii(radii); 235 | mPressedBackground.setCornerRadii(radii); 236 | mUnableBackground.setCornerRadii(radii); 237 | } 238 | 239 | /******************** background color **********************/ 240 | 241 | public void setStateBackgroundColor(@ColorInt int normal, @ColorInt int pressed, @ColorInt int unable){ 242 | mNormalBackgroundColor = normal; 243 | mPressedBackgroundColor = pressed; 244 | mUnableBackgroundColor = unable; 245 | mNormalBackground.setColor(mNormalBackgroundColor); 246 | mPressedBackground.setColor(mPressedBackgroundColor); 247 | mUnableBackground.setColor(mUnableBackgroundColor); 248 | } 249 | 250 | public void setNormalBackgroundColor(@ColorInt int normalBackgroundColor) { 251 | this.mNormalBackgroundColor = normalBackgroundColor; 252 | mNormalBackground.setColor(mNormalBackgroundColor); 253 | } 254 | 255 | public void setPressedBackgroundColor(@ColorInt int pressedBackgroundColor) { 256 | this.mPressedBackgroundColor = pressedBackgroundColor; 257 | mPressedBackground.setColor(mPressedBackgroundColor); 258 | } 259 | 260 | public void setUnableBackgroundColor(@ColorInt int unableBackgroundColor) { 261 | this.mUnableBackgroundColor = unableBackgroundColor; 262 | mUnableBackground.setColor(mUnableBackgroundColor); 263 | } 264 | 265 | /*******************alpha animation duration********************/ 266 | public void setAnimationDuration(@IntRange(from = 0)int duration){ 267 | this.mDuration = duration; 268 | mStateBackground.setEnterFadeDuration(mDuration); 269 | } 270 | 271 | /*************** text color ***********************/ 272 | 273 | private void setTextColor() { 274 | int[] colors = new int[] {mPressedTextColor, mPressedTextColor, mNormalTextColor, mUnableTextColor}; 275 | mTextColorStateList = new ColorStateList(states, colors); 276 | setTextColor(mTextColorStateList); 277 | } 278 | 279 | public void setStateTextColor(@ColorInt int normal, @ColorInt int pressed, @ColorInt int unable){ 280 | this.mNormalTextColor = normal; 281 | this.mPressedTextColor = pressed; 282 | this.mUnableTextColor = unable; 283 | setTextColor(); 284 | } 285 | 286 | public void setNormalTextColor(@ColorInt int normalTextColor) { 287 | this.mNormalTextColor = normalTextColor; 288 | setTextColor(); 289 | 290 | } 291 | 292 | public void setPressedTextColor(@ColorInt int pressedTextColor) { 293 | this.mPressedTextColor = pressedTextColor; 294 | setTextColor(); 295 | } 296 | 297 | public void setUnableTextColor(@ColorInt int unableTextColor) { 298 | this.mUnableTextColor = unableTextColor; 299 | setTextColor(); 300 | } 301 | } 302 | -------------------------------------------------------------------------------- /libary/src/main/java/com/deadline/statebutton/StateConfig.java: -------------------------------------------------------------------------------- 1 | package com.deadline.statebutton; 2 | 3 | import android.support.annotation.ColorInt; 4 | import android.support.annotation.FloatRange; 5 | import android.support.annotation.IntRange; 6 | 7 | public final class StateConfig { 8 | 9 | //animation duration 10 | private int mDuration; 11 | 12 | //radius 13 | private float mRadius; 14 | 15 | //text color 16 | private int mNormalTextColor; 17 | private int mPressedTextColor; 18 | private int mUnableTextColor; 19 | 20 | //stroke 21 | private float mStrokeDashWidth; 22 | private float mStrokeDashGap; 23 | private int mNormalStrokeWidth; 24 | private int mPressedStrokeWidth; 25 | private int mUnableStrokeWidth; 26 | private int mNormalStrokeColor; 27 | private int mPressedStrokeColor; 28 | private int mUnableStrokeColor; 29 | 30 | //background color 31 | private int mNormalBackgroundColor; 32 | private int mPressedBackgroundColor; 33 | private int mUnableBackgroundColor; 34 | 35 | private StateConfig(Builder builder) { 36 | this.mNormalTextColor = builder.mNormalTextColor; 37 | this.mPressedTextColor = builder.mPressedTextColor; 38 | this.mUnableTextColor = builder.mUnableTextColor; 39 | this.mDuration = builder.mDuration; 40 | this.mRadius = builder.mRadius; 41 | this.mStrokeDashWidth = builder.mStrokeDashWidth; 42 | this.mStrokeDashGap = builder.mStrokeDashGap; 43 | this.mNormalStrokeWidth = builder.mNormalStrokeWidth; 44 | this.mPressedStrokeWidth = builder.mPressedStrokeWidth; 45 | this.mUnableStrokeWidth = builder.mUnableStrokeWidth; 46 | this.mNormalStrokeColor = builder.mNormalStrokeColor; 47 | this.mPressedStrokeColor = builder.mPressedStrokeColor; 48 | this.mUnableStrokeColor = builder.mUnableStrokeColor; 49 | this.mNormalBackgroundColor = builder.mNormalBackgroundColor; 50 | this.mPressedBackgroundColor = builder.mPressedBackgroundColor; 51 | this.mUnableBackgroundColor = builder.mUnableBackgroundColor; 52 | } 53 | 54 | public int getNormalTextColor() { 55 | return mNormalTextColor; 56 | } 57 | 58 | public int getPressedTextColor() { 59 | return mPressedTextColor; 60 | } 61 | 62 | public int getUnableTextColor() { 63 | return mUnableTextColor; 64 | } 65 | 66 | public int getDuration() { 67 | return mDuration; 68 | } 69 | 70 | public float getRadius() { 71 | return mRadius; 72 | } 73 | 74 | public float getStrokeDashWidth() { 75 | return mStrokeDashWidth; 76 | } 77 | 78 | public float getStrokeDashGap() { 79 | return mStrokeDashGap; 80 | } 81 | 82 | public int getNormalStrokeWidth() { 83 | return mNormalStrokeWidth; 84 | } 85 | 86 | public int getPressedStrokeWidth() { 87 | return mPressedStrokeWidth; 88 | } 89 | 90 | public int getUnableStrokeWidth() { 91 | return mUnableStrokeWidth; 92 | } 93 | 94 | public int getNormalStrokeColor() { 95 | return mNormalStrokeColor; 96 | } 97 | 98 | public int getPressedStrokeColor() { 99 | return mPressedStrokeColor; 100 | } 101 | 102 | public int getUnableStrokeColor() { 103 | return mUnableStrokeColor; 104 | } 105 | 106 | public int getNormalBackgroundColor() { 107 | return mNormalBackgroundColor; 108 | } 109 | 110 | public int getPressedBackgroundColor() { 111 | return mPressedBackgroundColor; 112 | } 113 | 114 | public int getUnableBackgroundColor() { 115 | return mUnableBackgroundColor; 116 | } 117 | 118 | public static Builder newBuilder() { 119 | return new Builder(); 120 | } 121 | 122 | public static class Builder { 123 | 124 | //animation duration 125 | private int mDuration; 126 | 127 | //radius 128 | private float mRadius; 129 | 130 | //text color 131 | private int mNormalTextColor; 132 | private int mPressedTextColor; 133 | private int mUnableTextColor; 134 | 135 | //stroke 136 | private float mStrokeDashWidth; 137 | private float mStrokeDashGap; 138 | private int mNormalStrokeWidth; 139 | private int mPressedStrokeWidth; 140 | private int mUnableStrokeWidth; 141 | private int mNormalStrokeColor; 142 | private int mPressedStrokeColor; 143 | private int mUnableStrokeColor; 144 | 145 | //background color 146 | private int mNormalBackgroundColor; 147 | private int mPressedBackgroundColor; 148 | private int mUnableBackgroundColor; 149 | 150 | 151 | public Builder setDuration(@IntRange(from = 0) int duration) { 152 | this.mDuration = duration; 153 | return this; 154 | } 155 | 156 | public Builder setRadius(@FloatRange(from = 0) float radius) { 157 | this.mRadius = radius; 158 | return this; 159 | } 160 | 161 | public Builder setNormalTextColor(@ColorInt int normalTextColor) { 162 | this.mNormalTextColor = normalTextColor; 163 | return this; 164 | } 165 | 166 | public Builder setPressedTextColor(@ColorInt int pressedTextColor) { 167 | this.mPressedTextColor = pressedTextColor; 168 | return this; 169 | } 170 | 171 | public Builder setUnableTextColor(@ColorInt int unableTextColor) { 172 | this.mUnableTextColor = unableTextColor; 173 | return this; 174 | } 175 | 176 | public Builder setStrokeDashWidth(@FloatRange(from = 0) float strokeDashWidth) { 177 | this.mStrokeDashWidth = strokeDashWidth; 178 | return this; 179 | } 180 | 181 | public Builder setStrokeDashGap(@FloatRange(from = 0) float strokeDashGap) { 182 | this.mStrokeDashGap = strokeDashGap; 183 | return this; 184 | } 185 | 186 | public Builder setNormalStrokeWidth(@IntRange(from = 0) int normalStrokeWidth) { 187 | this.mNormalStrokeWidth = normalStrokeWidth; 188 | return this; 189 | } 190 | 191 | public Builder setPressedStrokeWidth(@IntRange(from = 0) int pressedStrokeWidth) { 192 | this.mPressedStrokeWidth = pressedStrokeWidth; 193 | return this; 194 | } 195 | 196 | public Builder setUnableStrokeWidth(@IntRange(from = 0) int unableStrokeWidth) { 197 | this.mUnableStrokeWidth = unableStrokeWidth; 198 | return this; 199 | } 200 | 201 | public Builder setNormalStrokeColor(@ColorInt int normalStrokeColor) { 202 | this.mNormalStrokeColor = normalStrokeColor; 203 | return this; 204 | } 205 | 206 | public Builder setPressedStrokeColor(@ColorInt int pressedStrokeColor) { 207 | this.mPressedStrokeColor = pressedStrokeColor; 208 | return this; 209 | } 210 | 211 | public Builder setUnableStrokeColor(@ColorInt int unableStrokeColor) { 212 | this.mUnableStrokeColor = mUnableStrokeColor; 213 | return this; 214 | } 215 | 216 | public Builder setNormalBackgroundColor(@ColorInt int normalBackgroundColor) { 217 | this.mNormalBackgroundColor = normalBackgroundColor; 218 | return this; 219 | } 220 | 221 | public Builder setPressedBackgroundColor(@ColorInt int pressedBackgroundColor) { 222 | this.mPressedBackgroundColor = pressedBackgroundColor; 223 | return this; 224 | } 225 | 226 | public Builder setUnableBackgroundColor(@ColorInt int unableBackgroundColor) { 227 | this.mUnableBackgroundColor = unableBackgroundColor; 228 | return this; 229 | } 230 | 231 | public Builder setStateTextColor(@ColorInt int normal, @ColorInt int pressed, @ColorInt int unable){ 232 | this.mNormalTextColor = normal; 233 | this.mPressedTextColor = pressed; 234 | this.mUnableTextColor = unable; 235 | return this; 236 | } 237 | 238 | public Builder setStateBackgroundColor(@ColorInt int normal, @ColorInt int pressed, @ColorInt int unable){ 239 | mNormalBackgroundColor = normal; 240 | mPressedBackgroundColor = pressed; 241 | mUnableBackgroundColor = unable; 242 | return this; 243 | } 244 | 245 | public Builder setStateStrokeColor(@ColorInt int normal, @ColorInt int pressed, @ColorInt int unable){ 246 | mNormalStrokeColor = normal; 247 | mPressedStrokeColor = pressed; 248 | mUnableStrokeColor = unable; 249 | return this; 250 | } 251 | 252 | public Builder() { 253 | } 254 | 255 | public StateConfig build() { 256 | return new StateConfig(this); 257 | } 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /libary/src/main/java/com/deadline/statebutton/StateImageView.java: -------------------------------------------------------------------------------- 1 | package com.deadline.statebutton; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.drawable.Drawable; 6 | import android.graphics.drawable.StateListDrawable; 7 | import android.support.annotation.IntRange; 8 | import android.support.v7.widget.AppCompatImageView; 9 | import android.util.AttributeSet; 10 | 11 | /** 12 | * Created by deadline on 2017/1/11. 13 | * 根据用户点击状态设置不同的图片 14 | */ 15 | 16 | public class StateImageView extends AppCompatImageView { 17 | 18 | private Drawable mNormalDrawable; 19 | 20 | private Drawable mPressedDrawable; 21 | 22 | private Drawable mUnableDrawable; 23 | 24 | private int mDuration = 0; 25 | 26 | private int[][] states; 27 | 28 | private StateListDrawable mStateBackground; 29 | 30 | public StateImageView(Context context) { 31 | this(context, null); 32 | } 33 | 34 | public StateImageView(Context context, AttributeSet attrs) { 35 | this(context, attrs, 0); 36 | } 37 | 38 | public StateImageView(Context context, AttributeSet attrs, int defStyleAttr) { 39 | super(context, attrs, defStyleAttr); 40 | 41 | states = new int[4][]; 42 | states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }; 43 | states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }; 44 | states[3] = new int[] { -android.R.attr.state_enabled }; 45 | states[2] = new int[] { android.R.attr.state_enabled }; 46 | 47 | Drawable drawable = getBackground(); 48 | if(drawable != null && drawable instanceof StateListDrawable){ 49 | mStateBackground = (StateListDrawable) drawable; 50 | }else{ 51 | mStateBackground = new StateListDrawable(); 52 | } 53 | 54 | TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.StateImageView); 55 | 56 | mNormalDrawable = a.getDrawable(R.styleable.StateImageView_normalBackground); 57 | mPressedDrawable = a.getDrawable(R.styleable.StateImageView_pressedBackground); 58 | mUnableDrawable = a.getDrawable(R.styleable.StateImageView_unableBackground); 59 | setStateBackground(mNormalDrawable, mPressedDrawable, mUnableDrawable); 60 | 61 | mDuration = a.getInteger(R.styleable.StateImageView_AnimationDuration, mDuration); 62 | setAnimationDuration(mDuration); 63 | a.recycle(); 64 | } 65 | 66 | /** 67 | * 设置不同状态下的背景 68 | * @param normal 69 | * @param pressed 70 | * @param unable 71 | */ 72 | public void setStateBackground(Drawable normal, Drawable pressed, Drawable unable){ 73 | this.mNormalDrawable = normal; 74 | this.mPressedDrawable = pressed; 75 | this.mUnableDrawable = unable; 76 | 77 | //set background 78 | if(mPressedDrawable != null) { 79 | mStateBackground.addState(states[0], mPressedDrawable); 80 | mStateBackground.addState(states[1], mPressedDrawable); 81 | } 82 | 83 | if(mUnableDrawable != null) { 84 | mStateBackground.addState(states[3], mUnableDrawable); 85 | } 86 | 87 | if(mNormalDrawable != null) { 88 | mStateBackground.addState(states[2], mNormalDrawable); 89 | } 90 | setBackgroundDrawable(mStateBackground); 91 | } 92 | 93 | /** 94 | * 设置动画时长 95 | * @param duration 96 | */ 97 | public void setAnimationDuration(@IntRange(from = 0)int duration){ 98 | this.mDuration = duration; 99 | mStateBackground.setEnterFadeDuration(mDuration); 100 | mStateBackground.setExitFadeDuration(mDuration); 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /libary/src/main/java/com/deadline/statebutton/ViewExtend.kt: -------------------------------------------------------------------------------- 1 | package com.deadline.statebutton 2 | 3 | import android.content.res.ColorStateList 4 | import android.graphics.drawable.GradientDrawable 5 | import android.graphics.drawable.StateListDrawable 6 | import android.support.annotation.NonNull 7 | import android.support.v4.view.ViewCompat 8 | import android.view.View 9 | import android.widget.TextView 10 | 11 | /** 12 | * @author deadline 13 | * @time 2018/7/19 14 | */ 15 | fun View.setState(@NonNull config: StateConfig) { 16 | 17 | //pressed, focused, normal, unable 18 | val states = arrayOf(intArrayOf(android.R.attr.state_enabled, android.R.attr.state_pressed), 19 | intArrayOf(android.R.attr.state_enabled, android.R.attr.state_focused), 20 | intArrayOf(-android.R.attr.state_enabled), 21 | intArrayOf(android.R.attr.state_enabled)) 22 | 23 | 24 | val mNormalBackground = GradientDrawable() 25 | val mPressedBackground = GradientDrawable() 26 | val mUnableBackground = GradientDrawable() 27 | 28 | var mStateBackground: StateListDrawable? = null 29 | val drawable = background 30 | mStateBackground = if (drawable != null && drawable is StateListDrawable) drawable else StateListDrawable() 31 | 32 | applyState(mNormalBackground, config.normalBackgroundColor, config.radius, 33 | config.normalStrokeWidth, config.normalStrokeColor, 34 | config.strokeDashWidth, config.strokeDashGap) 35 | 36 | applyState(mPressedBackground, config.pressedBackgroundColor, config.radius, 37 | config.pressedStrokeWidth, config.pressedStrokeColor, 38 | config.strokeDashWidth, config.strokeDashGap) 39 | 40 | applyState(mUnableBackground, config.unableBackgroundColor, config.radius, 41 | config.unableStrokeWidth, config.unableStrokeColor, 42 | config.strokeDashWidth, config.strokeDashGap) 43 | 44 | 45 | if (this@setState is TextView) { 46 | 47 | val colors = intArrayOf(config.pressedTextColor, config.pressedTextColor, config.unableTextColor, config.normalTextColor) 48 | setTextColor(ColorStateList(states, colors)) 49 | } 50 | 51 | mStateBackground.addState(states[0], mPressedBackground) 52 | mStateBackground.addState(states[1], mPressedBackground) 53 | mStateBackground.addState(states[2], mUnableBackground) 54 | mStateBackground.addState(states[3], mNormalBackground) 55 | 56 | mStateBackground.apply { 57 | setEnterFadeDuration(config.duration) 58 | } 59 | 60 | ViewCompat.setBackground(this@setState, mStateBackground) 61 | } 62 | 63 | private fun applyState(drawable: GradientDrawable, 64 | backgroundColor: Int, radius: Float, 65 | strokeWidth: Int, strokeColor: Int, 66 | strokeDashWidth: Float, strokeDashGap: Float) { 67 | 68 | drawable.apply { 69 | setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap) 70 | setColor(backgroundColor) 71 | cornerRadius = radius 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /libary/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 | -------------------------------------------------------------------------------- /libary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | libarary 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':libary' 2 | -------------------------------------------------------------------------------- /stateButton.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niniloveyou/StateButton/faf14042fe532c0bfe5e1d0e1e5c376c993168e4/stateButton.gif --------------------------------------------------------------------------------