├── .editorconfig ├── .github └── workflows │ └── android.yml ├── .gitignore ├── LICENSE ├── README.md ├── art ├── anim1.gif └── anim2.gif ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── lcdsmao │ │ └── springx │ │ └── InstrumentationUiTestScope.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── lcdsmao │ │ │ └── springx │ │ │ ├── SpringAnimationConfig.kt │ │ │ ├── SpringAnimationHolder.kt │ │ │ ├── ViewPropertySpringAnimator.kt │ │ │ └── ViewPropertySpringAnimatorExt.kt │ └── res │ │ ├── layout │ │ └── activity_animation.xml │ │ └── values │ │ ├── ids.xml │ │ └── strings.xml │ ├── test │ └── java │ │ └── com │ │ └── github │ │ └── lcdsmao │ │ └── springx │ │ └── UnitUiTestScope.kt │ └── uiTest │ ├── AndroidManifest.xml │ └── java │ └── com │ └── github │ └── lcdsmao │ └── springx │ ├── AnimationActivity.kt │ ├── UiTestScope.kt │ └── ViewPropertySpringAnimatorTest.kt ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── lcdsmao │ │ └── springsample │ │ ├── BindingAdapters.kt │ │ ├── DragExampleFragment.kt │ │ ├── DragSampleViewModel.kt │ │ ├── MainActivity.kt │ │ ├── RecyclerViewExampleFragment.kt │ │ ├── SimpleExampleFragment.kt │ │ └── SpringMoveItemAnimator.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ ├── ic_android.xml │ ├── ic_launcher_background.xml │ └── ic_shuffle.xml │ ├── layout │ ├── activity_main.xml │ ├── fragment_drag_example.xml │ ├── fragment_recycler_view_example.xml │ ├── fragment_simple_example.xml │ └── item_view_color_block.xml │ ├── menu │ └── nav_menu.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.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 │ ├── navigation │ └── nav_graph.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── ids.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | indent_style = space 3 | end_of_line = lf 4 | charset = utf-8 5 | trim_trailing_whitespace = true 6 | insert_final_newline = true 7 | 8 | [*.{java,scala,rs,xml,gradle}] 9 | indent_size = 4 10 | 11 | [*.{kt,kts}] 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/android.yml: -------------------------------------------------------------------------------- 1 | name: Android CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: [ubuntu-latest] 14 | 15 | steps: 16 | - uses: actions/checkout@v1 17 | - name: Set up JDK 1.8 18 | uses: actions/setup-java@v1 19 | with: 20 | java-version: 1.8 21 | - name: Assemble debug 22 | run: ./gradlew assembleDebug 23 | 24 | unit_test: 25 | needs: [build] 26 | runs-on: [ubuntu-latest] 27 | 28 | steps: 29 | - uses: actions/checkout@v1 30 | - name: Set up JDK 1.8 31 | uses: actions/setup-java@v1 32 | with: 33 | java-version: 1.8 34 | - name: Run unit test 35 | run: ./gradlew :library:testDebugUnitTest 36 | 37 | android_test: 38 | needs: [build] 39 | runs-on: macOS-latest 40 | strategy: 41 | matrix: 42 | # Waiting mockk fix of verify(timeout = ...) with API < 24 43 | api-level: [24, 28] 44 | target: [default] 45 | 46 | steps: 47 | - uses: actions/checkout@v1 48 | - name: Run instrumented test 49 | uses: reactivecircus/android-emulator-runner@v1 50 | with: 51 | api-level: ${{ matrix.api-level }} 52 | target: ${{ matrix.target }} 53 | arch: x86_64 54 | profile: Nexus 6 55 | headless: true 56 | disable-animations: false 57 | script: ./gradlew :library:connectedCheck 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 Mao Yufeng 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpringX 2 | 3 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 4 | [ ![Download](https://api.bintray.com/packages/lcdsmao/maven/springx/images/download.svg) ](https://bintray.com/lcdsmao/maven/springx/_latestVersion) 5 | ![CI](https://github.com/lcdsmao/SpringX/workflows/Android%20CI/badge.svg) 6 | 7 | SpringX is an Android library that allows developers to easily use [SpringAnimation](https://developer.android.com/guide/topics/graphics/spring-animation). 8 | 9 | ## Demo 10 | 11 | | [Demo1](./sample/src/main/java/com/github/lcdsmao/springsample/DragExampleFragment.kt) | [Demo2](./sample/src/main/java/com/github/lcdsmao/springsample/SpringMoveItemAnimator.kt) | 12 | |-|-| 13 | | | | 14 | 15 | 16 | ## Setup 17 | 18 | Include the jcenter repository to your root `build.gradle` file: 19 | ```gralde 20 | repositories { 21 | jcenter() 22 | } 23 | ``` 24 | 25 | And then add dependency to your module `build.gradle`: 26 | ```gradle 27 | dependencies { 28 | implementation 'com.github.lcdsmao:springx:0.0.2' 29 | } 30 | ``` 31 | 32 | ## Usage 33 | 34 | You can manually create the `ViewPropertySpringAnimator` or use the extension function: 35 | 36 | ```kotlin 37 | val spring = ViewPropertySpringAnimator(view) // Always create a new one 38 | // or 39 | val spring = view.spring() // Reuse the one that associated with this view 40 | ``` 41 | 42 | The syntax of `ViewPropertySpringAnimator` is simliar to `ViewPropertyAnimator`: 43 | 44 | ```kotlin 45 | spring 46 | .rotation(30f) 47 | .scaleX(2f) 48 | .scaleY(2f) 49 | .y(500f) 50 | .x(500f) 51 | .start() 52 | ``` 53 | 54 | Complicated controls are also allowed: 55 | 56 | ```kotlin 57 | spring 58 | .defaultDampingRatio(0.7f) // default damping ratio for all animations 59 | .defaultStiffness(5f) // default stiffness for all animations 60 | .rotation(30f) 61 | .translationX(100f) { 62 | startVelocity = 30f // set start velocity of TRANSLATION_X 63 | dampingRatio = 0.3f // override the damping ratio 64 | onUpdate { spring, value, velocity -> 65 | // the update listener that associated with TRANSLATION_X 66 | } 67 | } 68 | .translationY(200f) { 69 | onUpdate { spring, value, velocity -> 70 | // the update listener that associated with TRANSLATION_Y 71 | } 72 | onEnd { spring, canceled, value, velocity -> 73 | // the end listener that associated with TRANSLATION_Y 74 | } 75 | } 76 | .setListener( 77 | onEnd = { 78 | // the end listener will be called when all animations end 79 | } 80 | ) 81 | ``` 82 | 83 | ## Thanks 84 | 85 | This library is inspired by the talk: [Motional Intelligence: Build Smarter Animations (Google I/O'19) 86 | ](https://youtu.be/f3Lm8iOr4mE?t=709) 87 | 88 | ## Related Articles 89 | 90 | - [Motional Intelligence: Build smarter animations](https://medium.com/androiddevelopers/motional-intelligence-build-smarter-animations-821af4d5f8c0) 91 | 92 | ## Contributing 93 | 94 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 95 | 96 | ## License 97 | 98 | ``` 99 | Copyright 2019 Mao Yufeng 100 | 101 | Licensed under the Apache License, Version 2.0 (the "License"); 102 | you may not use this file except in compliance with the License. 103 | You may obtain a copy of the License at 104 | 105 | http://www.apache.org/licenses/LICENSE-2.0 106 | 107 | Unless required by applicable law or agreed to in writing, software 108 | distributed under the License is distributed on an "AS IS" BASIS, 109 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 110 | See the License for the specific language governing permissions and 111 | limitations under the License. 112 | ``` 113 | -------------------------------------------------------------------------------- /art/anim1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcdsmao/SpringX/2b8d9fee70981142967680cb038e04c2fca163a6/art/anim1.gif -------------------------------------------------------------------------------- /art/anim2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcdsmao/SpringX/2b8d9fee70981142967680cb038e04c2fca163a6/art/anim2.gif -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext { 5 | version_code = 2 6 | version_name = '0.0.2' 7 | compile_sdk_version = 28 8 | min_sdk_version = 16 9 | target_sdk_version = 28 10 | 11 | kotlin_version = '1.3.61' 12 | appcompat_version = '1.1.0' 13 | corektx_version = '1.1.0' 14 | constraintlayout_version = '2.0.0-beta3' 15 | dynamicanimation_version = '1.0.0' 16 | navigation_version = '2.1.0' 17 | espresso_version = '3.2.0' 18 | robolectric_version = '4.3.1' 19 | androidxtest_version = '1.2.0' 20 | androidxtest_junit_version = '1.1.1' 21 | mockk_version = '1.9.3' 22 | } 23 | repositories { 24 | google() 25 | jcenter() 26 | } 27 | dependencies { 28 | classpath 'com.android.tools.build:gradle:3.5.2' 29 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 30 | classpath 'com.novoda:bintray-release:0.9.2' 31 | // NOTE: Do not place your application dependencies here; they belong 32 | // in the individual module build.gradle files 33 | } 34 | } 35 | 36 | allprojects { 37 | repositories { 38 | google() 39 | jcenter() 40 | } 41 | configurations.all { 42 | resolutionStrategy { 43 | force("org.objenesis:objenesis:2.6") 44 | } 45 | } 46 | } 47 | 48 | task clean(type: Delete) { 49 | delete rootProject.buildDir 50 | } 51 | -------------------------------------------------------------------------------- /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 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcdsmao/SpringX/2b8d9fee70981142967680cb038e04c2fca163a6/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Nov 10 11:34:49 JST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'com.novoda.bintray-release' 4 | 5 | android { 6 | compileSdkVersion compile_sdk_version 7 | 8 | defaultConfig { 9 | minSdkVersion min_sdk_version 10 | targetSdkVersion target_sdk_version 11 | versionCode version_code 12 | versionName version_name 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | 16 | archivesBaseName = "$archivesBaseName-$versionName" 17 | 18 | multiDexEnabled true 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | 28 | testOptions { 29 | unitTests { 30 | includeAndroidResources true 31 | } 32 | } 33 | 34 | sourceSets { 35 | androidTest { 36 | manifest.srcFile 'src/uiTest/AndroidManifest.xml' 37 | java.srcDirs += "src/uiTest/java" 38 | } 39 | test { 40 | manifest.srcFile 'src/uiTest/AndroidManifest.xml' 41 | java.srcDirs += "src/uiTest/java" 42 | } 43 | } 44 | } 45 | 46 | dependencies { 47 | implementation fileTree(dir: 'libs', include: ['*.jar']) 48 | 49 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 50 | implementation "androidx.appcompat:appcompat:$appcompat_version" 51 | implementation "androidx.core:core-ktx:$corektx_version" 52 | api "androidx.dynamicanimation:dynamicanimation:$dynamicanimation_version" 53 | 54 | implementation "androidx.multidex:multidex:2.0.1" 55 | 56 | testImplementation "androidx.test.ext:truth:$androidxtest_version" 57 | testImplementation "androidx.test.ext:junit:$androidxtest_junit_version" 58 | testImplementation "org.robolectric:robolectric:$robolectric_version" 59 | testImplementation "io.mockk:mockk:$mockk_version" 60 | 61 | androidTestImplementation "androidx.test.ext:truth:$androidxtest_version" 62 | androidTestImplementation "androidx.test.ext:junit:$androidxtest_junit_version" 63 | androidTestImplementation "androidx.test:runner:$androidxtest_version" 64 | androidTestImplementation "org.robolectric:annotations:$robolectric_version" 65 | androidTestImplementation "io.mockk:mockk-android:$mockk_version" 66 | } 67 | 68 | repositories { 69 | mavenCentral() 70 | } 71 | 72 | publish { 73 | bintrayUser = project.hasProperty('bintrayUser') ? bintrayUser : '' 74 | bintrayKey = project.hasProperty('bintrayKey') ? bintrayKey : '' 75 | userOrg = 'lcdsmao' 76 | groupId = 'com.github.lcdsmao' 77 | artifactId = 'springx' 78 | publishVersion = version_name 79 | desc = 'Easier to use SpringAnimation' 80 | website = 'https://github.com/lcdsmao/SpringX' 81 | } 82 | -------------------------------------------------------------------------------- /library/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 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/github/lcdsmao/springx/InstrumentationUiTestScope.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | 5 | @Suppress("unused") 6 | class InstrumentationUiTestScope : UiTestScope { 7 | 8 | override fun runOnMainSync(block: () -> Unit) { 9 | InstrumentationRegistry.getInstrumentation().runOnMainSync { 10 | block.invoke() 11 | } 12 | } 13 | 14 | override fun waitForIdleSync() { 15 | InstrumentationRegistry.getInstrumentation().waitForIdleSync() 16 | } 17 | 18 | class Runner : UiTestScope.Runner { 19 | 20 | override fun runUiTest(block: () -> Unit) { 21 | block.invoke() 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /library/src/main/java/com/github/lcdsmao/springx/SpringAnimationConfig.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import androidx.dynamicanimation.animation.DynamicAnimation 4 | import androidx.dynamicanimation.animation.SpringAnimation 5 | import androidx.dynamicanimation.animation.SpringForce 6 | 7 | class SpringAnimationConfig internal constructor(private var finalValue: Float) { 8 | 9 | companion object { 10 | val NOT_SET = Float.MAX_VALUE 11 | } 12 | 13 | private var startValueIsSet: Boolean = false 14 | var startValue: Float = NOT_SET 15 | set(value) { 16 | startValueIsSet = true 17 | field = value 18 | } 19 | 20 | private var startVelocityIsSet: Boolean = false 21 | var startVelocity: Float = NOT_SET 22 | set(value) { 23 | startVelocityIsSet = true 24 | field = value 25 | } 26 | 27 | var maxValue: Float = Float.MAX_VALUE 28 | var minValue: Float = -maxValue 29 | 30 | private var dampingRatioIsSet: Boolean = false 31 | internal var defaultDampingRatio: Float = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY 32 | var dampingRatio: Float = defaultDampingRatio 33 | get() { 34 | return if (dampingRatioIsSet) { 35 | field 36 | } else { 37 | defaultDampingRatio 38 | } 39 | } 40 | set(value) { 41 | dampingRatioIsSet = true 42 | field = value 43 | } 44 | 45 | private var stiffnessIsSet: Boolean = false 46 | internal var defaultStiffness: Float = SpringForce.STIFFNESS_MEDIUM 47 | var stiffness: Float = defaultStiffness 48 | get() { 49 | return if (stiffnessIsSet) { 50 | field 51 | } else { 52 | defaultStiffness 53 | } 54 | } 55 | set(value) { 56 | stiffnessIsSet = true 57 | field = value 58 | } 59 | 60 | private var updateListener: DynamicAnimation.OnAnimationUpdateListener? = null 61 | private var endListener: DynamicAnimation.OnAnimationEndListener? = null 62 | 63 | var skipToEndIfRunning: Boolean = false 64 | var cancelIfRunning: Boolean = false 65 | 66 | fun onUpdate( 67 | function: (spring: SpringAnimation, value: Float, velocity: Float) -> Unit 68 | ) { 69 | updateListener = DynamicAnimation.OnAnimationUpdateListener { animation, value, velocity -> 70 | val spring = animation as SpringAnimation 71 | function(spring, value, velocity) 72 | } 73 | } 74 | 75 | fun onEnd( 76 | function: (spring: SpringAnimation, canceled: Boolean, value: Float, velocity: Float) -> Unit 77 | ) { 78 | endListener = DynamicAnimation.OnAnimationEndListener { animation, canceled, value, velocity -> 79 | val spring = animation as SpringAnimation 80 | function(spring, canceled, value, velocity) 81 | } 82 | } 83 | 84 | internal fun applyTo(animation: SpringAnimation) { 85 | when { 86 | skipToEndIfRunning -> animation.skipToEnd() 87 | cancelIfRunning -> animation.cancel() 88 | } 89 | animation.setMinValue(minValue) 90 | animation.setMaxValue(maxValue) 91 | if (startValueIsSet) { 92 | animation.setStartValue(startValue) 93 | } 94 | if (startVelocityIsSet) { 95 | animation.setStartVelocity(startVelocity) 96 | } 97 | if (animation.spring == null) { 98 | animation.spring = SpringForce() 99 | } 100 | animation.spring.dampingRatio = dampingRatio 101 | animation.spring.stiffness = stiffness 102 | animation.spring.finalPosition = finalValue 103 | 104 | if (!animation.isRunning && updateListener != null) { 105 | animation.addUpdateListener(updateListener) 106 | } 107 | 108 | if (!animation.isRunning && endListener != null) { 109 | animation.addEndListener(endListener) 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /library/src/main/java/com/github/lcdsmao/springx/SpringAnimationHolder.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import androidx.dynamicanimation.animation.SpringAnimation 4 | 5 | internal data class SpringAnimationHolder( 6 | private val animation: SpringAnimation, 7 | private val config: SpringAnimationConfig 8 | ) { 9 | 10 | fun start() { 11 | config.applyTo(animation) 12 | animation.start() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /library/src/main/java/com/github/lcdsmao/springx/ViewPropertySpringAnimator.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import android.view.View 4 | import androidx.annotation.MainThread 5 | import androidx.dynamicanimation.animation.DynamicAnimation 6 | import androidx.dynamicanimation.animation.FloatPropertyCompat 7 | import androidx.dynamicanimation.animation.SpringAnimation 8 | import androidx.dynamicanimation.animation.SpringForce 9 | 10 | class ViewPropertySpringAnimator( 11 | private val view: T 12 | ) { 13 | 14 | interface AnimatorListener { 15 | fun onAnimationStart(animator: ViewPropertySpringAnimator) {} 16 | fun onAnimationCancel(animator: ViewPropertySpringAnimator) {} 17 | fun onAnimationEnd(animator: ViewPropertySpringAnimator) {} 18 | } 19 | 20 | private val pendingAnimations = mutableListOf() 21 | 22 | // Contains pending animations and running animations 23 | private val animatorMap = mutableMapOf, SpringAnimation>() 24 | val isRunning: Boolean 25 | get() = animatorMap.values.any { it.isRunning } 26 | 27 | private var defaultDampingRatio: Float = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY 28 | private var defaultStiffness: Float = SpringForce.STIFFNESS_MEDIUM 29 | private var animatorListener: AnimatorListener? = null 30 | 31 | fun defaultDampingRatio(value: Float) = apply { 32 | defaultDampingRatio = value 33 | } 34 | 35 | fun defaultStiffness(value: Float) = apply { 36 | defaultStiffness = value 37 | } 38 | 39 | fun x( 40 | value: Float, 41 | config: SpringAnimationConfig.() -> Unit = {} 42 | ): ViewPropertySpringAnimator = 43 | animateProperty(DynamicAnimation.X, value, config) 44 | 45 | /** 46 | * Eager evaluate the final value from the current value. 47 | */ 48 | fun xBy( 49 | value: Float, 50 | config: SpringAnimationConfig.() -> Unit = {} 51 | ): ViewPropertySpringAnimator = 52 | animatePropertyBy(DynamicAnimation.X, value, config) 53 | 54 | fun y( 55 | value: Float, 56 | config: SpringAnimationConfig.() -> Unit = {} 57 | ): ViewPropertySpringAnimator = 58 | animateProperty(DynamicAnimation.Y, value, config) 59 | 60 | /** 61 | * Eager evaluate the final value from the current value. 62 | */ 63 | fun yBy( 64 | value: Float, 65 | config: SpringAnimationConfig.() -> Unit = {} 66 | ): ViewPropertySpringAnimator = 67 | animatePropertyBy(DynamicAnimation.Y, value, config) 68 | 69 | fun z( 70 | value: Float, 71 | config: SpringAnimationConfig.() -> Unit = {} 72 | ): ViewPropertySpringAnimator = 73 | animateProperty(DynamicAnimation.Z, value, config) 74 | 75 | /** 76 | * Eager evaluate the final value from the current value. 77 | */ 78 | fun zBy( 79 | value: Float, 80 | config: SpringAnimationConfig.() -> Unit = {} 81 | ): ViewPropertySpringAnimator = 82 | animatePropertyBy(DynamicAnimation.Z, value, config) 83 | 84 | fun rotation( 85 | value: Float, 86 | config: SpringAnimationConfig.() -> Unit = {} 87 | ): ViewPropertySpringAnimator = 88 | animateProperty(DynamicAnimation.ROTATION, value, config) 89 | 90 | /** 91 | * Eager evaluate the final value from the current value. 92 | */ 93 | fun rotationBy( 94 | value: Float, 95 | config: SpringAnimationConfig.() -> Unit = {} 96 | ): ViewPropertySpringAnimator = 97 | animatePropertyBy(DynamicAnimation.ROTATION, value, config) 98 | 99 | fun rotationX( 100 | value: Float, 101 | config: SpringAnimationConfig.() -> Unit = {} 102 | ): ViewPropertySpringAnimator = 103 | animateProperty(DynamicAnimation.ROTATION_X, value, config) 104 | 105 | /** 106 | * Eager evaluate the final value from the current value. 107 | */ 108 | fun rotationXBy( 109 | value: Float, 110 | config: SpringAnimationConfig.() -> Unit = {} 111 | ): ViewPropertySpringAnimator = 112 | animatePropertyBy(DynamicAnimation.ROTATION_X, value, config) 113 | 114 | fun rotationY( 115 | value: Float, 116 | config: SpringAnimationConfig.() -> Unit = {} 117 | ): ViewPropertySpringAnimator = 118 | animateProperty(DynamicAnimation.ROTATION_Y, value, config) 119 | 120 | /** 121 | * Eager evaluate the final value from the current value. 122 | */ 123 | fun rotationYBy( 124 | value: Float, 125 | config: SpringAnimationConfig.() -> Unit = {} 126 | ): ViewPropertySpringAnimator = 127 | animatePropertyBy(DynamicAnimation.ROTATION_Y, value, config) 128 | 129 | fun translationX( 130 | value: Float, 131 | config: SpringAnimationConfig.() -> Unit = {} 132 | ): ViewPropertySpringAnimator = 133 | animateProperty(DynamicAnimation.TRANSLATION_X, value, config) 134 | 135 | /** 136 | * Eager evaluate the final value from the current value. 137 | */ 138 | fun translationXBy( 139 | value: Float, 140 | config: SpringAnimationConfig.() -> Unit = {} 141 | ): ViewPropertySpringAnimator = 142 | animatePropertyBy(DynamicAnimation.TRANSLATION_X, value, config) 143 | 144 | fun translationY( 145 | value: Float, 146 | config: SpringAnimationConfig.() -> Unit = {} 147 | ): ViewPropertySpringAnimator = 148 | animateProperty(DynamicAnimation.TRANSLATION_Y, value, config) 149 | 150 | /** 151 | * Eager evaluate the final value from the current value. 152 | */ 153 | fun translationYBy( 154 | value: Float, 155 | config: SpringAnimationConfig.() -> Unit = {} 156 | ): ViewPropertySpringAnimator = 157 | animatePropertyBy(DynamicAnimation.TRANSLATION_Y, value, config) 158 | 159 | fun translationZ( 160 | value: Float, 161 | config: SpringAnimationConfig.() -> Unit = {} 162 | ): ViewPropertySpringAnimator = 163 | animateProperty(DynamicAnimation.TRANSLATION_Z, value, config) 164 | 165 | /** 166 | * Eager evaluate the final value from the current value. 167 | */ 168 | fun translationZBy( 169 | value: Float, 170 | config: SpringAnimationConfig.() -> Unit = {} 171 | ): ViewPropertySpringAnimator = 172 | animatePropertyBy(DynamicAnimation.TRANSLATION_Z, value, config) 173 | 174 | fun scaleX( 175 | value: Float, 176 | config: SpringAnimationConfig.() -> Unit = {} 177 | ): ViewPropertySpringAnimator = 178 | animateProperty(DynamicAnimation.SCALE_X, value, config) 179 | 180 | /** 181 | * Eager evaluate the final value from the current value. 182 | */ 183 | fun scaleXBy( 184 | value: Float, 185 | config: SpringAnimationConfig.() -> Unit = {} 186 | ): ViewPropertySpringAnimator = 187 | animatePropertyBy(DynamicAnimation.SCALE_X, value, config) 188 | 189 | fun scaleY( 190 | value: Float, 191 | config: SpringAnimationConfig.() -> Unit = {} 192 | ): ViewPropertySpringAnimator = 193 | animateProperty(DynamicAnimation.SCALE_Y, value, config) 194 | 195 | /** 196 | * Eager evaluate the final value from the current value. 197 | */ 198 | fun scaleYBy( 199 | value: Float, 200 | config: SpringAnimationConfig.() -> Unit = {} 201 | ): ViewPropertySpringAnimator = 202 | animatePropertyBy(DynamicAnimation.SCALE_Y, value, config) 203 | 204 | fun alpha( 205 | value: Float, 206 | config: SpringAnimationConfig.() -> Unit = {} 207 | ): ViewPropertySpringAnimator = 208 | animateProperty(DynamicAnimation.ALPHA, value, config) 209 | 210 | /** 211 | * Eager evaluate the final value from the current value. 212 | */ 213 | fun alphaBy( 214 | value: Float, 215 | config: SpringAnimationConfig.() -> Unit = {} 216 | ): ViewPropertySpringAnimator = 217 | animatePropertyBy(DynamicAnimation.ALPHA, value, config) 218 | 219 | /** 220 | * A new [SpringAnimation] will be created every time you call this method. 221 | */ 222 | fun animateProperty( 223 | value: Float, 224 | setter: T.(Float) -> Unit, 225 | getter: T.() -> Float, 226 | config: SpringAnimationConfig.() -> Unit = {} 227 | ): ViewPropertySpringAnimator = 228 | animateProperty(createCustomProperty(setter, getter), value, config) 229 | 230 | /** 231 | * Eager evaluate the final value from the current value. 232 | * A new [SpringAnimation] will be created every time you call this method. 233 | */ 234 | fun animatePropertyBy( 235 | setter: T.(Float) -> Unit, 236 | getter: T.() -> Float, 237 | value: Float, 238 | config: SpringAnimationConfig.() -> Unit = {} 239 | ): ViewPropertySpringAnimator = 240 | animatePropertyBy(createCustomProperty(setter, getter), value, config) 241 | 242 | fun animateProperty( 243 | property: FloatPropertyCompat, 244 | value: Float, 245 | config: SpringAnimationConfig.() -> Unit = {} 246 | ) = apply { 247 | animatePropertyInternal(property, value, config) 248 | } 249 | 250 | /** 251 | * Eager evaluate the final value from the current value. 252 | */ 253 | fun animatePropertyBy( 254 | property: FloatPropertyCompat, 255 | value: Float, 256 | config: SpringAnimationConfig.() -> Unit = {} 257 | ) = apply { 258 | animatePropertyInternal(property, value + property.getValue(view), config) 259 | } 260 | 261 | @MainThread 262 | fun start(): ViewPropertySpringAnimator = apply { 263 | if (pendingAnimations.isEmpty()) return@apply 264 | val animations = pendingAnimations.toList() 265 | pendingAnimations.clear() 266 | animatorListener?.onAnimationStart(this) 267 | animations.forEach { it.start() } 268 | } 269 | 270 | @MainThread 271 | fun cancel() { 272 | pendingAnimations.clear() 273 | val animations = animatorMap.values.toList() 274 | animatorMap.clear() 275 | animations.forEach { it.cancel() } 276 | animatorListener?.onAnimationCancel(this) 277 | } 278 | 279 | @MainThread 280 | fun skipToEnd() { 281 | pendingAnimations.clear() 282 | val animations = animatorMap.values.toList() 283 | animations.filter { it.canSkipToEnd() } 284 | .forEach { it.skipToEnd() } 285 | } 286 | 287 | fun setListener( 288 | onStart: (animator: ViewPropertySpringAnimator) -> Unit = {}, 289 | onCancel: (animator: ViewPropertySpringAnimator) -> Unit = {}, 290 | onEnd: (animator: ViewPropertySpringAnimator) -> Unit = {} 291 | ) = setListener(object : AnimatorListener { 292 | override fun onAnimationStart(animator: ViewPropertySpringAnimator) { 293 | onStart(animator) 294 | } 295 | 296 | override fun onAnimationCancel(animator: ViewPropertySpringAnimator) { 297 | onCancel(animator) 298 | } 299 | 300 | override fun onAnimationEnd(animator: ViewPropertySpringAnimator) { 301 | onEnd(animator) 302 | } 303 | }) 304 | 305 | fun setListener(listener: AnimatorListener?) = apply { 306 | this.animatorListener = listener 307 | } 308 | 309 | fun removeUpdateListener(listener: DynamicAnimation.OnAnimationUpdateListener) { 310 | animatorMap.forEach { (_, animation) -> 311 | animation.removeUpdateListener(listener) 312 | } 313 | } 314 | 315 | fun removeEndListener(listener: DynamicAnimation.OnAnimationEndListener) { 316 | animatorMap.forEach { (_, animation) -> 317 | animation.removeEndListener(listener) 318 | } 319 | } 320 | 321 | private fun createCustomProperty( 322 | setter: T.(Float) -> Unit, 323 | getter: T.() -> Float 324 | ) = object : FloatPropertyCompat("CustomProperty") { 325 | override fun getValue(view: T): Float { 326 | return getter.invoke(view) 327 | } 328 | 329 | override fun setValue(view: T, value: Float) { 330 | setter.invoke(view, value) 331 | } 332 | } 333 | 334 | private fun animatePropertyInternal( 335 | property: FloatPropertyCompat, 336 | finalValue: Float, 337 | configBuilder: SpringAnimationConfig.() -> Unit = {} 338 | ) { 339 | var anim = animatorMap[property] 340 | if (anim == null) { 341 | anim = SpringAnimation(view, property) 342 | anim.cleanSelfOnEnd(property) 343 | animatorMap[property] = anim 344 | } 345 | val config = SpringAnimationConfig(finalValue).apply(configBuilder) 346 | config.defaultDampingRatio = defaultDampingRatio 347 | config.defaultStiffness = defaultStiffness 348 | pendingAnimations += SpringAnimationHolder(anim, config) 349 | } 350 | 351 | private fun SpringAnimation.cleanSelfOnEnd( 352 | property: FloatPropertyCompat 353 | ) { 354 | val listener = object : DynamicAnimation.OnAnimationEndListener { 355 | override fun onAnimationEnd( 356 | animation: DynamicAnimation>?, 357 | canceled: Boolean, 358 | value: Float, 359 | velocity: Float 360 | ) { 361 | animatorMap.remove(property) 362 | animation?.removeEndListener(this) 363 | 364 | if (animatorMap.isEmpty() && !canceled) { 365 | animatorListener?.onAnimationEnd(this@ViewPropertySpringAnimator) 366 | } 367 | } 368 | } 369 | addEndListener(listener) 370 | } 371 | } 372 | -------------------------------------------------------------------------------- /library/src/main/java/com/github/lcdsmao/springx/ViewPropertySpringAnimatorExt.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import android.view.View 4 | 5 | @Suppress("UNCHECKED_CAST") 6 | fun T.spring(): ViewPropertySpringAnimator { 7 | var springAnimator = getTag(R.id.view_property_spring_animator_key) as? ViewPropertySpringAnimator 8 | if (springAnimator == null) { 9 | springAnimator = ViewPropertySpringAnimator(this) 10 | setTag(R.id.view_property_spring_animator_key, springAnimator) 11 | } 12 | return springAnimator 13 | } 14 | -------------------------------------------------------------------------------- /library/src/main/res/layout/activity_animation.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | -------------------------------------------------------------------------------- /library/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Spring 3 | 4 | -------------------------------------------------------------------------------- /library/src/test/java/com/github/lcdsmao/springx/UnitUiTestScope.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import android.os.Handler 4 | import android.os.Looper 5 | import org.robolectric.shadows.ShadowLooper 6 | import java.util.concurrent.CountDownLatch 7 | import kotlin.concurrent.thread 8 | 9 | @Suppress("unused") 10 | class UnitUiTestScope : UiTestScope { 11 | 12 | private val mainHandler = Handler(Looper.getMainLooper()) 13 | 14 | override fun runOnMainSync(block: () -> Unit) { 15 | val latch = CountDownLatch(1) 16 | mainHandler.post { 17 | try { 18 | block.invoke() 19 | } finally { 20 | latch.countDown() 21 | } 22 | } 23 | latch.await() 24 | } 25 | 26 | override fun waitForIdleSync() { 27 | val latch = CountDownLatch(1) 28 | mainHandler.post { 29 | ShadowLooper.idleMainLooper() 30 | latch.countDown() 31 | } 32 | latch.await() 33 | } 34 | 35 | class Runner : UiTestScope.Runner { 36 | 37 | override fun runUiTest(block: () -> Unit) { 38 | val testThread = thread(name = "Unit UI Test Thread") { 39 | block.invoke() 40 | } 41 | while (testThread.isAlive) { 42 | ShadowLooper.idleMainLooper() 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /library/src/uiTest/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /library/src/uiTest/java/com/github/lcdsmao/springx/AnimationActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | 5 | class AnimationActivity : AppCompatActivity(R.layout.activity_animation) 6 | -------------------------------------------------------------------------------- /library/src/uiTest/java/com/github/lcdsmao/springx/UiTestScope.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | interface UiTestScope { 4 | 5 | interface Runner { 6 | fun runUiTest(block: () -> Unit) 7 | } 8 | 9 | fun runOnMainSync(block: () -> Unit) 10 | fun waitForIdleSync() 11 | } 12 | 13 | fun runUiTest(uiTestScope: UiTestScope.() -> Unit) { 14 | val (scope, runner) = getUiTestDelegate() 15 | runner.runUiTest { uiTestScope(scope) } 16 | } 17 | 18 | private fun getUiTestDelegate(): Pair { 19 | val testScopeName = 20 | if (System.getProperty("java.runtime.name")!!.toLowerCase().contains("android")) { 21 | "com.github.lcdsmao.springx.InstrumentationUiTestScope" 22 | } else { 23 | "com.github.lcdsmao.springx.UnitUiTestScope" 24 | } 25 | val runnerName = "$testScopeName\$Runner" 26 | return Class.forName(testScopeName).newInstance() as UiTestScope to 27 | Class.forName(runnerName).newInstance() as UiTestScope.Runner 28 | } 29 | -------------------------------------------------------------------------------- /library/src/uiTest/java/com/github/lcdsmao/springx/ViewPropertySpringAnimatorTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springx 2 | 3 | import android.view.View 4 | import androidx.dynamicanimation.animation.DynamicAnimation 5 | import androidx.dynamicanimation.animation.FloatPropertyCompat 6 | import androidx.dynamicanimation.animation.SpringForce 7 | import androidx.test.ext.junit.rules.ActivityScenarioRule 8 | import androidx.test.ext.junit.runners.AndroidJUnit4 9 | import com.google.common.truth.Truth 10 | import io.mockk.mockk 11 | import io.mockk.verify 12 | import io.mockk.verifySequence 13 | import org.junit.Before 14 | import org.junit.Ignore 15 | import org.junit.Rule 16 | import org.junit.Test 17 | import org.junit.runner.RunWith 18 | import org.robolectric.annotation.LooperMode 19 | 20 | @RunWith(AndroidJUnit4::class) 21 | @LooperMode(LooperMode.Mode.PAUSED) 22 | class ViewPropertySpringAnimatorTest { 23 | 24 | @get:Rule 25 | val activityRule = ActivityScenarioRule(AnimationActivity::class.java) 26 | 27 | private lateinit var animView: View 28 | 29 | @Before 30 | fun setup() { 31 | activityRule.scenario.onActivity { 32 | animView = it.findViewById(R.id.anim_view) 33 | } 34 | } 35 | 36 | @Test 37 | fun testAnimateCommonProperty() = runUiTest { 38 | val cases = listOf( 39 | DynamicAnimation.X to 100f, 40 | DynamicAnimation.Y to -100f, 41 | DynamicAnimation.Z to 20f, 42 | DynamicAnimation.TRANSLATION_X to -200f, 43 | DynamicAnimation.TRANSLATION_Y to 200f, 44 | DynamicAnimation.TRANSLATION_Z to -20f, 45 | DynamicAnimation.ROTATION to 45f, 46 | DynamicAnimation.ROTATION_X to 30f, 47 | DynamicAnimation.ROTATION_Y to 60f, 48 | DynamicAnimation.SCALE_X to 4f, 49 | DynamicAnimation.SCALE_Y to 8f, 50 | DynamicAnimation.ALPHA to 0.5f 51 | ) 52 | 53 | cases.forEach { (p, v) -> 54 | val listener = mockk>(relaxed = true) 55 | val anim = ViewPropertySpringAnimator(animView).animate(p, v, false) 56 | .setListener(listener) 57 | runOnMainSync { 58 | anim.start() 59 | Truth.assertThat(anim.isRunning).isTrue() 60 | anim.skipToEnd() 61 | } 62 | verify(exactly = 1) { listener.onAnimationStart(anim) } 63 | verify(exactly = 1, timeout = 1000) { listener.onAnimationEnd(anim) } 64 | Truth.assertThat(anim.isRunning).isFalse() 65 | Truth.assertThat(p.getValue(animView)).isEqualTo(v) 66 | } 67 | } 68 | 69 | @Test 70 | fun testAnimateByCommonProperty() = runUiTest { 71 | val cases = listOf( 72 | DynamicAnimation.X to 100f, 73 | DynamicAnimation.Y to -100f, 74 | DynamicAnimation.Z to 20f, 75 | DynamicAnimation.TRANSLATION_X to -200f, 76 | DynamicAnimation.TRANSLATION_Y to 200f, 77 | DynamicAnimation.TRANSLATION_Z to -20f, 78 | DynamicAnimation.ROTATION to 45f, 79 | DynamicAnimation.ROTATION_X to 30f, 80 | DynamicAnimation.ROTATION_Y to 60f, 81 | DynamicAnimation.SCALE_X to 4f, 82 | DynamicAnimation.SCALE_Y to 8f, 83 | DynamicAnimation.ALPHA to -0.5f 84 | ) 85 | 86 | cases.forEach { (p, v) -> 87 | val oldValue = p.getValue(animView) 88 | val listener = mockk>(relaxed = true) 89 | val anim = ViewPropertySpringAnimator(animView).animate(p, v, true) 90 | .setListener(listener) 91 | runOnMainSync { 92 | anim.start() 93 | Truth.assertThat(anim.isRunning).isTrue() 94 | anim.skipToEnd() 95 | } 96 | verify(exactly = 1) { listener.onAnimationStart(anim) } 97 | verify(exactly = 1, timeout = 1000) { listener.onAnimationEnd(anim) } 98 | Truth.assertThat(anim.isRunning).isFalse() 99 | Truth.assertThat(p.getValue(animView)).isEqualTo(oldValue + v) 100 | } 101 | } 102 | 103 | @Test 104 | fun testAnimateCustomProperty() = runUiTest { 105 | val property = object : FloatPropertyCompat("Custom") { 106 | 107 | private var value = 50f 108 | 109 | override fun getValue(view: View): Float { 110 | return value 111 | } 112 | 113 | override fun setValue(view: View, value: Float) { 114 | this.value = value 115 | } 116 | } 117 | val listener = mockk>(relaxed = true) 118 | val anim = ViewPropertySpringAnimator(animView).animateProperty(property, 100f) 119 | .setListener(listener) 120 | runOnMainSync { 121 | anim.start() 122 | Truth.assertThat(anim.isRunning).isTrue() 123 | anim.skipToEnd() 124 | } 125 | verify(exactly = 1) { listener.onAnimationStart(anim) } 126 | verify(exactly = 1, timeout = 1000) { listener.onAnimationEnd(anim) } 127 | Truth.assertThat(anim.isRunning).isFalse() 128 | Truth.assertThat(property.getValue(animView)).isEqualTo(100f) 129 | } 130 | 131 | @Test 132 | fun testAnimateByCustomProperty() = runUiTest { 133 | val property = object : FloatPropertyCompat("Custom") { 134 | 135 | private var value = 50f 136 | 137 | override fun getValue(view: View): Float { 138 | return value 139 | } 140 | 141 | override fun setValue(view: View, value: Float) { 142 | this.value = value 143 | } 144 | } 145 | val listener = mockk>(relaxed = true) 146 | val anim = ViewPropertySpringAnimator(animView).animatePropertyBy(property, 100f) 147 | .setListener(listener) 148 | runOnMainSync { 149 | anim.start() 150 | Truth.assertThat(anim.isRunning).isTrue() 151 | anim.skipToEnd() 152 | } 153 | verify(exactly = 1) { listener.onAnimationStart(anim) } 154 | verify(exactly = 1, timeout = 1000) { listener.onAnimationEnd(anim) } 155 | Truth.assertThat(anim.isRunning).isFalse() 156 | Truth.assertThat(property.getValue(animView)).isEqualTo(50f + 100f) 157 | } 158 | 159 | @Test 160 | fun testAnimatorReuse() = runUiTest { 161 | val anim1 = animView.spring().translationX(-100f) 162 | val listener = mockk>(relaxed = true) 163 | anim1.setListener(listener) 164 | runOnMainSync { anim1.start() } 165 | 166 | val anim2 = animView.spring().translationX(100f) 167 | runOnMainSync { anim2.start() } 168 | Truth.assertThat(anim1).isSameAs(anim2) 169 | 170 | runOnMainSync { anim1.skipToEnd() } 171 | verify(exactly = 1, timeout = 1000) { listener.onAnimationEnd(anim1) } 172 | Truth.assertThat(animView.translationX).isEqualTo(100f) 173 | } 174 | 175 | @Test 176 | @Ignore("Fix the implementation of [ViewPropertySpringAnimator] to let this test pass") 177 | fun testDampingRatio() = runUiTest { 178 | val onEnd = mockk<(Int) -> Unit>(relaxed = true) 179 | val anim = animView.spring() 180 | .defaultDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY) 181 | .rotation(100f) { 182 | dampingRatio = SpringForce.DAMPING_RATIO_HIGH_BOUNCY 183 | onEnd { _, _, _, _ -> onEnd.invoke(2) } 184 | } 185 | .translationY(100f) { 186 | dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY 187 | onEnd { _, _, _, _ -> onEnd.invoke(1) } 188 | } 189 | .translationX(100f) { 190 | onEnd { _, _, _, _ -> onEnd.invoke(0) } 191 | } 192 | .setListener(onEnd = { onEnd.invoke(3) }) 193 | runOnMainSync { anim.start() } 194 | verify(exactly = 4, timeout = 1000L) { onEnd.invoke(any()) } 195 | verifySequence { 196 | onEnd.invoke(0) 197 | onEnd.invoke(1) 198 | onEnd.invoke(2) 199 | onEnd.invoke(3) 200 | } 201 | } 202 | 203 | @Test 204 | @Ignore("Fix the implementation of [ViewPropertySpringAnimator] to let this test pass") 205 | fun testStiffness() = runUiTest { 206 | val onEnd = mockk<(Int) -> Unit>(relaxed = true) 207 | val anim = animView.spring() 208 | .defaultStiffness(SpringForce.STIFFNESS_HIGH) 209 | .rotation(100f) { 210 | stiffness = SpringForce.STIFFNESS_MEDIUM 211 | onEnd { _, _, _, _ -> onEnd.invoke(2) } 212 | } 213 | .translationY(100f) { 214 | stiffness = SpringForce.STIFFNESS_LOW 215 | onEnd { _, _, _, _ -> onEnd.invoke(1) } 216 | } 217 | .translationX(100f) { 218 | onEnd { _, _, _, _ -> onEnd.invoke(0) } 219 | } 220 | .setListener(onEnd = { onEnd.invoke(3) }) 221 | runOnMainSync { anim.start() } 222 | verify(exactly = 4, timeout = 1000L) { onEnd.invoke(any()) } 223 | verifySequence { 224 | onEnd.invoke(0) 225 | onEnd.invoke(1) 226 | onEnd.invoke(2) 227 | onEnd.invoke(3) 228 | } 229 | } 230 | 231 | private fun ViewPropertySpringAnimator.animate( 232 | property: DynamicAnimation.ViewProperty, 233 | value: Float, 234 | relative: Boolean 235 | ) = apply { 236 | when (property) { 237 | DynamicAnimation.X -> if (relative) xBy(value) else x(value) 238 | DynamicAnimation.Y -> if (relative) yBy(value) else y(value) 239 | DynamicAnimation.Z -> if (relative) zBy(value) else z(value) 240 | DynamicAnimation.TRANSLATION_X -> if (relative) translationXBy(value) else translationX(value) 241 | DynamicAnimation.TRANSLATION_Y -> if (relative) translationYBy(value) else translationY(value) 242 | DynamicAnimation.TRANSLATION_Z -> if (relative) translationZBy(value) else translationZ(value) 243 | DynamicAnimation.ROTATION -> if (relative) rotationBy(value) else rotation(value) 244 | DynamicAnimation.ROTATION_X -> if (relative) rotationXBy(value) else rotationX(value) 245 | DynamicAnimation.ROTATION_Y -> if (relative) rotationYBy(value) else rotationY(value) 246 | DynamicAnimation.SCALE_X -> if (relative) scaleXBy(value) else scaleX(value) 247 | DynamicAnimation.SCALE_Y -> if (relative) scaleYBy(value) else scaleY(value) 248 | DynamicAnimation.ALPHA -> if (relative) alphaBy(value) else alpha(value) 249 | } 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion compile_sdk_version 8 | 9 | defaultConfig { 10 | applicationId "com.github.lcdsmao.springsample" 11 | minSdkVersion min_sdk_version 12 | targetSdkVersion target_sdk_version 13 | versionCode version_code 14 | versionName version_name 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | dataBinding { 25 | enabled = true 26 | } 27 | 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = "1.8" 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation fileTree(dir: 'libs', include: ['*.jar']) 39 | implementation project(':library') 40 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 41 | implementation "androidx.appcompat:appcompat:$appcompat_version" 42 | implementation "androidx.core:core-ktx:$corektx_version" 43 | implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version" 44 | implementation "androidx.navigation:navigation-ui-ktx:$navigation_version" 45 | implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version" 46 | testImplementation 'junit:junit:4.12' 47 | androidTestImplementation 'androidx.test:runner:1.2.0' 48 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 49 | } 50 | -------------------------------------------------------------------------------- /sample/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 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/BindingAdapters.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import android.widget.SeekBar 4 | import androidx.databinding.BindingAdapter 5 | 6 | @BindingAdapter("onProgressChanged") 7 | fun SeekBar.setProgressChangedListener(listener: OnProgressChangedListener) { 8 | setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { 9 | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { 10 | listener.onProgressChanged(progress) 11 | } 12 | 13 | override fun onStartTrackingTouch(seekBar: SeekBar?) { 14 | } 15 | 16 | override fun onStopTrackingTouch(seekBar: SeekBar?) { 17 | } 18 | }) 19 | } 20 | 21 | @BindingAdapter("progress") 22 | fun SeekBar.setProgressIfNotEqual(progress: Int) { 23 | if (this.progress != progress) { 24 | this.progress = progress 25 | } 26 | } 27 | 28 | interface OnProgressChangedListener { 29 | fun onProgressChanged(progress: Int) 30 | } 31 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/DragExampleFragment.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import android.graphics.Rect 4 | import android.os.Bundle 5 | import android.view.MotionEvent 6 | import android.view.View 7 | import androidx.fragment.app.Fragment 8 | import androidx.fragment.app.viewModels 9 | import com.github.lcdsmao.springx.spring 10 | import com.github.lcdsmao.springsample.databinding.FragmentDragExampleBinding 11 | 12 | class DragExampleFragment : Fragment(R.layout.fragment_drag_example) { 13 | 14 | private val viewModel by viewModels() 15 | private lateinit var binding: FragmentDragExampleBinding 16 | private val onTouchListener = object : View.OnTouchListener { 17 | 18 | private var isDragging: Boolean = false 19 | private val hitRect = Rect() 20 | 21 | override fun onTouch(v: View, event: MotionEvent): Boolean { 22 | val image1 = binding.image1 23 | val image2 = binding.image2 24 | val image3 = binding.image3 25 | when (event.actionMasked) { 26 | MotionEvent.ACTION_DOWN -> { 27 | image1.getHitRect(hitRect) 28 | if (hitRect.contains(event.x.toInt(), event.y.toInt())) { 29 | isDragging = true 30 | } 31 | } 32 | MotionEvent.ACTION_MOVE -> { 33 | if (isDragging) { 34 | val x = event.x - image1.width / 2 35 | val y = event.y - image1.width / 2 36 | val isDraggingRight = x > image1.x 37 | image1.x = x 38 | image1.y = y 39 | 40 | image2.spring() 41 | .defaultDampingRatio(viewModel.dampingRatio.value!!) 42 | .defaultStiffness(viewModel.stiffness.value!!) 43 | .translationX(image1.translationX) { 44 | onUpdate { _, value, _ -> 45 | image3.spring() 46 | .defaultDampingRatio(viewModel.dampingRatio.value!!) 47 | .defaultStiffness(viewModel.stiffness.value!!) 48 | .translationX(value) 49 | .start() 50 | } 51 | } 52 | .translationY(image1.translationY) { 53 | onUpdate { _, value, _ -> 54 | image3.spring() 55 | .defaultDampingRatio(viewModel.dampingRatio.value!!) 56 | .defaultStiffness(viewModel.stiffness.value!!) 57 | .translationY(value) 58 | .start() 59 | } 60 | } 61 | .rotation(0f) { 62 | startValue = if (isDraggingRight) { 63 | 100f 64 | } else { 65 | -100f 66 | } 67 | minValue = -120f 68 | maxValue = 120f 69 | } 70 | .start() 71 | } 72 | } 73 | MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { 74 | v.performClick() 75 | isDragging = false 76 | } 77 | } 78 | return true 79 | } 80 | } 81 | 82 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 83 | binding = FragmentDragExampleBinding.bind(view) 84 | binding.lifecycleOwner = viewLifecycleOwner 85 | binding.viewModel = viewModel 86 | binding.root.setOnTouchListener(onTouchListener) 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/DragSampleViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import androidx.dynamicanimation.animation.SpringForce 4 | import androidx.lifecycle.LiveData 5 | import androidx.lifecycle.MutableLiveData 6 | import androidx.lifecycle.Transformations 7 | import androidx.lifecycle.ViewModel 8 | 9 | private const val STIFFNESS_MAX = 10_000f 10 | private const val STIFFNESS_MIN = 100f 11 | 12 | class DragSampleViewModel : ViewModel() { 13 | 14 | private val _dampingRatio = MutableLiveData().apply { 15 | value = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY 16 | } 17 | val dampingRatio: LiveData 18 | get() = _dampingRatio 19 | val dampingProgress: LiveData 20 | get() = Transformations.map(dampingRatio) { 21 | (it * 100).toInt() 22 | } 23 | 24 | private val _stiffness = MutableLiveData().apply { 25 | value = SpringForce.STIFFNESS_MEDIUM 26 | } 27 | val stiffness: LiveData 28 | get() = _stiffness 29 | val stiffnessProgress: LiveData 30 | get() = Transformations.map(stiffness) { 31 | ((it - STIFFNESS_MIN) / (STIFFNESS_MAX - STIFFNESS_MIN) * 100).toInt() 32 | } 33 | 34 | fun setDampingRatio(value: Float) { 35 | _dampingRatio.value = value 36 | } 37 | 38 | fun setStiffness(value: Float) { 39 | _stiffness.value = value 40 | } 41 | 42 | fun setDampingProgress(progress: Int) { 43 | setDampingRatio(progress / 100f) 44 | } 45 | 46 | fun setStiffnessProgress(progress: Int) { 47 | setStiffness(progress.toFloat() / 100 * (STIFFNESS_MAX - STIFFNESS_MIN) + STIFFNESS_MIN) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.databinding.DataBindingUtil 6 | import androidx.navigation.NavController 7 | import androidx.navigation.findNavController 8 | import androidx.navigation.ui.AppBarConfiguration 9 | import androidx.navigation.ui.setupWithNavController 10 | import com.github.lcdsmao.springsample.databinding.ActivityMainBinding 11 | 12 | class MainActivity : AppCompatActivity() { 13 | 14 | private lateinit var binding: ActivityMainBinding 15 | private lateinit var navController: NavController 16 | 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | binding = DataBindingUtil.setContentView(this, R.layout.activity_main) 20 | navController = findNavController(R.id.nav_host_fragment) 21 | binding.navView.setupWithNavController(navController) 22 | setSupportActionBar(binding.toolbar) 23 | val appBarConfiguration = AppBarConfiguration( 24 | setOf( 25 | R.id.dragExampleFragment, 26 | R.id.simpleExampleFragment, 27 | R.id.recyclerViewExampleFragment 28 | ), 29 | binding.drawerLayout 30 | ) 31 | binding.toolbar.setupWithNavController(navController, appBarConfiguration) 32 | } 33 | 34 | override fun onSupportNavigateUp(): Boolean { 35 | return navController.navigateUp() || super.onSupportNavigateUp() 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/RecyclerViewExampleFragment.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import android.graphics.Color 4 | import android.os.Bundle 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import androidx.dynamicanimation.animation.SpringForce 9 | import androidx.fragment.app.Fragment 10 | import androidx.recyclerview.widget.DiffUtil 11 | import androidx.recyclerview.widget.GridLayoutManager 12 | import androidx.recyclerview.widget.ListAdapter 13 | import androidx.recyclerview.widget.RecyclerView 14 | import com.github.lcdsmao.springsample.databinding.FragmentRecyclerViewExampleBinding 15 | import com.github.lcdsmao.springsample.databinding.ItemViewColorBlockBinding 16 | import kotlin.random.Random 17 | 18 | class RecyclerViewExampleFragment : Fragment(R.layout.fragment_recycler_view_example) { 19 | 20 | private lateinit var binding: FragmentRecyclerViewExampleBinding 21 | 22 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 23 | binding = FragmentRecyclerViewExampleBinding.bind(view) 24 | binding.recyclerView.layoutManager = GridLayoutManager(requireContext(), 4) 25 | val adapter = MyAdapter() 26 | binding.recyclerView.adapter = adapter.also { it.submitList(COLORS) } 27 | val animator = SpringMoveItemAnimator().apply { 28 | dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY 29 | stiffness = 20f 30 | } 31 | binding.recyclerView.itemAnimator = animator 32 | binding.shuffle.setOnClickListener { 33 | adapter.submitList(COLORS.partShuffled()) 34 | } 35 | } 36 | 37 | private fun List.partShuffled(): List { 38 | val copy = toMutableList() 39 | val toBeShuffleIndex = (0 until copy.size).filter { 40 | Random.nextFloat() < 0.2f 41 | } 42 | val toBeShuffleElement = toBeShuffleIndex 43 | .zip(toBeShuffleIndex.shuffled()) 44 | .map { 45 | it.first to copy[it.second] 46 | } 47 | toBeShuffleElement.forEach { (k, v) -> 48 | copy[k] = v 49 | } 50 | return copy 51 | } 52 | 53 | class MyAdapter : ListAdapter(INT_DIFF_CALLBACK) { 54 | 55 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { 56 | val inflater = LayoutInflater.from(parent.context) 57 | return MyViewHolder(inflater.inflate(R.layout.item_view_color_block, parent, false)) 58 | } 59 | 60 | override fun onBindViewHolder(holder: MyViewHolder, position: Int) { 61 | holder.bind(getItem(position)) 62 | } 63 | } 64 | 65 | class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 66 | private val binding = ItemViewColorBlockBinding.bind(itemView) 67 | fun bind(color: Int) { 68 | binding.cardView.setCardBackgroundColor(color) 69 | binding.executePendingBindings() 70 | } 71 | } 72 | } 73 | 74 | private val COLORS = listOf( 75 | "#FFEBEE", 76 | "#FFCDD2", 77 | "#E57373", 78 | "#C62828", 79 | "#B71C1C", 80 | "#FF5252", 81 | "#FF1744", 82 | "#F8BBD0", 83 | "#EC407A", 84 | "#D81B60", 85 | "#C2185B", 86 | "#FF80AB", 87 | "#F50057", 88 | "#C51162", 89 | "#E1BEE7", 90 | "#BA68C8", 91 | "#9C27B0", 92 | "#7B1FA2", 93 | "#4A148C", 94 | "#E040FB" 95 | ).map { 96 | Color.parseColor(it) 97 | } 98 | 99 | private val INT_DIFF_CALLBACK = object : DiffUtil.ItemCallback() { 100 | override fun areContentsTheSame(oldItem: Int, newItem: Int): Boolean { 101 | return oldItem == newItem 102 | } 103 | 104 | override fun areItemsTheSame(oldItem: Int, newItem: Int): Boolean { 105 | return oldItem == newItem 106 | } 107 | } 108 | 109 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/SimpleExampleFragment.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import android.os.Bundle 4 | import android.view.View 5 | import androidx.fragment.app.Fragment 6 | import com.github.lcdsmao.springx.spring 7 | import com.github.lcdsmao.springsample.databinding.FragmentSimpleExampleBinding 8 | 9 | class SimpleExampleFragment : Fragment(R.layout.fragment_simple_example) { 10 | 11 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 12 | val binding = FragmentSimpleExampleBinding.bind(view) 13 | val animation = binding.imageIcon.spring() 14 | .defaultDampingRatio(0.7f) 15 | .defaultStiffness(5f) 16 | 17 | binding.buttonLeft.setOnClickListener { 18 | animation 19 | .rotation(30f) 20 | .scaleX(0.5f) 21 | .scaleY(0.5f) 22 | .y(binding.placeHolderLeft.y) 23 | .x(binding.placeHolderLeft.x) 24 | .start() 25 | } 26 | binding.buttonRight.setOnClickListener { 27 | animation 28 | .rotation(-30f) 29 | .scaleX(2f) 30 | .scaleY(2f) 31 | .y(binding.placeHolderRight.y) 32 | .x(binding.placeHolderRight.x) 33 | .start() 34 | } 35 | binding.buttonReset.setOnClickListener { 36 | animation 37 | .rotation(0f) 38 | .scaleX(1f) 39 | .scaleY(1f) 40 | .y(binding.imageIcon.top.toFloat()) { 41 | dampingRatio = 0.8f 42 | stiffness = 1000f 43 | } 44 | .x(binding.imageIcon.left.toFloat()) { 45 | dampingRatio = 0.5f 46 | stiffness = 500f 47 | } 48 | .start() 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/lcdsmao/springsample/SpringMoveItemAnimator.kt: -------------------------------------------------------------------------------- 1 | package com.github.lcdsmao.springsample 2 | 3 | import android.view.View 4 | import androidx.dynamicanimation.animation.SpringForce 5 | import androidx.recyclerview.widget.RecyclerView 6 | import androidx.recyclerview.widget.SimpleItemAnimator 7 | import com.github.lcdsmao.springx.ViewPropertySpringAnimator 8 | import com.github.lcdsmao.springx.spring 9 | 10 | class SpringMoveItemAnimator : SimpleItemAnimator() { 11 | 12 | var dampingRatio: Float = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY 13 | var stiffness: Float = SpringForce.STIFFNESS_MEDIUM 14 | 15 | private val pendingAnimations = mutableListOf() 16 | private val runningAnimations = mutableListOf() 17 | 18 | override fun runPendingAnimations() { 19 | val animations = pendingAnimations.toList() 20 | pendingAnimations.clear() 21 | runningAnimations.addAll(animations) 22 | animations.forEach { 23 | it.itemView.spring().start() 24 | } 25 | } 26 | 27 | override fun animateAdd(holder: RecyclerView.ViewHolder?): Boolean { 28 | dispatchAddFinished(holder) 29 | return false 30 | } 31 | 32 | override fun animateMove( 33 | holder: RecyclerView.ViewHolder?, 34 | fromX: Int, 35 | fromY: Int, 36 | toX: Int, 37 | toY: Int 38 | ): Boolean { 39 | if (holder == null) { 40 | dispatchAddFinished(holder) 41 | return false 42 | } 43 | val currentX = fromX + holder.itemView.translationX 44 | val currentY = fromY + holder.itemView.translationY 45 | val deltaX = toX - currentX 46 | val deltaY = toY - currentY 47 | val animation = holder.itemView.spring() 48 | animation.cancel() 49 | holder.itemView.translationX = -deltaX 50 | holder.itemView.translationY = -deltaY 51 | animation 52 | .reset() 53 | .alpha(1f) 54 | .translationX(0f) { 55 | startVelocity = holder.itemView.translationXVelocity 56 | onEnd { _, _, _, velocity -> 57 | holder.itemView.translationXVelocity = velocity 58 | } 59 | } 60 | .translationY(0f) { 61 | startValue = -deltaY 62 | onEnd { _, _, _, velocity -> 63 | holder.itemView.translationYVelocity = velocity 64 | } 65 | } 66 | .setListener( 67 | onStart = { 68 | dispatchMoveStarting(holder) 69 | }, 70 | onCancel = { 71 | it.setListener(null) 72 | dispatchMoveFinished(holder) 73 | runningAnimations.remove(holder) 74 | }, 75 | onEnd = { 76 | it.setListener(null) 77 | dispatchMoveFinished(holder) 78 | runningAnimations.remove(holder) 79 | } 80 | ) 81 | pendingAnimations.add(holder) 82 | return true 83 | } 84 | 85 | override fun animateChange( 86 | oldHolder: RecyclerView.ViewHolder?, 87 | newHolder: RecyclerView.ViewHolder?, 88 | fromLeft: Int, 89 | fromTop: Int, 90 | toLeft: Int, 91 | toTop: Int 92 | ): Boolean { 93 | if (oldHolder == newHolder) { 94 | return animateMove(oldHolder, fromLeft, fromTop, toLeft, toTop) 95 | } 96 | 97 | dispatchChangeFinished(oldHolder, true) 98 | dispatchChangeFinished(newHolder, false) 99 | return false 100 | } 101 | 102 | override fun animateRemove(holder: RecyclerView.ViewHolder?): Boolean { 103 | dispatchRemoveFinished(holder) 104 | return false 105 | } 106 | 107 | override fun isRunning(): Boolean { 108 | return runningAnimations.isNotEmpty() 109 | } 110 | 111 | override fun endAnimation(item: RecyclerView.ViewHolder) { 112 | item.itemView.spring().skipToEnd() 113 | runningAnimations.remove(item) 114 | if (!isRunning) { 115 | dispatchAnimationsFinished() 116 | } 117 | } 118 | 119 | override fun endAnimations() { 120 | pendingAnimations.clear() 121 | val animations = runningAnimations.toList() 122 | runningAnimations.clear() 123 | animations.forEach { it.itemView.spring().skipToEnd() } 124 | dispatchAnimationsFinished() 125 | } 126 | 127 | private fun ViewPropertySpringAnimator.reset() = apply { 128 | defaultDampingRatio(dampingRatio) 129 | defaultStiffness(stiffness) 130 | } 131 | 132 | private var View.translationXVelocity: Float 133 | set(value) { 134 | setTag(R.id.velocity_translate_x, value) 135 | } 136 | get() { 137 | return getTag(R.id.velocity_translate_x) as? Float ?: 0f 138 | } 139 | 140 | private var View.translationYVelocity: Float 141 | set(value) { 142 | setTag(R.id.velocity_translate_y, value) 143 | } 144 | get() { 145 | return getTag(R.id.velocity_translate_y) as? Float ?: 0f 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_android.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/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 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_shuffle.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 17 | 18 | 22 | 23 | 30 | 31 | 32 | 33 | 41 | 42 | 43 | 44 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/fragment_drag_example.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 15 | 16 | 29 | 30 | 38 | 39 | 48 | 49 | 58 | 59 | 75 | 76 | 85 | 86 | 94 | 95 | 104 | 105 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/fragment_recycler_view_example.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 9 | 10 | 15 | 16 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/fragment_simple_example.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | 18 | 19 | 26 | 27 | 38 | 39 |