├── .gitignore ├── CHANGELOG.md ├── README.md ├── SwitchButton ├── .gitignore ├── build.gradle ├── demo │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── kyleduo │ │ │ └── switchbutton │ │ │ └── switchbutton │ │ │ └── ApplicationTest.java │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── kyleduo │ │ │ └── switchbutton │ │ │ └── demo │ │ │ ├── MainActivity.java │ │ │ ├── RecyclerActivity.java │ │ │ ├── StyleActivity.java │ │ │ ├── StyleInCodeActivity.java │ │ │ └── UseActivity.java │ │ └── res │ │ ├── color │ │ ├── custom_back_color.xml │ │ ├── custom_text_state_color.xml │ │ └── custom_thumb_color.xml │ │ ├── drawable-xhdpi │ │ ├── ios_thumb.png │ │ └── ios_thumb_disable.png │ │ ├── drawable-xxhdpi │ │ ├── icon_blog.png │ │ ├── icon_blog_small.png │ │ └── icon_github.png │ │ ├── drawable │ │ ├── flyme_back_drawable.xml │ │ ├── flyme_thumb_drawable.xml │ │ ├── ios_back_drawable.xml │ │ ├── ios_off.xml │ │ ├── ios_thumb_selector.xml │ │ ├── miui_back_drawable.xml │ │ └── miui_thumb_drawable.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_recycler.xml │ │ ├── activity_style.xml │ │ ├── activity_style_in_code.xml │ │ ├── activity_use.xml │ │ └── layout_recycler_item.xml │ │ ├── menu │ │ └── menu_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-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library │ ├── .gitignore │ ├── build.gradle │ ├── gradle.properties │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── kyleduo │ │ │ └── switchbutton │ │ │ └── switchbutton │ │ │ └── ApplicationTest.java │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── kyleduo │ │ │ └── switchbutton │ │ │ ├── ColorUtils.java │ │ │ └── SwitchButton.java │ │ └── res │ │ ├── color │ │ └── ksw_md_back_color.xml │ │ ├── drawable │ │ └── ksw_md_thumb.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml └── settings.gradle ├── demo └── switchbutton_demo_200.apk └── images ├── demo_preview.png └── how_switchbutton_measure.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | .idea/ 3 | 4 | # Android Stuido profile 5 | *.iml 6 | 7 | # Mobile Tools for Java (J2ME) 8 | .mtj.tmp/ 9 | 10 | # Package Files # 11 | *.war 12 | *.ear 13 | 14 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 15 | hs_err_pid* 16 | 17 | # ignore gen 18 | bin/ 19 | gen/ 20 | .DS_Store 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | Change Log 3 | ============ 4 | 5 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-SwitchButton-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1119) 6 | 7 | **To get a quick preview, you can find Demo apk in [Google Play](https://play.google.com/store/apps/details?id=com.kyleduo.switchbutton.demo) or [Directly download](./demo/switchbutton_demo_141.apk).** 8 | 9 | This project provides you a convenient way to use and customise a SwitchButton widget in Android. With just resources changed and attrs set, you can create a lifelike SwitchButton of Android 5.0+, iOS, MIUI, or Flyme and so on. 10 | 11 | Now we get the biggest movement since SwitchButton published. v1.3.0 comes with totally reconsitution and more convenient API. A wholly new demo can give you a tour in it. 12 | 13 | *** 14 | 15 | ## 2.1.0 (Latest) 16 | 17 | **ENG** 18 | 19 | 1. Migrate to maven central 20 | 21 | 22 | **CHN** 23 | 24 | 1. 迁移到 maven central 25 | 26 | ## 2.0.3 27 | 28 | **ENG** 29 | 30 | 1. Fix [#103](https://github.com/kyleduo/SwitchButton/issues/103). Determin whether state change is triggered manully using `isPressed` in OnCheckedChangeListener is supported. 31 | 2. Remove targetSdkVersion declaration. 32 | 3. Upgrate compileSdkVersion to 29. 33 | 34 | **CHN** 35 | 36 | 1. 修复 [#103](https://github.com/kyleduo/SwitchButton/issues/103)。支持在OnCheckedChangeListener中使用`isPressed`判断是否是手动触发。 37 | 2. 移除targetSdkVersion声明 38 | 3. 更新compileSdkVersion到29 39 | 40 | ## 2.0.2 41 | 42 | **ENG** 43 | 44 | 1. Fix [#122](https://github.com/kyleduo/SwitchButton/issues/122). Support sub-class extending from SwitchButton. 45 | 2. Fix bug which move faster than expected while touch and move vertically. 46 | 3. Remove dependency of AppCompat library. 47 | 48 | 49 | **CHN** 50 | 51 | 1. 修复 [#122](https://github.com/kyleduo/SwitchButton/issues/122)。支持从SwitchButton集成子类。 52 | 2. 修复纵向滑动导致移动过快的Bug。 53 | 3. 移除对AppCompat库的依赖。 54 | 55 | 56 | ## 2.0.0 57 | 58 | **ENG** 59 | 60 | 1. Re-clarify the meaning of some params. 61 | 2. Update the measurement of SwitchButton and the logic becomes more clear, especially the text part. 62 | 3. Support config SwitchButton's size by setting a exact width and height. There are now TWO mainly method to control it's size. 63 | 64 | 65 | **CHN** 66 | 67 | 1. 重新明确了参数的含义。 68 | 2. 更新了SwitchButton的测量机制,逻辑更加清晰;尤其是文字部分。 69 | 3. 支持设置确定的宽高,来确定SwitchButton的View大小。现在有两种方式可以控制SwitchButton的大小了。 70 | 71 | 72 | 73 | ## 1.4.6 74 | 75 | - Fixed [#89](https://github.com/kyleduo/SwitchButton/issues/89) . 76 | 77 | ## 1.4.5 78 | 79 | - Fixed [#75](https://github.com/kyleduo/SwitchButton/issues/75) [#78](https://github.com/kyleduo/SwitchButton/issues/78) [#85](https://github.com/kyleduo/SwitchButton/issues/85). 80 | 81 | 82 | 83 | 1.4.4 84 | --- 85 | 86 | * Fixed [#65](https://github.com/kyleduo/SwitchButton/issues/65). 87 | * Update text layout, tests looks like center. 88 | 89 | 90 | 1.4.3 91 | --- 92 | 93 | * Fixed [#64](https://github.com/kyleduo/SwitchButton/issues/64). Respect to clickable and focusable attributes. 94 | 95 | 96 | 1.4.2 97 | --- 98 | 99 | * Support [#49](https://github.com/kyleduo/SwitchButton/issues/60). By default SwitchButton found accentColor of your theme for tintColor. (**accentColor** is used for controls according to [Material Design guideline](https://material.google.com/style/color.html#color-color-schemes). ) 100 | 101 | 102 | 1.4.1 103 | --- 104 | 105 | * fixed [#49](https://github.com/kyleduo/SwitchButton/issues/49). 106 | * Support operation without onCheckedChanged callback. 107 | 108 | 109 | 1.4.0 110 | --- 111 | 112 | * Add text feature. You can set text for either checked or unchecked status with __kswTextOn__ and __kswTextOff__ attrs. And you can set the margin of text in horizontal direction using __kswTextMarginH__ attr. 113 | * For additional, you can set small icons now by using SpannableString with __setText()__ method in code. 114 | * Thanks [@lpmfilho](https://github.com/lpmfilho) 115 | 116 | *** 117 | 118 | 1.3.4 119 | --- 120 | 121 | * Fix [#40](https://github.com/kyleduo/SwitchButton/issues/40) 122 | 123 | *** 124 | 125 | 1.3.3 126 | --- 127 | 128 | * Fix SwitchButtonMD style bug in RecyclerView and add page for test. 129 | *Fix bug in setCheckedImmediately() in onCheckedChanged() method. **(setChecked in onChecked)** 130 | 131 | *** 132 | 133 | 1.3.2 134 | --- 135 | 136 | * **setClickable(boolean)** support. 137 | * Bug fix. 138 | 139 | *** 140 | 141 | 1.3.1 142 | --- 143 | * Remove shadow of MD style to support under 5.0. 144 | 145 | *** 146 | 147 | 1.3.0 148 | --- 149 | * Reconstructe the whole library. 150 | * More convenient customization way by __tintColor__. 151 | * New design demo. All APIs in ONE. 152 | *Fix issue [#23](https://github.com/kyleduo/SwitchButton/issues/23) [#25](https://github.com/kyleduo/SwitchButton/issues/25) [#26](https://github.com/kyleduo/SwitchButton/issues/26) 153 | *Just exciting!!! 154 | 155 | *** 156 | 157 | 158 | 1.2.10 159 | --- 160 | * Fix issue [#22](https://github.com/kyleduo/SwitchButton/issues/22) by change the attributes' name to prevent conflict; 161 | 162 | *** 163 | 164 | 165 | 1.2.9 166 | --- 167 | * Fix issue [#19](https://github.com/kyleduo/SwitchButton/issues/19). 168 | 169 | *** 170 | 171 | 172 | 1.2.8 173 | --- 174 | * Fix stretch bug while using higher API. 175 | *Add Gradle support. 176 | *Built in Android Studio. 177 | 178 | *** 179 | 180 | 181 | 1.2.7 182 | --- 183 | * Fix rendering bug on some devices. 184 | *Fix states bug. 185 | 186 | *** 187 | 188 | 189 | 1.2.6 190 | --- 191 | * With calling the method ___setChecked(boolean, false);___, you can change the status without invoking the listener. 192 | 193 | *** 194 | 195 | 196 | 1.2.5 197 | --- 198 | * Fix shrink bug in Android 5.0 (the problem is same like it is in Android 4.4, which has been fixed in 1.2.4). 199 | *More available to setup Material Design style SwitchButton using ___@style/MD___ in xml layout. 200 | *Fix Demo Project bug 201 | 202 | *** 203 | 204 | 205 | 1.2.4 206 | --- 207 | * fix shrink bug(that will cause the content out of bounds not disapper, on Android 4.4) 208 | * upload .pad resource, whitch I forgot to upload before.(My fault.) 209 | 210 | *** 211 | 212 | 213 | 1.2.3 214 | --- 215 | * bug fix 216 | * upgrade demo apk 217 | * more clear way to use 218 | 219 | Since the animation ran on sub thread, "toggle" and "setChecked" methods may mot execute as you wish what cause the checked status change immediately. 220 | 221 | This problem may be solved, but I declared 2 methods to deal with the situation whether you want it happen immediately. 222 | 223 | If you want to set the checked status immediately, just call ___setChecked(boolean checked);___ method like CheckBox. And ___slideToChecked(boolean checked);___ method for slow one with slide animation. 224 | 225 | When toggle, call ___toggle();___ with change the status immediately and ___toggle(false);___ for slow one. 226 | 227 | 228 | ![easy_to_use](https://raw.githubusercontent.com/kyleduo/SwitchButton/master/preview/easy_to_use_128.png) 229 | 230 | *** 231 | 232 | 1.2 233 | --- 234 | (11/08/2014) 235 | 236 | * Add StateList support for all resources and enable/disable, pressed has been tested in Demo. 237 | * New Style with __Material Design__, preview below. 238 | * Add "shrink" feature. This makes you can draw your image resources out size the view bounds, in the other word, shrink the size of view and make the experience stay the same. 239 | 240 | New Style with Material Design: 241 | 242 | ![materialdesign_style](https://raw.githubusercontent.com/kyleduo/SwitchButton/master/preview/switchbutton_md.jpg) 243 | 244 | To use shrink feature, you can easily add these attributes in your xml file. It is recommended that set these values positive. 245 | 246 | * __insetLeft__: left size for shrinking 247 | * __insetRight__: right size for shrinking 248 | * __insetTop__: top size for shrinking 249 | * __insetBottom__: bottom size for shrinking 250 | 251 | *** 252 | 253 | 254 | 1.1 255 | --- 256 | (10/08/2014) 257 | 258 | * Fix lots of bugs. 259 | * Change the __default style__ 260 | * Add iOS7 style, you can just use like how the demo did 261 | * Update demo, it becomes more convenient, effective and beautiful 262 | * Add new attribute: measureFactor, you can custom the factor between width and height now. It is convenient to config the rest space of background. Learn more in the demo. 263 | * Update the logic of thumbMargin, it can work well with negative margins now (iOS style just using this trick). 264 | 265 | new default style and demo apk looks like this: 266 | 267 | ![default_style](https://raw.githubusercontent.com/kyleduo/SwitchButton/master/preview/default_style.png) 268 | 269 | ![demo_preview](https://raw.githubusercontent.com/kyleduo/SwitchButton/master/preview/easy_to_style_128.png) 270 | 271 | *** 272 | 273 | 274 | 1.0 275 | --- 276 | Add an attr of radius, now you can change the radius when configure the button's face! 277 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | SwitchButton 3 | ============ 4 | 5 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-SwitchButton-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1119) 6 | 7 | **To get a quick preview, you can get Demo apk in [Google Play](https://play.google.com/store/apps/details?id=com.kyleduo.switchbutton.demo) or [Directly download](./demo/switchbutton_demo_200.apk).** 8 | 9 | This project provides you a convenient way to use and customise a SwitchButton widget in Android. 10 | 11 | *** 12 | 13 | 14 | Change Log 15 | --- 16 | 17 | [All Change Log](./CHANGELOG.md) 18 | 19 | **Some attributes are changed in 2.0.0 and you need to update them to the new ones, since the measurement logic has been totally changed. (Default behavior and style does not change.)** 20 | 21 | **因为2.0.0测量逻辑的改变,部分属性已经做了修改,请更新到新的属性。(默认行为和样式并没有变化)** 22 | 23 | > 1. Re-clarify the meaning of some params. 24 | > 2. Update the measurement of SwitchButton and the logic becomes more clear, especially the text part. 25 | > 3. Support config SwitchButton's size by setting a exact width and height. There are now TWO mainly method to control it's size. 26 | > 4. When use SwitchButon in a scrollable view, SwitchButton consume scroll gestures only horizontal. This means the when you want to scroll the scrollable view vertically and start your touch on a SwitchButton, it will not stop you now. 27 | > 5. Bug fix. 28 | 29 | >1. 重新明确了参数的含义。 30 | >2. 更新了SwitchButton的测量机制,逻辑更加清晰;尤其是文字部分。 31 | >3. 支持设置确定的宽高,来确定SwitchButton的View大小。现在有两种方式可以控制SwitchButton的大小了。 32 | >4. 可滚动的View中的SwitchButton只会消费横向滚动事件。这意味着你可以从SwitchButton开始按下并纵向滚动可滚动View,SwitchButton现在不会阻止你滚动了。 33 | >5. Bug 修复。 34 | 35 | 36 | Using SwitchButton in your application 37 | --- 38 | 39 | **In Gradle** 40 | ​ 41 | Add dependencies in build.gradle of your module 42 | 43 | ```groovy 44 | dependencies { 45 | implementation 'com.kyleduo.switchbutton:library:2.1.0' 46 | } 47 | ``` 48 | 49 | > Version 2.1.0 can be pulled from `mavenCentral()` 50 | 51 | #### Migrate to 2.0.0 (迁移到2.0.0) 52 | 53 | **ENG** 54 | 55 | **There is a big diagram below to show how SwitchButton measure it self in 2.0.0. It is strongly recommended that you should check it out.** 56 | 57 | 1. **kswBackMeasureRatio** has been removed from SwitchButton attributes since it has an ambiguous meaning. I've add the new **kswThumbRangeRatio** attribute to represent how much multiple the scroll range of thumb than the width of thumb. 58 | 2. **kswTextMarginH** and **kswAutoAdjustTextPosition** have been removed from SwitchButton attributes since I updated the measurement logic of text part. And these two attributes do not represent the back meaning well. **kswTextThumbInset**, **kswTextExtra** and **kswTextAdjust** was introduced to represent "how much the text go under thumb", "how much extra space do you want around the text" and "how much to move the text from the center of text area to adjust the text's position". There are all shown on the diagram. 59 | 3. Setters and getters are also changed due to the change of attributes. 60 | 61 | **CHN** 62 | 63 | 下面有一张图表来解释SwitchButton在2.0.0版本中是如何进行测量的,非常建议你看一看。 64 | 65 | 1. **kswBackMeasureRatio** 属性被移除了,因为名称有歧义。新增加的 **kswThumbRangeRatio** 属性表示thumb移动区域和thumb宽度的比值。 66 | 2. **kswTextMarginH** 和 **kswAutoAdjustTextPosition** 属性被移除了,因为我更新了对文字的测量逻辑,而且这两个属性名称表意不明确。我增加了**kswTextThumbInset**, **kswTextExtra** 和 **kswTextAdjust** 这三个新属性来分别表示“文字在thumb下面的距离”,“额外文字空间”和”文字调节距离“。这些都在图表中有所体现。 67 | 3. setter和getter都跟随属性名称的改变而进行了改变。 68 | 69 | ### Diagram: How SwitchButton Measure 70 | 71 | This diagram shows how SwitchButton measure itself and what does those nouns mean. To measure width is much complex than the height, so if you know how to measure width, you know how to measure height. And text measurement and location increase the complexity. 72 | 73 | ![demo_preview](./images/how_switchbutton_measure.png) 74 | 75 | *** 76 | 77 | 78 | Demo 79 | --- 80 | I create a new demo apk to show you how to style the cute widget and use it. There's some screenshots of the new demo. 81 | 82 | ![demo_preview](./images/demo_preview.png) 83 | 84 | *** 85 | 86 | 87 | Usage 88 | --- 89 | 90 | The usage of SwitchButton is just like CheckBox. The basic control APIs of SwitchButton. 91 | 92 | * __setChecked(boolean)__ 93 | * __toggle()__ 94 | 95 | Since SwitchButton has addition animation when checked status changed, there are two addition methods for disable animation for single operation. 96 | 97 | * __setCheckedImmediately(boolean)__: like setChecked but NO animation. 98 | * __toggleImmediately()__: like toggle but NO animation. 99 | 100 | From **version 1.4.1** on, SwitchButton support operation without onCheckedChanged callback. It makes changing state in code more convenient. Using these methods to achieve that feature. 101 | 102 | 103 | * __setCheckedNoEvent(boolean)__ 104 | * __setCheckedImmediatelyNoEvent(boolean)__ 105 | * __toggleNoEvent()__ 106 | * __toggleImmediatelyNoEvent()__ 107 | 108 | *** 109 | 110 | 111 | Style 112 | --- 113 | 114 | In 1.3.0, I updated the usage of __SwitchButton__ library. To make it more Android way to use, I've combined the thumb and back style each to StateListColor/StateListDrawable. So you are free to create styles in different states. 115 | 116 | In __xml__ layout file, you can configure the face of switch button using these attrs. 117 | 118 | * __kswThumbDrawable__: drawable for thumb 119 | * __kswThumbColor__: color for thumb 120 | * __kswThumbMargin__: margin from thumb to back, can be negative. maybe cover by single direction margins 121 | * __kswThumbMarginTop__: same to __kswThumbMargin__, just top 122 | * __kswThumbMarginBottom__: same to __kswThumbMargin__, just bottom 123 | * __kswThumbMarginLeft__: same to __kswThumbMargin__, just left 124 | * __kswThumbMarginRight__: same to __kswThumbMargin__, just right 125 | * __kswThumbWidth__: width of thumb 126 | * __kswThumbHeight__: height of thumb 127 | * __kswThumbRadius__: radius of thumb rect, only work with __kswThumbColor__ 128 | * __kswBackRadius__: radius of background rect, only work with __kswBackColor__ 129 | * __kswBackDrawable__: drawable for background 130 | * __kswBackColor__: color for background 131 | * __kswFadeBack__: fade background color/drawable when drag or animate between on/off status or not 132 | * __kswAnimationDuration__: duration of animation between 2 status 133 | * __kswTintColor__: change SwitchButton's style just by __one__ property, all relevant color will be generate automatically. Do not support `SwitchButtonMD` or other style created by xml resources. 134 | * __kswTextOn__: text for checked status. 135 | * __kswTextOff__: text for unchecked status. 136 | * __kswTextThumbInset (since 2.0.0)__: length of the part of text under the thumb. 137 | * __kswTextExtra (since 2.0.0)__: extra space needed by background besides the actual text width. 138 | * __kswTextAdjust (since 2.0.0)__: move the text after position text on the center of text area. 139 | * **kswThumbRangeRatio (since 2.0.0)**: (thumb move range width / thumb's width). float value. *see measure diagram* 140 | * ~~__kswBackMeasureRatio__: (background's width / thumb's width). float value.~~ *Removed since 2.0.0* 141 | * ~~**kswTextMarginH**: horizontal margin of text.~~ *Removed since 2.0.0* 142 | * ~~__kswAutoAdjustTextPosition__: **(since 1.4.4)** whether auto adjust text position to make them looks centered (NOT really centered) when there are round corners. You should set this to false when you don't need this feature.~~ *Removed since 2.0.0* 143 | 144 | You can alse change the configuration of SwitchButton ___in code___. You can find the api from Demo apk. There's a glance. 145 | 146 | ```java 147 | private String[] opts = new String[]{ 148 | "setThumbColorRes/setThumbColor", 149 | "setThumbDrawableRes/setThumbDrawable", 150 | "setBackColorRes/setBackColor", 151 | "setBackDrawableRes/setBackDrawable", 152 | "setTintColor", 153 | "setThumbMargin", 154 | "setThumbSize", 155 | "setThumbRadius (color-mode only)", 156 | "setBackRadius (color-mode only)", 157 | "setFadeBack", 158 | "setBackMeasureRatio", 159 | "setAnimationDuration", 160 | "setDrawDebugRect", 161 | "setText", 162 | }; 163 | ``` 164 | 165 | 166 | Beautiful Apps 167 | --- 168 | 169 | If you're using SwitchButton in your app, wish you can email me these infomation of your app and I'll create a list here. And that should be an utmost encouragement to me. :-) [kyleduo@gmail.com](mailto:kyleduo@gmail.com) 170 | 171 | | App | Name | Description | Markets | Developer | 172 | | ---------------------------------------- | ----------------------- | ---------------------------------------- | ---------------------------------------- | ---------------------------------------- | 173 | | ![headlines.png](https://static.kyleduo.com/headlines.png?imageView/2/w/80/) | Headlines | Headlines is a news dashboard for your smart TV. | [![market_logo_google_play.png](https://static.kyleduo.com/market_logo_google_play.png?imageView/2/w/80)](https://play.google.com/store/apps/details?id=com.mystraldesign.headlines)[![amazon-underground-app-us-black.png](https://static.kyleduo.com/amazon-underground-app-us-black.png?imageView/2/w/80)](https://www.amazon.com/Headlines-news-your-big-screen/dp/B01G93GRGC/ref=sr_1_1?ie=UTF8&qid=1487843182&sr=8-1&keywords=mystral+design) | [MYSTRAL](http://www.mystraldesign.com/) | 174 | | ![lantouzi.png](https://static.kyleduo.com/lantouzi.png?imageView/2/w/80) | 懒投资 | 专业安全的投资理财平台 | [官方网站](https://lantouzi.com/mobile/download) | [懒投资 lantouzi.com](https://lantouzi.com) | 175 | | ![notifications_in_bubble.png](https://static.kyleduo.com/project/switchbutton/apps/logo/notifications_in_bubble.png) | Notifications in bubble | Access all notifications from a floating bubble. | [![market_logo_google_play.png](https://static.kyleduo.com/market_logo_google_play.png?imageView/2/w/80)](https://play.google.com/store/apps/details?id=com.ram.chocolate.nm.premium) | [BestAppzz](https://play.google.com/store/apps/developer?id=BestAppzz) | 176 | 177 | 178 | License 179 | --- 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /SwitchButton/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | .DS_Store 4 | /build 5 | 6 | .idea/ 7 | *.iml 8 | -------------------------------------------------------------------------------- /SwitchButton/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:8.0.0' 10 | classpath "com.vanniktech:gradle-maven-publish-plugin:0.25.3" 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | mavenCentral() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /SwitchButton/demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /SwitchButton/demo/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id('com.android.application') 3 | } 4 | 5 | android { 6 | compileSdk 33 7 | 8 | namespace "com.kyleduo.switchbutton.demo" 9 | 10 | defaultConfig { 11 | applicationId "com.kyleduo.switchbutton.demo" 12 | minSdkVersion 14 13 | targetSdk 33 14 | versionCode Integer.parseInt(VERSION_CODE) 15 | versionName VERSION_NAME 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled true 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | implementation("androidx.appcompat:appcompat:1.6.1") 29 | implementation("androidx.recyclerview:recyclerview:1.3.2") 30 | implementation project(':library') 31 | // implementation 'com.kyleduo.switchbutton:library:2.0.3' 32 | } 33 | -------------------------------------------------------------------------------- /SwitchButton/demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/kyle/Documents/developer/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 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/androidTest/java/com/kyleduo/switchbutton/switchbutton/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.kyleduo.switchbutton.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 | } -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 27 | 28 | 32 | 35 | 36 | 40 | 43 | 44 | 48 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/java/com/kyleduo/switchbutton/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.kyleduo.switchbutton.demo; 2 | 3 | import android.content.Intent; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import android.view.View; 9 | import android.widget.AdapterView; 10 | import android.widget.ListView; 11 | 12 | import androidx.appcompat.app.AppCompatActivity; 13 | 14 | 15 | public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | ListView listView = (ListView) findViewById(R.id.list); 22 | listView.setOnItemClickListener(this); 23 | } 24 | 25 | 26 | @Override 27 | public boolean onCreateOptionsMenu(Menu menu) { 28 | getMenuInflater().inflate(R.menu.menu_main, menu); 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean onOptionsItemSelected(MenuItem item) { 34 | Intent intent = new Intent(Intent.ACTION_VIEW); 35 | int id = item.getItemId(); 36 | if (id == R.id.action_github) { 37 | intent.setData(Uri.parse("https://github.com/kyleduo/SwitchButton")); 38 | startActivity(intent); 39 | return true; 40 | } else if (id == R.id.action_blog) { 41 | intent.setData(Uri.parse("https://kyleduo.com")); 42 | startActivity(intent); 43 | return true; 44 | } 45 | return super.onOptionsItemSelected(item); 46 | } 47 | 48 | private void jumpToStyle() { 49 | startActivity(new Intent(this, StyleActivity.class)); 50 | } 51 | 52 | private void jumpToStyleInCode() { 53 | startActivity(new Intent(this, StyleInCodeActivity.class)); 54 | } 55 | 56 | private void jumpToUse() { 57 | startActivity(new Intent(this, UseActivity.class)); 58 | } 59 | 60 | private void jumpToRecycler() { 61 | startActivity(new Intent(this, RecyclerActivity.class)); 62 | } 63 | 64 | private void gotoBlog() { 65 | Intent intent = new Intent(Intent.ACTION_VIEW); 66 | intent.setData(Uri.parse("https://kyleduo.com")); 67 | startActivity(intent); 68 | } 69 | 70 | private void gotoLicense() { 71 | Intent intent = new Intent(Intent.ACTION_VIEW); 72 | intent.setData(Uri.parse("https://www.apache.org/licenses/LICENSE-2.0")); 73 | startActivity(intent); 74 | } 75 | 76 | 77 | @Override 78 | public void onItemClick(AdapterView parent, View view, int position, long id) { 79 | switch (position) { 80 | case 0: 81 | jumpToStyle(); 82 | break; 83 | case 1: 84 | jumpToStyleInCode(); 85 | break; 86 | case 2: 87 | jumpToUse(); 88 | break; 89 | case 3: 90 | jumpToRecycler(); 91 | break; 92 | case 4: 93 | gotoBlog(); 94 | break; 95 | case 5: 96 | gotoLicense(); 97 | break; 98 | 99 | default: 100 | break; 101 | } 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/java/com/kyleduo/switchbutton/demo/RecyclerActivity.java: -------------------------------------------------------------------------------- 1 | package com.kyleduo.switchbutton.demo; 2 | 3 | import android.os.Bundle; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.CompoundButton; 8 | import android.widget.TextView; 9 | 10 | import com.kyleduo.switchbutton.SwitchButton; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import androidx.appcompat.app.AppCompatActivity; 16 | import androidx.recyclerview.widget.LinearLayoutManager; 17 | import androidx.recyclerview.widget.RecyclerView; 18 | 19 | /** 20 | * Created by kyle on 16/1/8. 21 | */ 22 | public class RecyclerActivity extends AppCompatActivity { 23 | 24 | private RecyclerView mRecyclerView; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_recycler); 30 | 31 | mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 32 | SwitchRecyclerAdapter adapter = new SwitchRecyclerAdapter(); 33 | mRecyclerView.setAdapter(adapter); 34 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 35 | } 36 | 37 | private class SwitchViewHolder extends RecyclerView.ViewHolder { 38 | 39 | TextView tv; 40 | SwitchButton sb; 41 | 42 | public SwitchViewHolder(View itemView) { 43 | super(itemView); 44 | tv = (TextView) itemView.findViewById(R.id.recycler_item_tv); 45 | sb = (SwitchButton) itemView.findViewById(R.id.recycler_item_sb); 46 | } 47 | } 48 | 49 | private class SwitchRecyclerAdapter extends RecyclerView.Adapter { 50 | 51 | private List mSbStates; 52 | 53 | public SwitchRecyclerAdapter() { 54 | mSbStates = new ArrayList<>(getItemCount()); 55 | for (int i = 0; i < getItemCount(); i++) { 56 | mSbStates.add(false); 57 | } 58 | } 59 | 60 | @Override 61 | public SwitchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 62 | View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler_item, parent, false); 63 | return new SwitchViewHolder(v); 64 | } 65 | 66 | @Override 67 | public void onBindViewHolder(SwitchViewHolder holder, final int position) { 68 | holder.sb.setOnCheckedChangeListener(null); 69 | holder.sb.setCheckedImmediately(mSbStates.get(position)); 70 | holder.tv.setText("SwitchButton can be used in RecyclerView."); 71 | holder.sb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 72 | @Override 73 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 74 | mSbStates.set(position, isChecked); 75 | } 76 | }); 77 | } 78 | 79 | @Override 80 | public int getItemCount() { 81 | return 30; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/java/com/kyleduo/switchbutton/demo/StyleActivity.java: -------------------------------------------------------------------------------- 1 | package com.kyleduo.switchbutton.demo; 2 | 3 | import android.os.Bundle; 4 | import android.widget.CompoundButton; 5 | 6 | import com.kyleduo.switchbutton.SwitchButton; 7 | 8 | import androidx.appcompat.app.AppCompatActivity; 9 | 10 | public class StyleActivity extends AppCompatActivity { 11 | 12 | private SwitchButton mFlymeSb, mMIUISb, mCustomSb, mDefaultSb, mSB; 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_style); 18 | 19 | SwitchButton disableSb = (SwitchButton) findViewById(R.id.sb_disable_control); 20 | SwitchButton disableNoEventSb = (SwitchButton) findViewById(R.id.sb_disable_control_no_event); 21 | mFlymeSb = (SwitchButton) findViewById(R.id.sb_custom_flyme); 22 | mMIUISb = (SwitchButton) findViewById(R.id.sb_custom_miui); 23 | mCustomSb = (SwitchButton) findViewById(R.id.sb_custom); 24 | mDefaultSb = (SwitchButton) findViewById(R.id.sb_default); 25 | mSB = (SwitchButton) findViewById(R.id.sb_ios); 26 | 27 | if (disableSb != null) { 28 | disableSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 29 | @Override 30 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 31 | mFlymeSb.setEnabled(isChecked); 32 | mMIUISb.setEnabled(isChecked); 33 | mCustomSb.setEnabled(isChecked); 34 | mDefaultSb.setEnabled(isChecked); 35 | mSB.setEnabled(isChecked); 36 | } 37 | }); 38 | } 39 | if (disableNoEventSb != null) { 40 | disableNoEventSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 41 | @Override 42 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 43 | mFlymeSb.setEnabled(isChecked); 44 | mMIUISb.setEnabled(isChecked); 45 | mCustomSb.setEnabled(isChecked); 46 | mDefaultSb.setEnabled(isChecked); 47 | mSB.setEnabled(isChecked); 48 | } 49 | }); 50 | disableNoEventSb.setCheckedImmediatelyNoEvent(false); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/java/com/kyleduo/switchbutton/demo/StyleInCodeActivity.java: -------------------------------------------------------------------------------- 1 | package com.kyleduo.switchbutton.demo; 2 | 3 | import android.graphics.RectF; 4 | import android.os.Bundle; 5 | import android.text.Spannable; 6 | import android.text.SpannableString; 7 | import android.text.TextUtils; 8 | import android.text.style.ImageSpan; 9 | import android.view.View; 10 | import android.widget.AdapterView; 11 | import android.widget.ArrayAdapter; 12 | import android.widget.ListView; 13 | 14 | import com.kyleduo.switchbutton.SwitchButton; 15 | 16 | import androidx.appcompat.app.AppCompatActivity; 17 | 18 | public class StyleInCodeActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 19 | 20 | private SwitchButton mChangeSb; 21 | private boolean mThumbMarginFlag, mThumbSizeFlag, mThumbRadiusFlag, mBackRadiusFlag, mBackMeasureRatioFlag, mAnimationDurationFlag; 22 | private String[] opts = new String[]{ 23 | "setThumbColorRes/setThumbColor", 24 | "setThumbDrawableRes/setThumbDrawable", 25 | "setBackColorRes/setBackColor", 26 | "setBackDrawableRes/setBackDrawable", 27 | "setTintColor", 28 | "setThumbMargin", 29 | "setThumbSize", 30 | "setThumbRadius (color-mode only)", 31 | "setBackRadius (color-mode only)", 32 | "setFadeBack", 33 | "setThumbRangeRatio", 34 | "setAnimationDuration", 35 | "setText", 36 | "setDrawDebugRect", 37 | }; 38 | 39 | @Override 40 | protected void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_style_in_code); 43 | 44 | mChangeSb = (SwitchButton) findViewById(R.id.sb_code); 45 | ListView optLv = (ListView) findViewById(R.id.opt_lv); 46 | 47 | optLv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, opts)); 48 | optLv.setOnItemClickListener(this); 49 | } 50 | 51 | @Override 52 | public void onItemClick(AdapterView parent, View view, int position, long id) { 53 | switch (position) { 54 | case 0: 55 | mChangeSb.setThumbColorRes(R.color.custom_thumb_color); 56 | break; 57 | case 1: 58 | mChangeSb.setThumbDrawableRes(R.drawable.miui_thumb_drawable); 59 | break; 60 | case 2: 61 | mChangeSb.setBackColorRes(R.color.custom_back_color); 62 | break; 63 | case 3: 64 | mChangeSb.setBackDrawableRes(R.drawable.miui_back_drawable); 65 | break; 66 | case 4: 67 | mChangeSb.setTintColor(0x9F6C66); 68 | break; 69 | case 5: { 70 | float margin = 10 * getResources().getDisplayMetrics().density; 71 | float defaultMargin = SwitchButton.DEFAULT_THUMB_MARGIN_DP * getResources().getDisplayMetrics().density; 72 | mChangeSb.setThumbMargin(mThumbMarginFlag ? new RectF(defaultMargin, defaultMargin, defaultMargin, defaultMargin) : new RectF(margin, margin, margin, margin)); 73 | mThumbMarginFlag = !mThumbMarginFlag; 74 | } 75 | break; 76 | case 6: { 77 | int size = (int) (30 * getResources().getDisplayMetrics().density); 78 | mChangeSb.setThumbSize(size, size); 79 | mThumbSizeFlag = !mThumbSizeFlag; 80 | } 81 | break; 82 | case 7: { 83 | float r = 2 * getResources().getDisplayMetrics().density; 84 | mChangeSb.setThumbRadius(mThumbRadiusFlag ? Math.min(mChangeSb.getThumbWidth(), mChangeSb.getThumbHeight()) / 2f : r); 85 | mThumbRadiusFlag = !mThumbRadiusFlag; 86 | } 87 | break; 88 | case 8: { 89 | float r = 2 * getResources().getDisplayMetrics().density; 90 | mChangeSb.setBackRadius(mBackRadiusFlag ? Math.min(mChangeSb.getBackSizeF().x, mChangeSb.getBackSizeF().y) / 2f : r); 91 | mBackRadiusFlag = !mBackRadiusFlag; 92 | } 93 | break; 94 | case 9: 95 | mChangeSb.setFadeBack(!mChangeSb.isFadeBack()); 96 | break; 97 | case 10: 98 | mChangeSb.setThumbRangeRatio(mBackMeasureRatioFlag ? SwitchButton.DEFAULT_THUMB_RANGE_RATIO : 4f); 99 | mBackMeasureRatioFlag = !mBackMeasureRatioFlag; 100 | break; 101 | case 11: 102 | mChangeSb.setAnimationDuration(mAnimationDurationFlag ? SwitchButton.DEFAULT_ANIMATION_DURATION : 1000); 103 | mAnimationDurationFlag = !mAnimationDurationFlag; 104 | break; 105 | case 12: { 106 | CharSequence on = mChangeSb.getTextOn(); 107 | CharSequence off = mChangeSb.getTextOff(); 108 | if (TextUtils.isEmpty(on) || TextUtils.isEmpty(off)) { 109 | SpannableString ss = new SpannableString("[icon]"); 110 | ImageSpan span = new ImageSpan(this, R.drawable.icon_blog_small, ImageSpan.ALIGN_BOTTOM); 111 | ss.setSpan(span, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 112 | mChangeSb.setText(ss, "OFF"); 113 | mChangeSb.setTextExtra((int) (getResources().getDisplayMetrics().density * 4)); 114 | } else { 115 | mChangeSb.setText("", ""); 116 | } 117 | } 118 | break; 119 | case 13: 120 | mChangeSb.setDrawDebugRect(!mChangeSb.isDrawDebugRect()); 121 | break; 122 | default: 123 | break; 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/java/com/kyleduo/switchbutton/demo/UseActivity.java: -------------------------------------------------------------------------------- 1 | package com.kyleduo.switchbutton.demo; 2 | 3 | import android.animation.Animator; 4 | import android.animation.ObjectAnimator; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.CompoundButton; 9 | import android.widget.LinearLayout; 10 | import android.widget.ProgressBar; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import com.kyleduo.switchbutton.SwitchButton; 15 | 16 | import androidx.appcompat.app.AppCompatActivity; 17 | 18 | public class UseActivity extends AppCompatActivity implements View.OnClickListener { 19 | 20 | private SwitchButton mListenerSb, mListenerDistinguishSb, mLongSb, mToggleSb, mCheckedSb, mDelaySb, mForceOpenSb, mForceOpenControlSb; 21 | private ProgressBar mPb; 22 | private Button mStartBt; 23 | private TextView mListenerFinish; 24 | private TextView mTriggerTv; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_use); 30 | 31 | findView(); 32 | 33 | LinearLayout toggleWrapper = (LinearLayout) findViewById(R.id.toggle_wrapper); 34 | for (int i = 0; i < toggleWrapper.getChildCount(); i++) { 35 | toggleWrapper.getChildAt(i).setOnClickListener(this); 36 | } 37 | 38 | LinearLayout checkWrapper = (LinearLayout) findViewById(R.id.check_wrapper); 39 | for (int i = 0; i < checkWrapper.getChildCount(); i++) { 40 | checkWrapper.getChildAt(i).setOnClickListener(this); 41 | } 42 | 43 | 44 | // work with listener 45 | mListenerSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 46 | 47 | @Override 48 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 49 | mListenerFinish.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE); 50 | if (mListenerDistinguishSb.isChecked() != isChecked) { 51 | mListenerDistinguishSb.setChecked(isChecked); 52 | } 53 | } 54 | }); 55 | 56 | // listener with distinguish 57 | mListenerDistinguishSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 58 | @Override 59 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 60 | mTriggerTv.setVisibility(isChecked ? View.VISIBLE : View.GONE); 61 | if (isChecked) { 62 | mTriggerTv.setText(buttonView.isPressed() ? R.string.use_trigger_manually : R.string.use_trigger_by_code); 63 | } 64 | } 65 | }); 66 | 67 | // work with delay 68 | mDelaySb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 69 | @Override 70 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 71 | mDelaySb.setEnabled(false); 72 | mDelaySb.postDelayed(new Runnable() { 73 | @Override 74 | public void run() { 75 | mDelaySb.setEnabled(true); 76 | } 77 | }, 1500); 78 | } 79 | }); 80 | 81 | // work with stuff takes long 82 | mStartBt.setOnClickListener(new View.OnClickListener() { 83 | 84 | @Override 85 | public void onClick(View v) { 86 | ObjectAnimator animator = ObjectAnimator.ofInt(mPb, "progress", 0, 1000); 87 | animator.setDuration(1000); 88 | animator.addListener(new Animator.AnimatorListener() { 89 | @Override 90 | public void onAnimationStart(Animator animation) { 91 | mStartBt.setEnabled(false); 92 | mLongSb.setChecked(false); 93 | } 94 | 95 | @Override 96 | public void onAnimationEnd(Animator animation) { 97 | mStartBt.setEnabled(true); 98 | mLongSb.setChecked(true); 99 | } 100 | 101 | @Override 102 | public void onAnimationCancel(Animator animation) { 103 | mStartBt.setEnabled(true); 104 | } 105 | 106 | @Override 107 | public void onAnimationRepeat(Animator animation) { 108 | 109 | } 110 | }); 111 | animator.start(); 112 | } 113 | }); 114 | 115 | // check in check 116 | mForceOpenSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 117 | @Override 118 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 119 | if (mForceOpenControlSb.isChecked()) { 120 | toast("Call mForceOpenSb.setChecked(true); in on CheckedChanged"); 121 | mForceOpenSb.setChecked(true); 122 | } 123 | } 124 | }); 125 | } 126 | 127 | private void findView() { 128 | mListenerSb = (SwitchButton) findViewById(R.id.sb_use_listener); 129 | mListenerDistinguishSb = (SwitchButton) findViewById(R.id.sb_listener_distinguish); 130 | mLongSb = (SwitchButton) findViewById(R.id.sb_use_long); 131 | mToggleSb = (SwitchButton) findViewById(R.id.sb_use_toggle); 132 | mCheckedSb = (SwitchButton) findViewById(R.id.sb_use_checked); 133 | mDelaySb = (SwitchButton) findViewById(R.id.sb_use_delay); 134 | 135 | mPb = (ProgressBar) findViewById(R.id.pb); 136 | mPb.setProgress(0); 137 | mPb.setMax(1000); 138 | 139 | mStartBt = (Button) findViewById(R.id.long_start); 140 | 141 | mListenerFinish = (TextView) findViewById(R.id.listener_finish); 142 | mListenerFinish.setVisibility(mListenerSb.isChecked() ? View.VISIBLE : View.INVISIBLE); 143 | mTriggerTv = (TextView) findViewById(R.id.listener_trigger); 144 | 145 | mForceOpenSb = (SwitchButton) findViewById(R.id.use_focus_open); 146 | mForceOpenControlSb = (SwitchButton) findViewById(R.id.use_focus_open_control); 147 | 148 | mToggleSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 149 | @Override 150 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 151 | toast("Toggle SwitchButton new check state: " + (isChecked ? "Checked" : "Unchecked")); 152 | } 153 | }); 154 | 155 | mCheckedSb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 156 | @Override 157 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 158 | toast("Check SwitchButton new check state: " + (isChecked ? "Checked" : "Unchecked")); 159 | } 160 | }); 161 | } 162 | 163 | @Override 164 | public void onClick(View v) { 165 | int id = v.getId(); 166 | if (id == R.id.toggle_ani) { 167 | mToggleSb.toggle(); 168 | } else if (id == R.id.toggle_ani_no_event) { 169 | mToggleSb.toggleNoEvent(); 170 | } else if (id == R.id.toggle_not_ani) { 171 | mToggleSb.toggleImmediately(); 172 | } else if (id == R.id.toggle_not_ani_no_event) { 173 | mToggleSb.toggleImmediatelyNoEvent(); 174 | } else if (id == R.id.checked_ani) { 175 | mCheckedSb.setChecked(!mCheckedSb.isChecked()); 176 | } else if (id == R.id.checked_ani_no_event) { 177 | mCheckedSb.setCheckedNoEvent(!mCheckedSb.isChecked()); 178 | } else if (id == R.id.checked_not_ani) { 179 | mCheckedSb.setCheckedImmediately(!mCheckedSb.isChecked()); 180 | } else if (id == R.id.checked_not_ani_no_event) { 181 | mCheckedSb.setCheckedImmediatelyNoEvent(!mCheckedSb.isChecked()); 182 | } 183 | } 184 | 185 | private void toast(String text) { 186 | Toast.makeText(UseActivity.this, text, Toast.LENGTH_SHORT).show(); 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/color/custom_back_color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/color/custom_text_state_color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/color/custom_thumb_color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable-xhdpi/ios_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kyleduo/SwitchButton/60a42e46ea19b4dbc8d5d298377186e93202d393/SwitchButton/demo/src/main/res/drawable-xhdpi/ios_thumb.png -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable-xhdpi/ios_thumb_disable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kyleduo/SwitchButton/60a42e46ea19b4dbc8d5d298377186e93202d393/SwitchButton/demo/src/main/res/drawable-xhdpi/ios_thumb_disable.png -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable-xxhdpi/icon_blog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kyleduo/SwitchButton/60a42e46ea19b4dbc8d5d298377186e93202d393/SwitchButton/demo/src/main/res/drawable-xxhdpi/icon_blog.png -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable-xxhdpi/icon_blog_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kyleduo/SwitchButton/60a42e46ea19b4dbc8d5d298377186e93202d393/SwitchButton/demo/src/main/res/drawable-xxhdpi/icon_blog_small.png -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable-xxhdpi/icon_github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kyleduo/SwitchButton/60a42e46ea19b4dbc8d5d298377186e93202d393/SwitchButton/demo/src/main/res/drawable-xxhdpi/icon_github.png -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/flyme_back_drawable.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/flyme_thumb_drawable.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 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/ios_back_drawable.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/ios_off.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/ios_thumb_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/miui_back_drawable.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/drawable/miui_thumb_drawable.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 28 | 29 | 30 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/layout/activity_recycler.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/layout/activity_style.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 15 | 16 | 21 | 22 | 28 | 29 | 34 | 35 | 38 | 39 | 46 | 47 | 56 | 57 | 66 | 67 | 68 | 72 | 73 | 81 | 82 | 92 | 93 | 103 | 104 | 105 | 109 | 110 | 121 | 122 | 135 | 136 | 149 | 150 | 151 | 156 | 157 | 161 | 162 | 174 | 175 | 187 | 188 | 189 | 190 | 191 | 196 | 197 | 203 | 204 | 209 | 210 | 211 | 212 | 217 | 218 | 223 | 224 | 229 | 230 | 231 | 232 | 233 | 234 | 245 | 246 | 258 | 259 | 271 | 272 | 273 | 274 | 275 | 280 | 281 | 286 | 287 | 294 | 295 | 302 | 303 | 310 | 311 | 312 | 317 | 318 | 323 | 324 | 329 | 330 | 343 | 344 | 345 | 350 | 351 | 355 | 356 | 362 | 363 | 370 | 371 | 372 | 377 | 378 | 382 | 383 | 390 | 391 | 397 | 398 | 402 | 403 | 404 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/layout/activity_style_in_code.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 17 | 18 | 24 | 25 | 30 | 31 | -------------------------------------------------------------------------------- /SwitchButton/demo/src/main/res/layout/activity_use.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 16 | 17 | 22 | 23 | 28 | 29 | 34 | 35 | 42 | 43 | 44 | 50 | 51 | 56 | 57 | 63 | 64 | 69 | 70 | 75 | 76 | 85 | 86 | 87 | 93 | 94 | 99 | 100 | 106 | 107 | 113 | 114 | 119 | 120 | 124 | 125 | 130 | 131 | 137 | 138 | 139 | 140 | 146 | 147 | 152 | 153 | 159 | 160 |