├── .gitignore ├── .idea └── workspace.xml ├── ChangeLogs.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── kingja │ │ └── switchbutton │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── fonts │ │ ├── DeVinneTxtBT.ttf │ │ └── tielanti.ttf │ ├── java │ └── com │ │ └── kingja │ │ └── switchbutton │ │ └── MainActivity.java │ └── 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 │ ├── arrays.xml │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img ├── leftPath.png ├── line.png ├── mark.png ├── selected.png ├── stroke.png └── usage.gif ├── libk-switchbutton ├── .gitignore ├── build.gradle ├── libk-switchbutton.iml ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── lib │ │ └── kingja │ │ └── switchbutton │ │ └── SwitchMultiButton.java │ └── res │ └── values │ ├── attr.xml │ └── strings.xml ├── local.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | .idea 7 | /captures 8 | .externalNativeBuild 9 | /app/build/ 10 | /libk-switchbutton/build/ 11 | -------------------------------------------------------------------------------- /ChangeLogs.md: -------------------------------------------------------------------------------- 1 | **v1.1.8** 2 | - support setEnable 3 | - support attr 'disableColor' 4 | 5 | **v1.1.6** 6 | - support typeface.[issues6](https://github.com/KingJA/SwitchButton/issues/6) 7 | 8 | **v1.1.5** 9 | - Fix tabs count error.[issues8](https://github.com/KingJA/SwitchButton/issues/8) 10 | 11 | **v1.1.3** 12 | - Call onSwitch() when setSelectedTab() is called. 13 | - Perfect default size for 'wrap_content'. 14 | - Add attribute ***switchTabs*** for getting string-array in xml. 15 | 16 | **v1.1.2** 17 | - Fix onRestoreInstanceState. 18 | 19 | **v1.1.1** 20 | - Initial release . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwitchButton 2 | A smart switchable button,support multiple tabs. CLICK THE ***STAR*** if it's useful for you. 3 | 4 | ## Preview 5 | ![](https://github.com/KingJA/SwitchButton/blob/master/img/usage.gif) 6 | ## Custom attribute 7 | | attribute | format | example | 8 | | :------------- |:-------------| :-----| 9 | | strokeRadius | dimension | app:strokeRadius="5dp" | 10 | | strokeWidth | dimension | app:strokeWidth="2dp" | 11 | | textSize | dimension | app:textSize="16sp" | 12 | | selectedColor | color/reference | app:selectedColor="@color/red" | 13 | | disableColor | color/reference | app:selectedColor="@color/gray" | 14 | | selectedTab | integer | app:selectedTab="1" | 15 | | switchTabs | reference | app:switchTabs="@array/switch_tabs" | 16 | | typeface | string | app:typeface="DeVinneTxtBT.ttf" | 17 | 18 | ![](https://github.com/KingJA/SwitchButton/blob/master/img/mark.png) 19 | ## Gradle 20 | ```xml 21 | compile 'lib.kingja.switchbutton:switchbutton:1.1.8' 22 | ``` 23 | 24 | ## Usage 25 | ### step 1 26 | ```xml 27 | 38 | ``` 39 | 40 | ### step 2 41 | ```java 42 | //set switch tabs with ***app:switchTabs*** in xml 43 | 44 | SwitchMultiButton mSwitchMultiButton = (SwitchMultiButton) findViewById(R.id.switchmultibutton); 45 | mSwitchMultiButton.setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() { 46 | @Override 47 | public void onSwitch(int position, String tabText) { 48 | Toast.makeText(MainActivity.this, tabText, Toast.LENGTH_SHORT).show(); 49 | } 50 | }); 51 | 52 | //or set switch tabs in java code 53 | 54 | SwitchMultiButton mSwitchMultiButton = (SwitchMultiButton) findViewById(R.id.switchmultibutton); 55 | mSwitchMultiButton.setText("点个Star", "狠心拒绝").setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() { 56 | @Override 57 | public void onSwitch(int position, String tabText) { 58 | Toast.makeText(MainActivity.this, tabText, Toast.LENGTH_SHORT).show(); 59 | } 60 | }); 61 | ``` 62 | ## [Changelog](ChangeLogs.md) 63 | 64 | 65 | ## Contact me 66 | Any questions:Welcome to contact me. 67 | * Email:kingjavip@gmail.com 68 | 69 | ## License 70 | 71 | Copyright 2017 KingJA 72 | 73 | Licensed under the Apache License, Version 2.0 (the "License"); 74 | you may not use this file except in compliance with the License. 75 | You may obtain a copy of the License at 76 | 77 | http://www.apache.org/licenses/LICENSE-2.0 78 | 79 | Unless required by applicable law or agreed to in writing, software 80 | distributed under the License is distributed on an "AS IS" BASIS, 81 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 82 | See the License for the specific language governing permissions and 83 | limitations under the License. 84 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion COMPILE_SDK_VERSION as int 5 | buildToolsVersion BUILD_TOOLS_VERSION 6 | defaultConfig { 7 | applicationId "com.lib.kingja.switchbutton" 8 | minSdkVersion MIN_SDK_VERSION as int 9 | targetSdkVersion TARGET_SDK_VERSION as int 10 | versionCode VERSION_CODE as int 11 | versionName VERSION_NAME 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | lintOptions { 21 | abortOnError false 22 | } 23 | buildToolsVersion '26.0.2' 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | } 29 | 30 | dependencies { 31 | compile fileTree(include: ['*.jar'], dir: 'libs') 32 | testCompile 'junit:junit:4.12' 33 | implementation project(':libk-switchbutton') 34 | // compile 'lib.kingja.switchbutton:switchbutton:1.1.6' 35 | implementation "com.android.support:appcompat-v7:${SUPPORT_VERSION}" 36 | } 37 | -------------------------------------------------------------------------------- /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 E:\wxy\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/kingja/switchbutton/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.kingja.switchbutton; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/assets/fonts/DeVinneTxtBT.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/app/src/main/assets/fonts/DeVinneTxtBT.ttf -------------------------------------------------------------------------------- /app/src/main/assets/fonts/tielanti.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/app/src/main/assets/fonts/tielanti.ttf -------------------------------------------------------------------------------- /app/src/main/java/com/kingja/switchbutton/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 KingJA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.kingja.switchbutton; 19 | 20 | import android.os.Bundle; 21 | import android.support.v7.app.AppCompatActivity; 22 | import android.widget.Toast; 23 | 24 | import com.lib.kingja.switchbutton.R; 25 | 26 | import lib.kingja.switchbutton.SwitchMultiButton; 27 | 28 | 29 | /** 30 | * Description:A smart switchable button,support multiple tabs. 31 | * Create Time:2016/7/27 10:26 32 | * Author:KingJA 33 | * Email:kingjavip@gmail.com 34 | * update:add into Jenkins 16:23 35 | */ 36 | public class MainActivity extends AppCompatActivity { 37 | 38 | private String[] tabTexts1 = {"才子", "帅哥", "大湿", "猛将兄"}; 39 | private String[] tabTexts4 = {"已经", "在家", "等你"}; 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | setContentView(R.layout.activity_main); 45 | ((SwitchMultiButton) findViewById(R.id.switchmultibutton1)).setText(tabTexts1).setOnSwitchListener 46 | (onSwitchListener); 47 | ((SwitchMultiButton) findViewById(R.id.switchmultibutton2)).setText("点个Star", "狠心拒绝").setOnSwitchListener 48 | (onSwitchListener); 49 | ((SwitchMultiButton) findViewById(R.id.switchmultibutton3)).setOnSwitchListener(onSwitchListener).setSelectedTab(1); 50 | ((SwitchMultiButton) findViewById(R.id.switchmultibutton4)).setText(tabTexts4).setOnSwitchListener 51 | (onSwitchListener); 52 | ((SwitchMultiButton) findViewById(R.id.switchmultibutton6)).setEnabled(false); 53 | } 54 | 55 | private SwitchMultiButton.OnSwitchListener onSwitchListener = new SwitchMultiButton.OnSwitchListener() { 56 | @Override 57 | public void onSwitch(int position, String tabText) { 58 | Toast.makeText(MainActivity.this, tabText, Toast.LENGTH_SHORT).show(); 59 | } 60 | }; 61 | 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 23 | 24 | 25 | 40 | 41 | 55 | 56 | 66 | 67 | 79 | 80 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/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/arrays.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 凤姐 5 | 志玲姐姐 6 | 苍老师 7 | 8 | 9 | World 10 | China 11 | 12 | 13 | 14 | Disable 15 | Enable 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #b34167 7 | #4c8b4f 8 | #ffffff 9 | #ffe15b08 10 | #4866df 11 | #da8c05 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SwitchButton 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | repositories { 4 | google() 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:3.0.1' 9 | classpath 'com.novoda:bintray-release:0.8.0' 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | google() 16 | jcenter() 17 | } 18 | } 19 | 20 | task clean(type: Delete) { 21 | delete rootProject.buildDir 22 | } 23 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | VERSION_NAME=1.1.8 2 | VERSION_CODE=2019011001 3 | COMPILE_SDK_VERSION = 27 4 | BUILD_TOOLS_VERSION = 27.0.3 5 | MIN_SDK_VERSION = 14 6 | TARGET_SDK_VERSION = 27 7 | SUPPORT_VERSION = 27.1.1 8 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Jul 09 18:58:12 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-4.1-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 | -------------------------------------------------------------------------------- /img/leftPath.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/img/leftPath.png -------------------------------------------------------------------------------- /img/line.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/img/line.png -------------------------------------------------------------------------------- /img/mark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/img/mark.png -------------------------------------------------------------------------------- /img/selected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/img/selected.png -------------------------------------------------------------------------------- /img/stroke.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/img/stroke.png -------------------------------------------------------------------------------- /img/usage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingJA/SwitchButton/c36316ae6e8494031eb1c76588f63455580187b7/img/usage.gif -------------------------------------------------------------------------------- /libk-switchbutton/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /libk-switchbutton/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | android { 4 | compileSdkVersion COMPILE_SDK_VERSION as int 5 | buildToolsVersion BUILD_TOOLS_VERSION 6 | 7 | defaultConfig { 8 | minSdkVersion MIN_SDK_VERSION as int 9 | targetSdkVersion TARGET_SDK_VERSION as int 10 | versionCode VERSION_CODE as int 11 | versionName VERSION_NAME 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | compileOptions { 20 | sourceCompatibility JavaVersion.VERSION_1_8 21 | targetCompatibility JavaVersion.VERSION_1_8 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(include: ['*.jar'], dir: 'libs') 27 | } 28 | publish { 29 | artifactId = 'switchbutton' 30 | userOrg = 'kingja' 31 | groupId = 'lib.kingja.switchbutton' 32 | publishVersion = '1.1.8' 33 | desc = 'A smart switchable button,support multiple tabs.' 34 | website = 'https://github.com/KingJA/SwitchButton' 35 | } 36 | -------------------------------------------------------------------------------- /libk-switchbutton/libk-switchbutton.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 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 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /libk-switchbutton/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 D:\Android\SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /libk-switchbutton/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 20 | 21 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /libk-switchbutton/src/main/java/lib/kingja/switchbutton/SwitchMultiButton.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 KingJA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package lib.kingja.switchbutton; 19 | 20 | import android.content.Context; 21 | import android.content.res.TypedArray; 22 | import android.graphics.Canvas; 23 | import android.graphics.Paint; 24 | import android.graphics.Path; 25 | import android.graphics.RectF; 26 | import android.graphics.Typeface; 27 | import android.os.Bundle; 28 | import android.os.Parcelable; 29 | import android.text.TextPaint; 30 | import android.text.TextUtils; 31 | import android.util.AttributeSet; 32 | import android.view.MotionEvent; 33 | import android.view.View; 34 | 35 | /** 36 | * Description A smart switchable button,support multiple tabs. 37 | * Create Time 2016/7/27 14:57 38 | * Author KingJA 39 | * Email kingjavip@gmail.com 40 | */ 41 | public class SwitchMultiButton extends View { 42 | 43 | private static final String TAG = "SwitchMultiButton"; 44 | /*default value*/ 45 | private String[] mTabTexts = {"L", "R"}; 46 | private int mTabNum = mTabTexts.length; 47 | private static final float STROKE_RADIUS = 0; 48 | private static final float STROKE_WIDTH = 2; 49 | private static final float TEXT_SIZE = 14; 50 | private static final int SELECTED_COLOR = 0xffeb7b00; 51 | private static final int DISABLE_COLOR = 0xffcccccc; 52 | private static final int ENABLE_COLOR = 0xffffffff; 53 | private static final int SELECTED_TAB = 0; 54 | private static final String FONTS_DIR = "fonts/"; 55 | /*other*/ 56 | private Paint mStrokePaint; 57 | private Paint mFillPaint; 58 | private int mWidth; 59 | private int mHeight; 60 | private TextPaint mSelectedTextPaint; 61 | private TextPaint mUnselectedTextPaint; 62 | private OnSwitchListener onSwitchListener; 63 | private float mStrokeRadius; 64 | private float mStrokeWidth; 65 | private int mSelectedColor; 66 | private int mDisableColor; 67 | private float mTextSize; 68 | private int mSelectedTab; 69 | private float perWidth; 70 | private float mTextHeightOffset; 71 | private Paint.FontMetrics mFontMetrics; 72 | private Typeface typeface; 73 | private boolean mEnable = true; 74 | 75 | 76 | public SwitchMultiButton(Context context) { 77 | this(context, null); 78 | } 79 | 80 | public SwitchMultiButton(Context context, AttributeSet attrs) { 81 | this(context, attrs, 0); 82 | } 83 | 84 | public SwitchMultiButton(Context context, AttributeSet attrs, int defStyleAttr) { 85 | super(context, attrs, defStyleAttr); 86 | initAttrs(context, attrs); 87 | initPaint(); 88 | } 89 | 90 | /** 91 | * get the values of attributes 92 | * 93 | * @param context 94 | * @param attrs 95 | */ 96 | private void initAttrs(Context context, AttributeSet attrs) { 97 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchMultiButton); 98 | mStrokeRadius = typedArray.getDimension(R.styleable.SwitchMultiButton_strokeRadius, STROKE_RADIUS); 99 | mStrokeWidth = typedArray.getDimension(R.styleable.SwitchMultiButton_strokeWidth, STROKE_WIDTH); 100 | mTextSize = typedArray.getDimension(R.styleable.SwitchMultiButton_textSize, TEXT_SIZE); 101 | mSelectedColor = typedArray.getColor(R.styleable.SwitchMultiButton_selectedColor, SELECTED_COLOR); 102 | mDisableColor = typedArray.getColor(R.styleable.SwitchMultiButton_disableColor, DISABLE_COLOR); 103 | mSelectedTab = typedArray.getInteger(R.styleable.SwitchMultiButton_selectedTab, SELECTED_TAB); 104 | String mTypeface = typedArray.getString(R.styleable.SwitchMultiButton_typeface); 105 | int mSwitchTabsResId = typedArray.getResourceId(R.styleable.SwitchMultiButton_switchTabs, 0); 106 | if (mSwitchTabsResId != 0) { 107 | mTabTexts = getResources().getStringArray(mSwitchTabsResId); 108 | mTabNum = mTabTexts.length; 109 | } 110 | if (!TextUtils.isEmpty(mTypeface)) { 111 | typeface = Typeface.createFromAsset(context.getAssets(), FONTS_DIR + mTypeface); 112 | } 113 | typedArray.recycle(); 114 | } 115 | 116 | /** 117 | * define paints 118 | */ 119 | private void initPaint() { 120 | // round rectangle paint 121 | mStrokePaint = new Paint(); 122 | mStrokePaint.setColor(mSelectedColor); 123 | mStrokePaint.setStyle(Paint.Style.STROKE); 124 | mStrokePaint.setAntiAlias(true); 125 | mStrokePaint.setStrokeWidth(mStrokeWidth); 126 | // selected paint 127 | mFillPaint = new Paint(); 128 | mFillPaint.setColor(mSelectedColor); 129 | mFillPaint.setStyle(Paint.Style.FILL_AND_STROKE); 130 | mStrokePaint.setAntiAlias(true); 131 | // selected text paint 132 | mSelectedTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 133 | mSelectedTextPaint.setTextSize(mTextSize); 134 | mSelectedTextPaint.setColor(0xffffffff); 135 | mStrokePaint.setAntiAlias(true); 136 | // unselected text paint 137 | mUnselectedTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 138 | mUnselectedTextPaint.setTextSize(mTextSize); 139 | mUnselectedTextPaint.setColor(mSelectedColor); 140 | mStrokePaint.setAntiAlias(true); 141 | mTextHeightOffset = -(mSelectedTextPaint.ascent() + mSelectedTextPaint.descent()) * 0.5f; 142 | mFontMetrics = mSelectedTextPaint.getFontMetrics(); 143 | if (typeface != null) { 144 | mSelectedTextPaint.setTypeface(typeface); 145 | mUnselectedTextPaint.setTypeface(typeface); 146 | } 147 | } 148 | 149 | @Override 150 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 151 | int defaultWidth = getDefaultWidth(); 152 | int defaultHeight = getDefaultHeight(); 153 | setMeasuredDimension(getExpectSize(defaultWidth, widthMeasureSpec), getExpectSize(defaultHeight, 154 | heightMeasureSpec)); 155 | } 156 | 157 | /** 158 | * get default height when android:layout_height="wrap_content" 159 | */ 160 | private int getDefaultHeight() { 161 | return (int) (mFontMetrics.bottom - mFontMetrics.top) + getPaddingTop() + getPaddingBottom(); 162 | } 163 | 164 | /** 165 | * get default width when android:layout_width="wrap_content" 166 | */ 167 | private int getDefaultWidth() { 168 | float tabTextWidth = 0f; 169 | int tabs = mTabTexts.length; 170 | for (int i = 0; i < tabs; i++) { 171 | tabTextWidth = Math.max(tabTextWidth, mSelectedTextPaint.measureText(mTabTexts[i])); 172 | } 173 | float totalTextWidth = tabTextWidth * tabs; 174 | float totalStrokeWidth = (mStrokeWidth * tabs); 175 | int totalPadding = (getPaddingRight() + getPaddingLeft()) * tabs; 176 | return (int) (totalTextWidth + totalStrokeWidth + totalPadding); 177 | } 178 | 179 | 180 | /** 181 | * get expect size 182 | * 183 | * @param size 184 | * @param measureSpec 185 | * @return 186 | */ 187 | private int getExpectSize(int size, int measureSpec) { 188 | int result = size; 189 | int specMode = MeasureSpec.getMode(measureSpec); 190 | int specSize = MeasureSpec.getSize(measureSpec); 191 | switch (specMode) { 192 | case MeasureSpec.EXACTLY: 193 | result = specSize; 194 | break; 195 | case MeasureSpec.UNSPECIFIED: 196 | result = size; 197 | break; 198 | case MeasureSpec.AT_MOST: 199 | result = Math.min(size, specSize); 200 | break; 201 | default: 202 | break; 203 | } 204 | return result; 205 | } 206 | 207 | @Override 208 | protected void onDraw(Canvas canvas) { 209 | super.onDraw(canvas); 210 | if (!mEnable) { 211 | mStrokePaint.setColor(mDisableColor); 212 | mFillPaint.setColor(mDisableColor); 213 | mSelectedTextPaint.setColor(ENABLE_COLOR); 214 | mUnselectedTextPaint.setColor(mDisableColor); 215 | } 216 | float left = mStrokeWidth * 0.5f; 217 | float top = mStrokeWidth * 0.5f; 218 | float right = mWidth - mStrokeWidth * 0.5f; 219 | float bottom = mHeight - mStrokeWidth * 0.5f; 220 | 221 | //draw rounded rectangle 222 | canvas.drawRoundRect(new RectF(left, top, right, bottom), mStrokeRadius, mStrokeRadius, mStrokePaint); 223 | 224 | //draw line 225 | for (int i = 0; i < mTabNum - 1; i++) { 226 | canvas.drawLine(perWidth * (i + 1), top, perWidth * (i + 1), bottom, mStrokePaint); 227 | } 228 | //draw tab and line 229 | for (int i = 0; i < mTabNum; i++) { 230 | String tabText = mTabTexts[i]; 231 | float tabTextWidth = mSelectedTextPaint.measureText(tabText); 232 | if (i == mSelectedTab) { 233 | //draw selected tab 234 | if (i == 0) { 235 | drawLeftPath(canvas, left, top, bottom); 236 | } else if (i == mTabNum - 1) { 237 | drawRightPath(canvas, top, right, bottom); 238 | } else { 239 | canvas.drawRect(new RectF(perWidth * i, top, perWidth * (i + 1), bottom), mFillPaint); 240 | } 241 | // draw selected text 242 | canvas.drawText(tabText, 0.5f * perWidth * (2 * i + 1) - 0.5f * tabTextWidth, mHeight * 0.5f + 243 | mTextHeightOffset, mSelectedTextPaint); 244 | } else { 245 | //draw unselected text 246 | canvas.drawText(tabText, 0.5f * perWidth * (2 * i + 1) - 0.5f * tabTextWidth, mHeight * 0.5f + 247 | mTextHeightOffset, mUnselectedTextPaint); 248 | } 249 | } 250 | } 251 | 252 | /** 253 | * draw right path 254 | * 255 | * @param canvas 256 | * @param left 257 | * @param top 258 | * @param bottom 259 | */ 260 | private void drawLeftPath(Canvas canvas, float left, float top, float bottom) { 261 | Path leftPath = new Path(); 262 | leftPath.moveTo(left + mStrokeRadius, top); 263 | leftPath.lineTo(perWidth, top); 264 | leftPath.lineTo(perWidth, bottom); 265 | leftPath.lineTo(left + mStrokeRadius, bottom); 266 | leftPath.arcTo(new RectF(left, bottom - 2 * mStrokeRadius, left + 2 * mStrokeRadius, bottom), 90, 90); 267 | leftPath.lineTo(left, top + mStrokeRadius); 268 | leftPath.arcTo(new RectF(left, top, left + 2 * mStrokeRadius, top + 2 * mStrokeRadius), 180, 90); 269 | canvas.drawPath(leftPath, mFillPaint); 270 | } 271 | 272 | /** 273 | * draw left path 274 | * 275 | * @param canvas 276 | * @param top 277 | * @param right 278 | * @param bottom 279 | */ 280 | private void drawRightPath(Canvas canvas, float top, float right, float bottom) { 281 | Path rightPath = new Path(); 282 | rightPath.moveTo(right - mStrokeRadius, top); 283 | rightPath.lineTo(right - perWidth, top); 284 | rightPath.lineTo(right - perWidth, bottom); 285 | rightPath.lineTo(right - mStrokeRadius, bottom); 286 | rightPath.arcTo(new RectF(right - 2 * mStrokeRadius, bottom - 2 * mStrokeRadius, right, bottom), 90, -90); 287 | rightPath.lineTo(right, top + mStrokeRadius); 288 | rightPath.arcTo(new RectF(right - 2 * mStrokeRadius, top, right, top + 2 * mStrokeRadius), 0, -90); 289 | canvas.drawPath(rightPath, mFillPaint); 290 | } 291 | 292 | 293 | /** 294 | * called after onMeasure 295 | * 296 | * @param w 297 | * @param h 298 | * @param oldw 299 | * @param oldh 300 | */ 301 | @Override 302 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 303 | super.onSizeChanged(w, h, oldw, oldh); 304 | mWidth = getMeasuredWidth(); 305 | mHeight = getMeasuredHeight(); 306 | perWidth = mWidth / mTabNum; 307 | checkAttrs(); 308 | } 309 | 310 | /** 311 | * check attribute whehere suitable 312 | */ 313 | private void checkAttrs() { 314 | if (mStrokeRadius > 0.5f * mHeight) { 315 | mStrokeRadius = 0.5f * mHeight; 316 | } 317 | } 318 | 319 | /** 320 | * receive the event when touched 321 | * 322 | * @param event 323 | * @return 324 | */ 325 | @Override 326 | public boolean onTouchEvent(MotionEvent event) { 327 | if (!mEnable) { 328 | return true; 329 | } 330 | if (event.getAction() == MotionEvent.ACTION_UP) { 331 | float x = event.getX(); 332 | for (int i = 0; i < mTabNum; i++) { 333 | if (x > perWidth * i && x < perWidth * (i + 1)) { 334 | if (mSelectedTab == i) { 335 | return true; 336 | } 337 | mSelectedTab = i; 338 | if (onSwitchListener != null) { 339 | onSwitchListener.onSwitch(i, mTabTexts[i]); 340 | } 341 | } 342 | } 343 | invalidate(); 344 | } 345 | return true; 346 | } 347 | 348 | @Override 349 | public void setEnabled(boolean enabled) { 350 | if (enabled == isEnabled()) { 351 | return; 352 | } 353 | mEnable = enabled; 354 | invalidate(); 355 | } 356 | 357 | @Override 358 | public boolean isEnabled() { 359 | return mEnable; 360 | } 361 | /*=========================================Interface=========================================*/ 362 | 363 | /** 364 | * called when swtiched 365 | */ 366 | public interface OnSwitchListener { 367 | void onSwitch(int position, String tabText); 368 | } 369 | 370 | public SwitchMultiButton setOnSwitchListener(OnSwitchListener onSwitchListener) { 371 | this.onSwitchListener = onSwitchListener; 372 | return this; 373 | } 374 | 375 | /*=========================================Set and Get=========================================*/ 376 | 377 | /** 378 | * get position of selected tab 379 | */ 380 | public int getSelectedTab() { 381 | return mSelectedTab; 382 | } 383 | 384 | /** 385 | * set selected tab 386 | * 387 | * @param mSelectedTab 388 | * @return 389 | */ 390 | public SwitchMultiButton setSelectedTab(int mSelectedTab) { 391 | this.mSelectedTab = mSelectedTab; 392 | invalidate(); 393 | if (onSwitchListener != null) { 394 | onSwitchListener.onSwitch(mSelectedTab, mTabTexts[mSelectedTab]); 395 | } 396 | return this; 397 | } 398 | 399 | public void clearSelection() { 400 | this.mSelectedTab = -1; 401 | invalidate(); 402 | } 403 | 404 | /** 405 | * set data for the switchbutton 406 | * 407 | * @param tagTexts 408 | * @return 409 | */ 410 | public SwitchMultiButton setText(String... tagTexts) { 411 | if (tagTexts.length > 1) { 412 | this.mTabTexts = tagTexts; 413 | mTabNum = tagTexts.length; 414 | requestLayout(); 415 | return this; 416 | } else { 417 | throw new IllegalArgumentException("the size of tagTexts should greater then 1"); 418 | } 419 | } 420 | /*======================================save and restore======================================*/ 421 | 422 | @Override 423 | protected Parcelable onSaveInstanceState() { 424 | Bundle bundle = new Bundle(); 425 | bundle.putParcelable("View", super.onSaveInstanceState()); 426 | bundle.putFloat("StrokeRadius", mStrokeRadius); 427 | bundle.putFloat("StrokeWidth", mStrokeWidth); 428 | bundle.putFloat("TextSize", mTextSize); 429 | bundle.putInt("SelectedColor", mSelectedColor); 430 | bundle.putInt("DisableColor", mDisableColor); 431 | bundle.putInt("SelectedTab", mSelectedTab); 432 | bundle.putBoolean("Enable", mEnable); 433 | return bundle; 434 | } 435 | 436 | @Override 437 | protected void onRestoreInstanceState(Parcelable state) { 438 | if (state instanceof Bundle) { 439 | Bundle bundle = (Bundle) state; 440 | mStrokeRadius = bundle.getFloat("StrokeRadius"); 441 | mStrokeWidth = bundle.getFloat("StrokeWidth"); 442 | mTextSize = bundle.getFloat("TextSize"); 443 | mSelectedColor = bundle.getInt("SelectedColor"); 444 | mDisableColor = bundle.getInt("DisableColor"); 445 | mSelectedTab = bundle.getInt("SelectedTab"); 446 | mEnable = bundle.getBoolean("Enable"); 447 | super.onRestoreInstanceState(bundle.getParcelable("View")); 448 | } else { 449 | super.onRestoreInstanceState(state); 450 | } 451 | } 452 | } 453 | -------------------------------------------------------------------------------- /libk-switchbutton/src/main/res/values/attr.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /libk-switchbutton/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | LibK_SwitchButton 20 | 21 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Fri Jan 11 23:26:34 CST 2019 8 | sdk.dir=D\:\\Android\\SDK 9 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':libk-switchbutton' 2 | --------------------------------------------------------------------------------