├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ │ └── ic_launcher_background.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── it
│ │ └── emperor
│ │ └── animatedcheckbox
│ │ └── MainActivity.kt
├── proguard-rules.pro
└── build.gradle
├── animatedcheckbox
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ └── values
│ │ │ ├── strings.xml
│ │ │ └── attrs.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── it
│ │ └── emperor
│ │ └── animatedcheckbox
│ │ ├── binding
│ │ └── Binding.kt
│ │ ├── extension
│ │ └── NumberExt.kt
│ │ └── AnimatedCheckBox.kt
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── sample.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── encodings.xml
├── vcs.xml
├── runConfigurations.xml
├── gradle.xml
├── codeStyles
│ └── Project.xml
└── misc.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/animatedcheckbox/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':animatedcheckbox'
2 |
--------------------------------------------------------------------------------
/sample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/sample.gif
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AnimatedCheckBox
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/animatedcheckbox/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AnimatedCheckBox
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/animatedcheckbox/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moro56/AnimatedCheckBox/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches/build_file_checksums.ser
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | .DS_Store
9 | /build
10 | /captures
11 | .externalNativeBuild
12 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/animatedcheckbox/src/main/java/it/emperor/animatedcheckbox/binding/Binding.kt:
--------------------------------------------------------------------------------
1 | package it.emperor.animatedcheckbox.binding
2 |
3 | import androidx.databinding.BindingAdapter
4 | import it.emperor.animatedcheckbox.AnimatedCheckBox
5 |
6 | @BindingAdapter("checked")
7 | fun AnimatedCheckBox.setChecked(checked: Boolean) {
8 | this.setChecked(checked)
9 | }
10 |
11 | @BindingAdapter("onChange")
12 | fun AnimatedCheckBox.setListener(onChange: (checked: Boolean) -> Unit = {}) {
13 | this.setOnChangeListener(onChange)
14 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/animatedcheckbox/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | android.useAndroidX=true
15 | android.enableJetifier=true
16 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/animatedcheckbox/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/animatedcheckbox/src/main/java/it/emperor/animatedcheckbox/extension/NumberExt.kt:
--------------------------------------------------------------------------------
1 | package it.emperor.animatedcheckbox.extension
2 |
3 | import android.content.res.Resources
4 | import android.graphics.Color
5 |
6 | fun Float.toRange(oldMin: Float, oldMax: Float, newMin: Float, newMax: Float): Float =
7 | (((this - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin
8 |
9 | fun Float.clamp(min: Float, max: Float): Float {
10 | if (this < min) return min
11 | if (this > max) return max
12 | return this
13 | }
14 |
15 | fun FloatArray.animateColor(colorFrom: FloatArray, colorTo: FloatArray, animatedFraction: Float): Int {
16 | this[0] = colorFrom[0] + (colorTo[0] - colorFrom[0]) * animatedFraction;
17 | this[1] = colorFrom[1] + (colorTo[1] - colorFrom[1]) * animatedFraction;
18 | this[2] = colorFrom[2] + (colorTo[2] - colorFrom[2]) * animatedFraction;
19 | return Color.HSVToColor(this)
20 | }
21 |
22 | fun Float.toPx(): Float = this * Resources.getSystem().displayMetrics.density
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 |
5 | apply plugin: 'kotlin-android-extensions'
6 |
7 | android {
8 | compileSdkVersion 28
9 | defaultConfig {
10 | applicationId "it.emperor.animatedcheckbox"
11 | minSdkVersion 21
12 | targetSdkVersion 28
13 | versionCode 3
14 | versionName "1.0.3"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | dataBinding {
23 | enabled = true
24 | }
25 | }
26 |
27 | dependencies {
28 | def appcompat_version = "1.0.0"
29 | def constraintlayout_version = "2.0.0-alpha2"
30 | implementation fileTree(include: ['*.jar'], dir: 'libs')
31 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
32 | implementation "androidx.appcompat:appcompat:$appcompat_version"
33 | implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version"
34 | implementation project(':animatedcheckbox')
35 | }
36 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.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 |
--------------------------------------------------------------------------------
/animatedcheckbox/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'com.jfrog.bintray'
4 | apply plugin: 'com.github.dcendents.android-maven'
5 |
6 | ext {
7 | bintrayRepo = "AnimatedCheckBox"
8 | bintrayName = "it.emperor.animatedcheckbox"
9 |
10 | libraryName = "animatedcheckbox"
11 |
12 | publishedGroupId = "it.emperor.animatedcheckbox"
13 | artifact = "animatedcheckbox"
14 | libraryVersion = "1.0.3"
15 |
16 | libraryDescription = "Custom CheckBox with animation"
17 | siteUrl = "https://github.com/moro56/AnimatedCheckBox"
18 | gitUrl = "https://github.com/moro56/AnimatedCheckBox.git"
19 | developerId = "moro56"
20 | developerName = "Enrico Morotti"
21 | developerEmail = "emperorgames.info@gmail.com"
22 | licenseName = "The Apache Software License, Version 2.0"
23 | licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.txt"
24 | allLicenses = ["Apache-2.0"]
25 | }
26 |
27 | android {
28 | compileSdkVersion 28
29 |
30 | defaultConfig {
31 | minSdkVersion 21
32 | targetSdkVersion 28
33 | versionCode 2
34 | versionName "1.0.1"
35 | }
36 |
37 | buildTypes {
38 | release {
39 | minifyEnabled false
40 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
41 | }
42 | }
43 |
44 | dataBinding {
45 | enabled = true
46 | }
47 | }
48 |
49 | dependencies {
50 | implementation fileTree(dir: 'libs', include: ['*.jar'])
51 |
52 | implementation 'androidx.appcompat:appcompat:1.0.0'
53 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
54 | }
55 | repositories {
56 | mavenCentral()
57 | }
58 |
59 | if (project.rootProject.file("local.properties").exists()) {
60 | apply from: "https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle"
61 | apply from: "https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle"
62 | }
--------------------------------------------------------------------------------
/app/src/main/java/it/emperor/animatedcheckbox/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package it.emperor.animatedcheckbox
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import kotlinx.android.synthetic.main.activity_main.*
6 |
7 | class MainActivity : AppCompatActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 | setContentView(R.layout.activity_main)
12 |
13 | button.setOnClickListener {
14 | performClick()
15 | }
16 |
17 | radioGroup.setOnCheckedChangeListener { _, id ->
18 | when (id) {
19 | R.id.radioButton -> updateAnimationDuration(250L)
20 | R.id.radioButton2 -> updateAnimationDuration(500L)
21 | R.id.radioButton3 -> updateAnimationDuration(1000L)
22 | R.id.radioButton4 -> updateAnimationDuration(2000L)
23 | R.id.radioButton5 -> updateAnimationDuration(3000L)
24 | }
25 | }
26 |
27 | checkBox.setOnCheckedChangeListener { _, checked ->
28 | animatedcheckbox.ignoreAnimation = checked
29 | animatedcheckbox1.ignoreAnimation = checked
30 | animatedcheckbox2.ignoreAnimation = checked
31 | animatedcheckbox3.ignoreAnimation = checked
32 | animatedcheckbox11.ignoreAnimation = checked
33 | }
34 | }
35 |
36 | private fun updateAnimationDuration(duration: Long) {
37 | animatedcheckbox.duration = duration
38 | animatedcheckbox1.duration = duration
39 | animatedcheckbox2.duration = duration
40 | animatedcheckbox3.duration = duration
41 | animatedcheckbox11.duration = duration
42 | }
43 |
44 | private fun performClick() {
45 | animatedcheckbox.performClick()
46 | animatedcheckbox1.performClick()
47 | animatedcheckbox2.performClick()
48 | animatedcheckbox3.performClick()
49 | animatedcheckbox11.performClick()
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
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 Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AnimatedCheckBox
2 |
3 | Custom CheckBox with animation (Kotlin)
4 |
5 | ### Example
6 |
7 |
8 |
9 | ## Usage
10 | Add dependency into app build.gradle
11 | ```
12 | implementation 'it.emperor.animatedcheckbox:animatedcheckbox:1.0.3'
13 | ```
14 |
15 | If gradle failed to sync, try adding maven repository into project build.gradle
16 | ```
17 | allprojects {
18 | repositories {
19 | maven {
20 | url "https://dl.bintray.com/moro56/AnimatedCheckBox"
21 | }
22 | }
23 | }
24 | ```
25 |
26 | ## Sample
27 | See `app` module
28 |
29 | #### Add AnimatedCheckBox into layout
30 | ```xml
31 |
44 | ```
45 |
46 | #### Update state
47 | To update the state of the checkbox (checked or not checked):
48 | ```kotlin
49 | animatedCheckBox.setChecked(true, true) // animated
50 | animatedCheckBox.setChecked(false) // not animated
51 | ```
52 |
53 | #### Callback
54 | To listen for the check changed events use:
55 | ```kotlin
56 | animatedCheckBox.setOnChangeListener {
57 | println("Is checked: " + it) // true or false
58 | }
59 | ```
60 |
61 | #### Attributes
62 | Name | Type | Description
63 | --- | --- | ---
64 | `acb_animation_duration` | Integer | The duration of the animation
65 | `acb_border_checked_color` | Color | The color of the circle border (when checked)
66 | `acb_border_checked_stroke_width` | Dimension | The stroke width of the circle border (when checked)
67 | `acb_border_not_checked_color` | Color | The color of the circle border (when not checked)
68 | `acb_checked` | Boolean | The state of the checkbox
69 | `acb_circle_color` | Color | The color of the circle
70 | `acb_hook_color` | Color | The color of the hook
71 | `acb_hook_stroke_width` | Dimension | The stroke width of the circle (used for the circle border too (when checked))
72 | `acb_padding` | Dimension | The padding of the view
73 | `acb_ignore_animation` | Boolean | Don't animate the view when clicked
74 |
75 | ## License
76 | ```
77 | Copyright 2017 Enrico Morotti
78 |
79 | Licensed under the Apache License, Version 2.0 (the "License");
80 | you may not use this file except in compliance with the License.
81 | You may obtain a copy of the License at
82 |
83 | http://www.apache.org/licenses/LICENSE-2.0
84 |
85 | Unless required by applicable law or agreed to in writing, software
86 | distributed under the License is distributed on an "AS IS" BASIS,
87 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88 | See the License for the specific language governing permissions and
89 | limitations under the License.
90 | ```
91 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
21 |
22 |
33 |
34 |
47 |
48 |
63 |
64 |
75 |
76 |
87 |
88 |
98 |
99 |
106 |
107 |
113 |
114 |
120 |
121 |
127 |
128 |
134 |
135 |
136 |
146 |
147 |
157 |
--------------------------------------------------------------------------------
/animatedcheckbox/src/main/java/it/emperor/animatedcheckbox/AnimatedCheckBox.kt:
--------------------------------------------------------------------------------
1 | package it.emperor.animatedcheckbox
2 |
3 | import android.animation.Animator
4 | import android.animation.AnimatorListenerAdapter
5 | import android.animation.ValueAnimator
6 | import android.content.Context
7 | import android.graphics.*
8 | import android.util.AttributeSet
9 | import android.view.View
10 | import android.view.animation.AccelerateDecelerateInterpolator
11 | import it.emperor.animatedcheckbox.extension.animateColor
12 | import it.emperor.animatedcheckbox.extension.clamp
13 | import it.emperor.animatedcheckbox.extension.toPx
14 | import it.emperor.animatedcheckbox.extension.toRange
15 |
16 | // Default values
17 | private const val DEFAULT_CIRCLE_COLOR: Int = Color.GREEN
18 | private const val DEFAULT_HOOK_COLOR: Int = Color.BLACK
19 | private const val DEFAULT_BORDER_CHECKED_COLOR: Int = Color.BLACK
20 | private const val DEFAULT_HOOK_STROKE_WIDTH: Float = 1f
21 | private const val DEFAULT_BORDER_CHECKED_STROKE_WIDTH: Float = 0f
22 | private const val DEFAULT_ANIMATION_DURATION: Long = 250
23 |
24 | class AnimatedCheckBox @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
25 |
26 | private val animator: ValueAnimator
27 | private val paint: Paint
28 | private val path: Path
29 |
30 | var circleColor: Int = DEFAULT_CIRCLE_COLOR
31 | set(value) {
32 | field = value
33 | invalidate()
34 | }
35 | var hookColor: Int = DEFAULT_HOOK_COLOR
36 | set(value) {
37 | field = value
38 | invalidate()
39 | }
40 | var borderCheckedColor: Int = DEFAULT_BORDER_CHECKED_COLOR
41 | set(value) {
42 | field = value
43 | invalidate()
44 | }
45 | var borderNotCheckedColor: Int = hookColor
46 | set(value) {
47 | field = value
48 | invalidate()
49 | }
50 | var hookStrokeWidth: Float = DEFAULT_HOOK_STROKE_WIDTH
51 | set(value) {
52 | field = value
53 | invalidate()
54 | }
55 | var borderCheckedStrokeWidth: Float = DEFAULT_BORDER_CHECKED_STROKE_WIDTH
56 | set(value) {
57 | field = value
58 | invalidate()
59 | }
60 | var duration: Long = DEFAULT_ANIMATION_DURATION
61 | set(value) {
62 | field = value
63 | invalidate()
64 | }
65 | var padding: Float = 2f.toPx()
66 | set(value) {
67 | field = value
68 | invalidate()
69 | }
70 | var ignoreAnimation: Boolean = false
71 |
72 | private var checked: Boolean = false
73 | private var onChange: (checked: Boolean) -> Unit = {}
74 |
75 | // Internal fields
76 | private var animationProgress: Float
77 | private val colorFrom = FloatArray(3)
78 | private val colorTo = FloatArray(3)
79 | private val colorAnimationHsv = FloatArray(3)
80 | private var colorAnimation: Int = borderNotCheckedColor
81 | private var leftHookCircleX = 0.0
82 | private var leftHookCircleY = 0.0
83 | private var hookCenterX = 0.0
84 | private var hookCenterY = 0.0
85 |
86 | init {
87 | attrs?.let {
88 | val array = context.obtainStyledAttributes(attrs, R.styleable.AnimatedCheckBox)
89 | circleColor = array.getColor(R.styleable.AnimatedCheckBox_acb_circle_color, DEFAULT_CIRCLE_COLOR)
90 | hookColor = array.getColor(R.styleable.AnimatedCheckBox_acb_hook_color, DEFAULT_HOOK_COLOR)
91 | borderCheckedColor = array.getColor(R.styleable.AnimatedCheckBox_acb_border_checked_color, DEFAULT_BORDER_CHECKED_COLOR)
92 | borderNotCheckedColor = array.getColor(R.styleable.AnimatedCheckBox_acb_border_not_checked_color, hookColor)
93 | hookStrokeWidth = array.getDimension(R.styleable.AnimatedCheckBox_acb_hook_stroke_width, DEFAULT_HOOK_STROKE_WIDTH)
94 | borderCheckedStrokeWidth = array.getDimension(R.styleable.AnimatedCheckBox_acb_border_checked_stroke_width, DEFAULT_BORDER_CHECKED_STROKE_WIDTH)
95 | duration = array.getInteger(R.styleable.AnimatedCheckBox_acb_animation_duration, DEFAULT_ANIMATION_DURATION.toInt()).toLong()
96 | checked = array.getBoolean(R.styleable.AnimatedCheckBox_acb_checked, checked)
97 | colorAnimation = if (checked) hookColor else borderNotCheckedColor
98 | padding = array.getDimension(R.styleable.AnimatedCheckBox_acb_padding, padding)
99 | ignoreAnimation = array.getBoolean(R.styleable.AnimatedCheckBox_acb_ignore_animation, ignoreAnimation)
100 | array.recycle()
101 | }
102 | animationProgress = if (checked) 1f else 0f
103 |
104 | if (useAnimatedColor()) {
105 | Color.colorToHSV(hookColor, colorFrom)
106 | Color.colorToHSV(borderNotCheckedColor, colorTo)
107 | }
108 |
109 | // Initialize paint object
110 | paint = Paint()
111 | paint.isAntiAlias = true
112 | paint.isDither = true
113 | paint.strokeJoin = Paint.Join.ROUND
114 | paint.strokeCap = Paint.Cap.ROUND
115 | paint.pathEffect = CornerPathEffect(10f)
116 |
117 | // Initialize path object
118 | path = Path()
119 |
120 | // Initialize animator object
121 | animator = ValueAnimator()
122 | animator.interpolator = AccelerateDecelerateInterpolator()
123 | animator.addUpdateListener {
124 | update(it)
125 | }
126 |
127 | // Initialize click listener
128 | setOnClickListener {
129 | setChecked(!checked, !ignoreAnimation)
130 | }
131 | }
132 |
133 | /**
134 | * Register a callback to be invoked when the state changes.
135 | *
136 | * @param onChange callback to be invoked
137 | */
138 | fun setOnChangeListener(onChange: (checked: Boolean) -> Unit = {}) {
139 | this.onChange = onChange
140 | }
141 |
142 | /**
143 | * Update the state of this view.
144 | *
145 | * @param checked the new state
146 | * @param animate animating the update of the state
147 | */
148 | @JvmOverloads
149 | fun setChecked(checked: Boolean, animate: Boolean = false) {
150 | this.checked = checked
151 | animator.cancel()
152 | animator.removeAllListeners()
153 |
154 | if (animate) {
155 | startAnimation()
156 | } else {
157 | animationProgress = if (checked) 1f else 0f
158 | updateColorAnimation(1f)
159 | invalidate()
160 | }
161 | }
162 |
163 | /**
164 | * The view is checked
165 | *
166 | * @return is checked
167 | */
168 | fun isChecked() = checked
169 |
170 | /**
171 | * The animation is currently running
172 | *
173 | * @return animation is running
174 | */
175 | fun isAnimating() = animator.isRunning
176 |
177 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
178 | super.onLayout(changed, left, top, right, bottom)
179 |
180 | // Joint between the hook and the circle border
181 | leftHookCircleX = radius() * Math.cos(Math.toRadians(160.0))
182 | leftHookCircleY = radius() * Math.sin(Math.toRadians(160.0)) * -1
183 |
184 | // Joint between the two sides of the hook
185 | hookCenterX = leftHookCircleX + radius() * 0.9f * Math.cos(Math.toRadians(-20.0))
186 | hookCenterY = leftHookCircleY + radius() * Math.sin(Math.toRadians(-20.0)) * -1 + hookOffsetY()
187 | }
188 |
189 | override fun onDraw(canvas: Canvas) {
190 | super.onDraw(canvas)
191 | // Draw circle
192 | drawCircle(canvas)
193 | // Draw borders and hook
194 | drawCircleBorderAndHook(canvas)
195 | }
196 |
197 | /**
198 | * Draw circle
199 | *
200 | * @param canvas Canvas
201 | */
202 | private fun drawCircle(canvas: Canvas) {
203 | paint.style = Paint.Style.FILL
204 | paint.color = circleColor
205 | paint.alpha = (animationProgress * 255).toInt()
206 | canvas.drawCircle(centerX(), centerY(), radius(), paint)
207 | }
208 |
209 | /**
210 | * Draw borders and hook
211 | *
212 | * @param canvas Canvas
213 | */
214 | private fun drawCircleBorderAndHook(canvas: Canvas) {
215 | paint.style = Paint.Style.STROKE
216 | paint.alpha = 255
217 |
218 | path.reset()
219 |
220 | // Draw borders
221 | drawCircleBorder(canvas)
222 | // Draw hook
223 | drawHook()
224 |
225 | canvas.drawPath(path, paint)
226 | }
227 |
228 | /**
229 | * Draw borders
230 | *
231 | * @param canvas Canvas
232 | */
233 | private fun drawCircleBorder(canvas: Canvas) {
234 | // Draw circle border for the unchecked state
235 | if (animationProgress < 0.7f) {
236 | val arcProgress = (1f - animationProgress).toRange(0.3f, 1f, 0f, 1f).clamp(0f, 1f)
237 | path.arcTo(centerX() - radius(), centerY() - radius(), centerX() + radius(), centerY() + radius(),
238 | 200 + 360f * arcProgress, -360f * arcProgress, false)
239 | }
240 |
241 | // Draw circle border for the checked state, if specified
242 | if (borderCheckedStrokeWidth > 0 && animationProgress > 0.7f) {
243 | paint.strokeWidth = borderCheckedStrokeWidth
244 | paint.color = borderCheckedColor
245 |
246 | val arcProgressBorder = (1f - animationProgress).toRange(0f, 0.3f, 1f, 0f).clamp(0f, 1f)
247 | canvas.drawArc(centerX() - radiusBorderChecked(), centerY() - radiusBorderChecked(), centerX() + radiusBorderChecked(), centerY() + radiusBorderChecked(),
248 | 200f, -360f * arcProgressBorder, false, paint)
249 | }
250 | }
251 |
252 | /**
253 | * Draw hook
254 | *
255 | * @param canvas Canvas
256 | */
257 | private fun drawHook() {
258 | paint.strokeWidth = hookStrokeWidth
259 | if (useAnimatedColor()) {
260 | paint.color = colorAnimation
261 | } else {
262 | paint.color = hookColor
263 | }
264 | paint.alpha = 255
265 |
266 | // Draw left side hook
267 | drawLeftHook()
268 | // Draw right side hook
269 | drawRightHook()
270 | }
271 |
272 | /**
273 | * Draw left side hook
274 | */
275 | private fun drawLeftHook() {
276 | if (animationProgress < 0.7f) {
277 | val progress = animationProgress.toRange(0f, 0.7f, 0f, 1f)
278 | val lineRadius = radius() * progress
279 |
280 | val endX: Float = (leftHookCircleX + lineRadius * 0.9f * Math.cos(Math.toRadians(-20.0))).toFloat()
281 | val endY: Float = (leftHookCircleY + lineRadius * Math.sin(Math.toRadians(-20.0)) * -1 + (hookOffsetY() * progress)).toFloat()
282 |
283 | // LeftHook: left half-segment
284 | path.lineTo(centerX() + endX, centerY() + endY)
285 | } else {
286 | val progress = animationProgress.toRange(0.7f, 1f, 0f, 0.5f)
287 | val lineRadius = radius() * (1 - progress)
288 |
289 | val endX: Float = (lineRadius * Math.cos(Math.toRadians(160.0))).toFloat()
290 | val endY: Float = (lineRadius * Math.sin(Math.toRadians(160.0)) * -1 + (hookOffsetY() * progress)).toFloat()
291 |
292 | // LeftHook: right half-segment
293 | path.moveTo((centerX() + hookCenterX).toFloat(), (centerY() + hookCenterY).toFloat())
294 | path.lineTo(centerX() + endX, centerY() + endY)
295 | }
296 | }
297 |
298 | /**
299 | * Draw right side hook
300 | */
301 | private fun drawRightHook() {
302 | if (animationProgress >= 0.7f) {
303 | val progress = animationProgress.toRange(0.7f, 1f, 0f, 0.75f)
304 | val lineRadius = radius() * progress
305 |
306 | val endX: Float = (lineRadius * Math.cos(Math.toRadians(45.0))).toFloat()
307 | val endY: Float = (lineRadius * Math.sin(Math.toRadians(45.0)) * -1 + hookOffsetY() * animationProgress).toFloat()
308 |
309 | // RightHook
310 | path.moveTo((centerX() + hookCenterX).toFloat(), (centerY() + hookCenterY).toFloat())
311 | path.lineTo(centerX() + endX, centerY() + endY)
312 | }
313 | }
314 |
315 | /**
316 | * Start the animation
317 | */
318 | private fun startAnimation() {
319 | val endValue = if (checked) 1f else 0f
320 | val fraction = Math.abs(endValue - animationProgress)
321 | animator.setFloatValues(animationProgress, endValue)
322 | animator.duration = (duration * fraction).toLong()
323 | animator.addListener(object : AnimatorListenerAdapter() {
324 | override fun onAnimationCancel(animation: Animator?) {
325 | animation?.removeAllListeners()
326 | }
327 |
328 | override fun onAnimationEnd(animation: Animator?) {
329 | onChange(checked)
330 | }
331 | })
332 | animator.start()
333 | }
334 |
335 | /**
336 | * Update the state of this view during the animation
337 | *
338 | * @param animation the current animation
339 | */
340 | private fun update(animation: ValueAnimator) {
341 | val fraction = animation.animatedValue as Float
342 | animationProgress = fraction
343 | if (useAnimatedColor()) {
344 | updateColorAnimation(animation.animatedFraction)
345 | }
346 | invalidate()
347 | }
348 |
349 | /**
350 | * Update the color animation value
351 | *
352 | * @param animatedFraction Progress of the animation
353 | */
354 | private fun updateColorAnimation(animatedFraction: Float) {
355 | if (!checked) colorAnimation = colorAnimationHsv.animateColor(colorFrom, colorTo, animatedFraction)
356 | else colorAnimation = colorAnimationHsv.animateColor(colorTo, colorFrom, animatedFraction)
357 | }
358 |
359 | private fun padding() = padding * 2
360 | private fun width() = width - padding()
361 | private fun height() = height - padding()
362 | private fun centerX() = (width() + padding()) / 2f
363 | private fun centerY() = (height() + padding()) / 2f
364 | private fun hookOffsetY() = height() / 8f
365 | private fun radius() = (if (width() > height()) height() else width()) / 2f
366 | private fun radiusBorderChecked() = (if (width() > height()) height() else width()) / 2f
367 | private fun useAnimatedColor() = hookColor != borderNotCheckedColor
368 | }
--------------------------------------------------------------------------------