├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── RELEASING.md ├── art ├── demo.gif ├── ic_launcher │ ├── res │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ └── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ └── web_hi_res_512.png ├── scissors.png └── scissors.sketch ├── build.gradle ├── gradle.properties ├── gradle ├── deploy_snapshot.sh ├── gradle-mvn-push.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── scissors-sample ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── lyft │ │ └── android │ │ └── scissorssample │ │ ├── App.java │ │ ├── CropResultActivity.java │ │ ├── MainActivity.java │ │ └── RequestCodes.java │ └── res │ ├── drawable-hdpi │ ├── ic_aspect_ratio.png │ ├── ic_content_add.png │ └── ic_content_content_cut.png │ ├── drawable-mdpi │ ├── ic_aspect_ratio.png │ ├── ic_content_add.png │ └── ic_content_content_cut.png │ ├── drawable-xhdpi │ ├── ic_aspect_ratio.png │ ├── ic_content_add.png │ └── ic_content_content_cut.png │ ├── drawable-xxhdpi │ ├── ic_aspect_ratio.png │ ├── ic_content_add.png │ └── ic_content_content_cut.png │ ├── drawable-xxxhdpi │ ├── ic_aspect_ratio.png │ ├── ic_content_add.png │ └── ic_content_content_cut.png │ ├── layout │ ├── activity_crop_result.xml │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-v21 │ └── dimens.xml │ ├── values-v23 │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ ├── styles.xml │ └── themes.xml ├── scissors2 ├── build.gradle ├── gradle.properties ├── proguard-rules.txt └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── lyft │ │ │ └── android │ │ │ └── scissors2 │ │ │ ├── BitmapLoader.java │ │ │ ├── CropRequest.java │ │ │ ├── CropView.java │ │ │ ├── CropViewConfig.java │ │ │ ├── CropViewExtensions.java │ │ │ ├── GlideBitmapLoader.java │ │ │ ├── GlideFillViewportTransformation.java │ │ │ ├── LoadRequest.java │ │ │ ├── PicassoBitmapLoader.java │ │ │ ├── PicassoFillViewportTransformation.java │ │ │ ├── TouchManager.java │ │ │ ├── TouchPoint.java │ │ │ ├── UILBitmapLoader.java │ │ │ ├── UILFillViewportDisplayer.java │ │ │ └── Utils.java │ └── res │ │ └── values │ │ └── attrs.xml │ └── test │ └── java │ └── com │ └── lyft │ └── android │ └── scissors2 │ └── TargetSizeTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | ##### ANDROID ##### 2 | 3 | # built application files 4 | out/ 5 | *.apk 6 | *.ap_ 7 | 8 | # files for the dex VM 9 | *.dex 10 | 11 | # Java class files 12 | *.class 13 | 14 | # generated files 15 | bin/ 16 | gen/ 17 | 18 | # Local configuration file (sdk path, etc) 19 | local.properties 20 | 21 | # Secure keystore settings 22 | 23 | secure.properties 24 | 25 | assets/ 26 | 27 | ##### ANDROID ##### 28 | 29 | ##### Gradle ###### 30 | .gradle 31 | build 32 | 33 | ##### JAVA ##### 34 | 35 | *.class 36 | 37 | # Package Files # 38 | *.war 39 | *.ear 40 | 41 | ##### JAVA ##### 42 | 43 | ##### IntelliJ ##### 44 | 45 | *.iml 46 | *.ipr 47 | *.iws 48 | .idea/ 49 | 50 | ##### IntelliJ ##### 51 | 52 | ##### Eclipse ##### 53 | 54 | *.pydevproject 55 | .project 56 | .metadata 57 | bin/** 58 | tmp/** 59 | tmp/**/* 60 | *.tmp 61 | *.bak 62 | *.swp 63 | *~.nib 64 | local.properties 65 | .classpath 66 | .settings/ 67 | .loadpath 68 | 69 | # External tool builders 70 | .externalToolBuilders/ 71 | 72 | # Locally stored "Eclipse launch configurations" 73 | *.launch 74 | 75 | # CDT-specific 76 | .cproject 77 | 78 | # PDT-specific 79 | .buildpath 80 | 81 | ##### Eclipse ##### 82 | 83 | ##### Maven ##### 84 | 85 | target/ 86 | gen-external-apklibs/ 87 | 88 | ##### Maven ##### 89 | 90 | ## OSX ## 91 | 92 | .DS_Store 93 | 94 | # Thumbnails 95 | ._* 96 | 97 | # Files that might appear on external disk 98 | .Spotlight-V100 99 | .Trashes 100 | 101 | #Crashlytics 102 | 103 | com_crashlytics_export_strings.xml 104 | 105 | # Automation 106 | *.pyc 107 | venv 108 | 109 | junit.xml 110 | executeAppium.bash 111 | automation/**/*.jpeg -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | android: 4 | components: 5 | - tools 6 | - build-tools-24.0.3 7 | - android-24 8 | - extra-android-m2repository 9 | 10 | licenses: 11 | - 'android-sdk-preview-license-.+' 12 | - 'android-sdk-license-.+' 13 | - 'google-gdk-license-.+' 14 | 15 | jdk: 16 | - oraclejdk8 17 | 18 | script: 19 | - ./gradlew clean connectedCheck --stacktrace 20 | 21 | after_success: 22 | - gradle/deploy_snapshot.sh 23 | 24 | branches: 25 | except: 26 | - gh-pages 27 | 28 | notifications: 29 | email: false 30 | 31 | sudo: false 32 | 33 | cache: 34 | directories: 35 | - $HOME/.gradle 36 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 1.1.2 *(2017-03-28)* 5 | ---------------------------- 6 | - Add support for oval overlay 7 | 8 | Version 1.1.1 *(2016-07-21)* 9 | ---------------------------- 10 | - Fix #53, Image not centered wrt viewport 11 | 12 | Version 1.1.0 *(2016-04-15)* 13 | ---------------------------- 14 | 15 | - Add support for full overlay (#27 Thanks @rharter!) 16 | - Breaking changes attrs renamed: 17 | * cropviewViewportHeightRatio -> cropviewViewportRatio 18 | * cropviewViewportHeaderFooterColor -> cropviewViewportOverlayColor 19 | - Add support for `pickUsing` framework `Fragment`s (#43) 20 | 21 | Version 1.0.3 *(2015-02-15)* 22 | ---------------------------- 23 | 24 | Fix misplaced sample resources (#28 Thanks @rharter!) 25 | 26 | Version 1.0.2 *(2015-02-15)* 27 | ---------------------------- 28 | 29 | Add extensions support for Android-Universal-Image-Loader (#10 Thanks @skyfishjy!) 30 | Defer load until first layout pass (Fixes #6) 31 | 32 | 33 | Version 1.0.1 *(2015-11-19)* 34 | ---------------------------- 35 | 36 | Add proguard rules for optional dependencies. 37 | 38 | 39 | Version 1.0.0 *(2015-11-17)* 40 | ---------------------------- 41 | 42 | Initial release. 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ### Code of conduct 2 | 3 | This project is governed by [Lyft's code of 4 | conduct](https://github.com/lyft/code-of-conduct). 5 | All contributors and participants agree to abide by its terms. 6 | 7 | ## Contributing 8 | 9 | 1. Fork the repo 10 | 1. Run the tests. You can do this from the command line with `./gradlew test` 11 | 1. Please make every effort to follow existing conventions and style in order to keep the code as readable and consistent as possible 12 | 1. [Install and use our java codestyles](https://github.com/lyft/java-code-styles) 13 | 1. Add tests if you are adding a feature or fixing a bug 14 | 1. Make your tests pass 15 | 1. Add an entry to the `CHANGELOG.md` 16 | 1. Before your code can be accepted into the project you must also [sign our Individual Contributor License Agreement (CLA)](https://oss.lyft.com/cla). 17 | 1. Carefully review your changes, by using `git diff` and `git diff --cached` if you have staged changes 18 | 1. Commit with a meaningful message 19 | 1. [Rebase and squash](http://rebaseandsqua.sh/) 20 | 1. Push to your fork and submit a pull request! 21 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2015 Lyft, Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ⚠️ This repository has been archived and is no longer accepting contributions ⚠️ 2 | 3 | 4 | 5 | Scissors 6 | ========================= 7 | 8 | Fixed viewport image cropping library for Android with built-in support for [Picasso][picasso], [Glide][glide] or [Universal Image Loader][uil]. 9 | 10 | Usage 11 | ----- 12 | 13 | See `scissors-sample`. 14 | 15 | 16 | 17 | 18 | - Include it on your layout: 19 | ```xml 20 | 26 | ``` 27 | - Set a Bitmap to be cropped. In example by calling `cropView.setImageBitmap(someBitmap);` 28 | - Call `Bitmap croppedBitmap = cropView.crop();` to obtain a cropped Bitmap to match viewport dimensions 29 | 30 | Extensions 31 | ---------- 32 | Scissors comes with handy extensions which help with common tasks like: 33 | 34 | #### Loading a Bitmap 35 | To load a Bitmap automatically with [Picasso][picasso], [Glide][glide] or [Universal Image Loader][uil] into `CropView` use as follows: 36 | 37 | ```java 38 | cropView.extensions() 39 | .load(galleryUri); 40 | ``` 41 | #### Cropping into a File 42 | To save a cropped Bitmap into a `File` use as follows: 43 | 44 | ```java 45 | cropView.extensions() 46 | .crop() 47 | .quality(87) 48 | .format(PNG) 49 | .into(croppedFile)) 50 | ``` 51 | 52 | Questions 53 | ---------- 54 | For questions please use github issues. Mark question issue with "question" label. 55 | 56 | Download 57 | -------- 58 | 59 | ```groovy 60 | compile 'com.lyft:scissors:1.1.1' 61 | ``` 62 | 63 | Snapshots of development version are available in [Sonatype's `snapshots` repository][snap]. 64 | 65 | License 66 | ------- 67 | 68 | Copyright (C) 2015 Lyft, Inc. 69 | 70 | Licensed under the Apache License, Version 2.0 (the "License"); 71 | you may not use this file except in compliance with the License. 72 | You may obtain a copy of the License at 73 | 74 | http://www.apache.org/licenses/LICENSE-2.0 75 | 76 | Unless required by applicable law or agreed to in writing, software 77 | distributed under the License is distributed on an "AS IS" BASIS, 78 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 79 | See the License for the specific language governing permissions and 80 | limitations under the License. 81 | 82 | Contributing 83 | ------------ 84 | 85 | Please see `CONTRIBUTING.md`. 86 | 87 | Contributors 88 | ------------ 89 | - [See contributors on GitHub](https://github.com/lyft/scissors/graphs/contributors) 90 | - [Join us, work for Lyft](https://www.lyft.com/jobs) 91 | 92 | [snap]: https://oss.sonatype.org/content/repositories/snapshots/ 93 | [picasso]: https://github.com/square/picasso 94 | [glide]: https://github.com/bumptech/glide 95 | [uil]: https://github.com/nostra13/Android-Universal-Image-Loader 96 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | Releasing 2 | ======== 3 | 4 | 1. Change the version in `gradle.properties` to a non-SNAPSHOT version. 5 | 2. Update the `CHANGELOG.md` for the impending release. 6 | 3. Update the `README.md` with the new version. 7 | 4. `git commit -am "Prepare for release X.Y.Z."` (where X.Y.Z is the new version) 8 | 5. `git tag -a X.Y.X -m "Version X.Y.Z"` (where X.Y.Z is the new version) 9 | 6. `./gradlew clean uploadArchives` 10 | 7. Update the `gradle.properties` to the next SNAPSHOT version. 11 | 8. `git commit -am "Prepare next development version."` 12 | 9. `git push && git push --tags` 13 | 10. Visit [Sonatype Nexus](https://oss.sonatype.org/) and promote the artifact. 14 | -------------------------------------------------------------------------------- /art/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/demo.gif -------------------------------------------------------------------------------- /art/ic_launcher/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/ic_launcher/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /art/ic_launcher/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/ic_launcher/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /art/ic_launcher/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/ic_launcher/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /art/ic_launcher/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/ic_launcher/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /art/ic_launcher/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/ic_launcher/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /art/ic_launcher/web_hi_res_512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/ic_launcher/web_hi_res_512.png -------------------------------------------------------------------------------- /art/scissors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/scissors.png -------------------------------------------------------------------------------- /art/scissors.sketch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/art/scissors.sketch -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:2.2.2' 8 | } 9 | } 10 | 11 | allprojects { 12 | repositories { 13 | jcenter() 14 | maven { 15 | url "https://maven.google.com" 16 | } 17 | } 18 | } 19 | 20 | ext { 21 | minSdkVersion = 14 22 | compileSdkVersion = 24 23 | buildToolsVersion = '24.0.3' 24 | 25 | junitVersion = '4.12' 26 | mockitoVersion = '1.10.19' 27 | robolectricVersion = '3.0' 28 | assertjVersion = '1.7.1' 29 | supportVersion = '24.2.1' 30 | 31 | ci = 'true'.equals(System.getenv('CI')) 32 | } 33 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | GROUP=com.lyft 2 | VERSION_NAME=2.0.0-SNAPSHOT 3 | 4 | POM_DESCRIPTION=Android image cropping library. 5 | 6 | POM_URL=https://github.com/lyft/scissors/ 7 | POM_SCM_URL=https://github.com/lyft/scissors/ 8 | POM_SCM_CONNECTION=scm:git:git://github.com/lyft/scissors.git 9 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/lyft/scissors.git 10 | 11 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 12 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 13 | POM_LICENCE_DIST=repo 14 | 15 | POM_DEVELOPER_ID=lyft 16 | POM_DEVELOPER_NAME=Lyft Open Source -------------------------------------------------------------------------------- /gradle/deploy_snapshot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Deploy artifacts to Sonatype's snapshot repo. 4 | # 5 | # Adapted from https://coderwall.com/p/9b_lfq and 6 | # http://benlimmer.com/2013/12/26/automatically-publish-javadoc-to-gh-pages-with-travis-ci/ 7 | 8 | SLUG="lyft/scissors" 9 | BRANCH="master" 10 | 11 | set -e 12 | 13 | if [ "$TRAVIS_REPO_SLUG" != "$SLUG" ]; then 14 | echo "Skipping snapshot deployment: wrong repository. Expected '$SLUG' but was '$TRAVIS_REPO_SLUG'." 15 | elif [ "$TRAVIS_PULL_REQUEST" != "false" ]; then 16 | echo "Skipping snapshot deployment: was pull request." 17 | elif [ "$TRAVIS_BRANCH" != "$BRANCH" ]; then 18 | echo "Skipping snapshot deployment: wrong branch. Expected '$BRANCH' but was '$TRAVIS_BRANCH'." 19 | else 20 | echo "Deploying snapshot..." 21 | ./gradlew clean uploadArchives 22 | echo "Snapshot deployed!" 23 | fi 24 | -------------------------------------------------------------------------------- /gradle/gradle-mvn-push.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Chris Banes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'maven' 18 | apply plugin: 'signing' 19 | 20 | def isReleaseBuild() { 21 | return VERSION_NAME.contains("SNAPSHOT") == false 22 | } 23 | 24 | def getRepositoryUsername() { 25 | return hasProperty('SONATYPE_NEXUS_USERNAME') ? SONATYPE_NEXUS_USERNAME : "$System.env.SONATYPE_NEXUS_USERNAME" 26 | } 27 | 28 | def getRepositoryPassword() { 29 | return hasProperty('SONATYPE_NEXUS_PASSWORD') ? SONATYPE_NEXUS_PASSWORD : "$System.env.SONATYPE_NEXUS_PASSWORD" 30 | } 31 | 32 | afterEvaluate { project -> 33 | uploadArchives { 34 | repositories { 35 | mavenDeployer { 36 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 37 | 38 | pom.groupId = GROUP 39 | pom.artifactId = POM_ARTIFACT_ID 40 | pom.version = VERSION_NAME 41 | 42 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 43 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 44 | } 45 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 46 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 47 | } 48 | 49 | pom.project { 50 | name POM_NAME 51 | packaging POM_PACKAGING 52 | description POM_DESCRIPTION 53 | url POM_URL 54 | 55 | scm { 56 | url POM_SCM_URL 57 | connection POM_SCM_CONNECTION 58 | developerConnection POM_SCM_DEV_CONNECTION 59 | } 60 | 61 | licenses { 62 | license { 63 | name POM_LICENCE_NAME 64 | url POM_LICENCE_URL 65 | distribution POM_LICENCE_DIST 66 | } 67 | } 68 | 69 | developers { 70 | developer { 71 | id POM_DEVELOPER_ID 72 | name POM_DEVELOPER_NAME 73 | } 74 | } 75 | } 76 | } 77 | } 78 | } 79 | 80 | signing { 81 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 82 | sign configurations.archives 83 | } 84 | 85 | task androidJavadocs(type: Javadoc) { 86 | source = android.sourceSets.main.java.srcDirs 87 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 88 | 89 | if (JavaVersion.current().isJava8Compatible()) { 90 | allprojects { 91 | tasks.withType(Javadoc) { 92 | options.addStringOption('Xdoclint:none', '-quiet') 93 | } 94 | } 95 | } 96 | } 97 | 98 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { 99 | classifier = 'javadoc' 100 | from androidJavadocs.destinationDir 101 | } 102 | 103 | task androidSourcesJar(type: Jar) { 104 | classifier = 'sources' 105 | from android.sourceSets.main.java.sourceFiles 106 | } 107 | 108 | artifacts { 109 | archives androidSourcesJar 110 | archives androidJavadocsJar 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Apr 14 17:41:18 PDT 2016 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-3.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 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 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /scissors-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion rootProject.ext.compileSdkVersion 5 | buildToolsVersion rootProject.ext.buildToolsVersion 6 | defaultConfig { 7 | applicationId 'com.lyft.scissorssample' 8 | minSdkVersion rootProject.ext.minSdkVersion 9 | targetSdkVersion rootProject.ext.compileSdkVersion 10 | versionCode 1 11 | versionName "$VERSION_NAME" 12 | } 13 | 14 | lintOptions { 15 | disable 'InvalidPackage' 16 | } 17 | compileOptions { 18 | sourceCompatibility JavaVersion.VERSION_1_7 19 | targetCompatibility JavaVersion.VERSION_1_7 20 | } 21 | 22 | dexOptions { 23 | preDexLibraries = !rootProject.ext.ci 24 | } 25 | } 26 | 27 | dependencies { 28 | compile project(':scissors2') 29 | compile 'com.android.support:appcompat-v7:' + rootProject.ext.supportVersion 30 | compile 'com.android.support:design:' + rootProject.ext.supportVersion 31 | compile 'com.jakewharton:butterknife:7.0.1' 32 | compile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 33 | compile 'com.squareup.picasso:picasso:2.5.2' 34 | // Or Glide 35 | // compile 'com.github.bumptech.glide:glide:3.6.1' 36 | // Or Android-Universal-Image-Loader 37 | // compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4' 38 | compile 'io.reactivex:rxjava:1.0.15' 39 | compile 'io.reactivex:rxandroid:1.0.1' 40 | } -------------------------------------------------------------------------------- /scissors-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /scissors-sample/src/main/java/com/lyft/android/scissorssample/App.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissorssample; 17 | 18 | import android.app.Application; 19 | import android.content.Context; 20 | import android.os.StrictMode; 21 | import com.squareup.leakcanary.LeakCanary; 22 | import com.squareup.leakcanary.RefWatcher; 23 | 24 | public class App extends Application { 25 | 26 | public static RefWatcher getRefWatcher(Context context) { 27 | App application = (App) context.getApplicationContext(); 28 | return application.refWatcher; 29 | } 30 | 31 | private RefWatcher refWatcher; 32 | 33 | @Override 34 | public void onCreate() { 35 | super.onCreate(); 36 | refWatcher = LeakCanary.install(this); 37 | 38 | StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 39 | .detectDiskReads() 40 | .detectDiskWrites() 41 | .detectNetwork() // or .detectAll() for all detectable problems 42 | .penaltyLog() 43 | .build()); 44 | StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 45 | .detectLeakedSqlLiteObjects() 46 | .detectLeakedClosableObjects() 47 | .penaltyLog() 48 | .penaltyDeath() 49 | .build()); 50 | 51 | // If using Android-Universal-Image-Loader 52 | // Create global configuration and initialize ImageLoader with default config 53 | // ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build(); 54 | // ImageLoader.getInstance().init(config); 55 | } 56 | } -------------------------------------------------------------------------------- /scissors-sample/src/main/java/com/lyft/android/scissorssample/CropResultActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissorssample; 17 | 18 | import android.app.Activity; 19 | import android.content.Intent; 20 | import android.os.Bundle; 21 | import android.widget.ImageView; 22 | import butterknife.Bind; 23 | import butterknife.ButterKnife; 24 | import com.squareup.picasso.MemoryPolicy; 25 | import com.squareup.picasso.Picasso; 26 | import java.io.File; 27 | 28 | public class CropResultActivity extends Activity { 29 | 30 | private static final String EXTRA_FILE_PATH = "EXTRA_FILE_PATH"; 31 | 32 | @Bind(R.id.result_image) 33 | ImageView resultView; 34 | 35 | @Override 36 | protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | 39 | setContentView(R.layout.activity_crop_result); 40 | ButterKnife.bind(this); 41 | 42 | String filePath = getIntent().getStringExtra(EXTRA_FILE_PATH); 43 | File imageFile = new File(filePath); 44 | 45 | Picasso.with(this) 46 | .load(imageFile) 47 | .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) 48 | .into(resultView); 49 | 50 | // Or Glide 51 | //Glide.with(this) 52 | // .load(imageFile) 53 | // .diskCacheStrategy(DiskCacheStrategy.NONE) 54 | // .skipMemoryCache(true) 55 | // .into(resultView); 56 | 57 | // Or Android-Universal-Image-Loader 58 | //DisplayImageOptions options = new DisplayImageOptions.Builder() 59 | // .cacheInMemory(false) 60 | // .cacheOnDisk(false) 61 | // .build(); 62 | //ImageLoader.getInstance().displayImage("file://" + filePath, resultView, options); 63 | } 64 | 65 | static void startUsing(File croppedPath, Activity activity) { 66 | Intent intent = new Intent(activity, CropResultActivity.class); 67 | intent.putExtra(EXTRA_FILE_PATH, croppedPath.getPath()); 68 | activity.startActivity(intent); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /scissors-sample/src/main/java/com/lyft/android/scissorssample/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissorssample; 17 | 18 | import android.animation.Animator; 19 | import android.animation.Animator.AnimatorListener; 20 | import android.animation.ObjectAnimator; 21 | import android.annotation.TargetApi; 22 | import android.app.Activity; 23 | import android.content.Intent; 24 | import android.content.pm.ActivityInfo; 25 | import android.net.Uri; 26 | import android.os.Build; 27 | import android.os.Bundle; 28 | import android.view.MotionEvent; 29 | import android.view.View; 30 | import android.widget.Toast; 31 | import butterknife.Bind; 32 | import butterknife.ButterKnife; 33 | import butterknife.OnClick; 34 | import butterknife.OnTouch; 35 | import com.lyft.android.scissors2.CropView; 36 | import com.squareup.leakcanary.RefWatcher; 37 | import java.io.File; 38 | import java.util.List; 39 | import rx.Observable; 40 | import rx.functions.Action1; 41 | import rx.subscriptions.CompositeSubscription; 42 | 43 | import static android.graphics.Bitmap.CompressFormat.JPEG; 44 | import static rx.android.schedulers.AndroidSchedulers.mainThread; 45 | import static rx.schedulers.Schedulers.io; 46 | 47 | public class MainActivity extends Activity { 48 | 49 | private static final float[] ASPECT_RATIOS = { 0f, 1f, 6f/4f, 16f/9f }; 50 | 51 | private static final String[] ASPECT_LABELS = { "\u00D8", "1:1", "6:4", "16:9" }; 52 | 53 | @Bind(R.id.crop_view) 54 | CropView cropView; 55 | 56 | @Bind({ R.id.crop_fab, R.id.pick_mini_fab, R.id.ratio_fab }) 57 | List buttons; 58 | 59 | @Bind(R.id.pick_fab) 60 | View pickButton; 61 | 62 | CompositeSubscription subscriptions = new CompositeSubscription(); 63 | 64 | private int selectedRatio = 0; 65 | private AnimatorListener animatorListener = new AnimatorListener() { 66 | @Override 67 | public void onAnimationStart(Animator animation) { 68 | ButterKnife.apply(buttons, VISIBILITY, View.INVISIBLE); 69 | } 70 | 71 | @Override 72 | public void onAnimationEnd(Animator animation) { 73 | ButterKnife.apply(buttons, VISIBILITY, View.VISIBLE); 74 | } 75 | 76 | @Override 77 | public void onAnimationCancel(Animator animation) { 78 | } 79 | 80 | @Override 81 | public void onAnimationRepeat(Animator animation) { 82 | } 83 | }; 84 | 85 | @Override 86 | protected void onCreate(Bundle savedInstanceState) { 87 | super.onCreate(savedInstanceState); 88 | 89 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 90 | setContentView(R.layout.activity_main); 91 | 92 | ButterKnife.bind(this); 93 | } 94 | 95 | @Override 96 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 97 | super.onActivityResult(requestCode, resultCode, data); 98 | 99 | if (requestCode == RequestCodes.PICK_IMAGE_FROM_GALLERY 100 | && resultCode == Activity.RESULT_OK) { 101 | Uri galleryPictureUri = data.getData(); 102 | 103 | cropView.extensions() 104 | .load(galleryPictureUri); 105 | 106 | updateButtons(); 107 | } 108 | } 109 | 110 | @OnClick(R.id.crop_fab) 111 | public void onCropClicked() { 112 | final File croppedFile = new File(getCacheDir(), "cropped.jpg"); 113 | 114 | Observable onSave = Observable.from(cropView.extensions() 115 | .crop() 116 | .quality(100) 117 | .format(JPEG) 118 | .into(croppedFile)) 119 | .subscribeOn(io()) 120 | .observeOn(mainThread()); 121 | 122 | subscriptions.add(onSave 123 | .subscribe(new Action1() { 124 | @Override 125 | public void call(Void nothing) { 126 | CropResultActivity.startUsing(croppedFile, MainActivity.this); 127 | } 128 | })); 129 | } 130 | 131 | @OnClick({ R.id.pick_fab, R.id.pick_mini_fab }) 132 | public void onPickClicked() { 133 | cropView.extensions() 134 | .pickUsing(this, RequestCodes.PICK_IMAGE_FROM_GALLERY); 135 | } 136 | 137 | @OnClick(R.id.ratio_fab) 138 | public void onRatioClicked() { 139 | final float oldRatio = cropView.getImageRatio(); 140 | selectedRatio = (selectedRatio + 1) % ASPECT_RATIOS.length; 141 | 142 | // Since the animation needs to interpolate to the native 143 | // ratio, we need to get that instead of using 0 144 | float newRatio = ASPECT_RATIOS[selectedRatio]; 145 | if (Float.compare(0, newRatio) == 0) { 146 | newRatio = cropView.getImageRatio(); 147 | } 148 | 149 | ObjectAnimator viewportRatioAnimator = ObjectAnimator.ofFloat(cropView, "viewportRatio", oldRatio, newRatio) 150 | .setDuration(420); 151 | autoCancel(viewportRatioAnimator); 152 | viewportRatioAnimator.addListener(animatorListener); 153 | viewportRatioAnimator.start(); 154 | 155 | Toast.makeText(this, ASPECT_LABELS[selectedRatio], Toast.LENGTH_SHORT).show(); 156 | } 157 | 158 | @Override 159 | protected void onDestroy() { 160 | super.onDestroy(); 161 | 162 | subscriptions.unsubscribe(); 163 | 164 | RefWatcher refWatcher = App.getRefWatcher(this); 165 | refWatcher.watch(this, "MainActivity"); 166 | refWatcher.watch(cropView, "cropView"); 167 | } 168 | 169 | @OnTouch(R.id.crop_view) 170 | public boolean onTouchCropView(MotionEvent event) { // GitHub issue #4 171 | if (event.getPointerCount() > 1 || cropView.getImageBitmap() == null) { 172 | return true; 173 | } 174 | 175 | switch (event.getActionMasked()) { 176 | case MotionEvent.ACTION_DOWN: 177 | case MotionEvent.ACTION_MOVE: 178 | ButterKnife.apply(buttons, VISIBILITY, View.INVISIBLE); 179 | break; 180 | default: 181 | ButterKnife.apply(buttons, VISIBILITY, View.VISIBLE); 182 | break; 183 | } 184 | return true; 185 | } 186 | 187 | private void updateButtons() { 188 | ButterKnife.apply(buttons, VISIBILITY, View.VISIBLE); 189 | pickButton.setVisibility(View.GONE); 190 | } 191 | 192 | static final ButterKnife.Setter VISIBILITY = new ButterKnife.Setter() { 193 | @Override 194 | public void set(final View view, final Integer visibility, int index) { 195 | view.setVisibility(visibility); 196 | } 197 | }; 198 | 199 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 200 | static void autoCancel(ObjectAnimator objectAnimator) { 201 | if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { 202 | objectAnimator.setAutoCancel(true); 203 | } 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /scissors-sample/src/main/java/com/lyft/android/scissorssample/RequestCodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissorssample; 17 | 18 | public interface RequestCodes { 19 | 20 | int PICK_IMAGE_FROM_GALLERY = 10001; 21 | } 22 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-hdpi/ic_aspect_ratio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-hdpi/ic_aspect_ratio.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-hdpi/ic_content_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-hdpi/ic_content_add.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-hdpi/ic_content_content_cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-hdpi/ic_content_content_cut.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-mdpi/ic_aspect_ratio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-mdpi/ic_aspect_ratio.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-mdpi/ic_content_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-mdpi/ic_content_add.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-mdpi/ic_content_content_cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-mdpi/ic_content_content_cut.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xhdpi/ic_aspect_ratio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xhdpi/ic_aspect_ratio.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xhdpi/ic_content_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xhdpi/ic_content_add.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xhdpi/ic_content_content_cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xhdpi/ic_content_content_cut.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xxhdpi/ic_aspect_ratio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xxhdpi/ic_aspect_ratio.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xxhdpi/ic_content_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xxhdpi/ic_content_add.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xxhdpi/ic_content_content_cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xxhdpi/ic_content_content_cut.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xxxhdpi/ic_aspect_ratio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xxxhdpi/ic_aspect_ratio.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xxxhdpi/ic_content_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xxxhdpi/ic_content_add.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/drawable-xxxhdpi/ic_content_content_cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/drawable-xxxhdpi/ic_content_content_cut.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/layout/activity_crop_result.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 24 | 25 | 35 | 36 | 44 | 45 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyft/scissors/eeb0f8688c5d04bc92a49dcded9aa35a86d4e409/scissors-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values-v21/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 24dp 4 | 24dp 5 | 32dp 6 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values-v23/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 32dp 4 | 32dp 5 | 40dp 6 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ffe91e63 4 | #ffc2185b 5 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 0dp 4 | 0dp 5 | 8dp 6 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Scissors Sample 4 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | -------------------------------------------------------------------------------- /scissors-sample/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | -------------------------------------------------------------------------------- /scissors2/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion rootProject.ext.compileSdkVersion 5 | buildToolsVersion rootProject.ext.buildToolsVersion 6 | 7 | defaultConfig { 8 | minSdkVersion rootProject.ext.minSdkVersion 9 | consumerProguardFiles 'proguard-rules.txt' 10 | } 11 | 12 | compileOptions { 13 | sourceCompatibility JavaVersion.VERSION_1_7 14 | targetCompatibility JavaVersion.VERSION_1_7 15 | } 16 | 17 | dexOptions { 18 | preDexLibraries = !rootProject.ext.ci 19 | } 20 | } 21 | 22 | dependencies { 23 | // Test 24 | testCompile 'junit:junit:' + rootProject.ext.junitVersion 25 | testCompile 'org.mockito:mockito-core:' + rootProject.ext.mockitoVersion 26 | testCompile 'org.robolectric:robolectric:' + rootProject.ext.robolectricVersion 27 | testCompile 'org.assertj:assertj-core:' + rootProject.ext.assertjVersion 28 | // Support Annotations 29 | compile 'com.android.support:support-annotations:' + rootProject.ext.supportVersion 30 | // Optional dependencies for extensions 31 | // Picasso 32 | provided 'com.squareup.picasso:picasso:[2.4.0, 2.5.2)' 33 | // Glide 34 | provided 'com.github.bumptech.glide:glide:4.1.1' 35 | provided 'com.nostra13.universalimageloader:universal-image-loader:[1.9.2, 1.9.5)' 36 | provided 'com.android.support:support-v4:' + rootProject.ext.supportVersion 37 | } 38 | 39 | apply from: rootProject.file('gradle/gradle-mvn-push.gradle') 40 | -------------------------------------------------------------------------------- /scissors2/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_ARTIFACT_ID=scissors2 2 | POM_NAME=Scissors Library 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /scissors2/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | # Picasso 2 | -dontwarn com.squareup.picasso.** 3 | -keepnames class com.squareup.picasso.Picasso 4 | 5 | # Glide 6 | -dontwarn com.bumptech.glide.** 7 | -keepnames class com.bumptech.glide.Glide 8 | 9 | # Android-Universal-Image-Loader 10 | -dontwarn com.nostra13.universalimageloader.core.** 11 | -keepnames class com.nostra13.universalimageloader.core.ImageLoader 12 | -------------------------------------------------------------------------------- /scissors2/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/BitmapLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.support.annotation.NonNull; 19 | import android.support.annotation.Nullable; 20 | import android.widget.ImageView; 21 | 22 | /** 23 | * Load extension delegates actual Bitmap loading to a BitmapLoader allowing it to use different implementations. 24 | * 25 | * @see PicassoBitmapLoader 26 | * @see GlideBitmapLoader 27 | */ 28 | public interface BitmapLoader { 29 | 30 | void load(@Nullable Object model, @NonNull ImageView view); 31 | } 32 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/CropRequest.java: -------------------------------------------------------------------------------- 1 | package com.lyft.android.scissors2; 2 | 3 | import android.graphics.Bitmap; 4 | import android.support.annotation.NonNull; 5 | 6 | import java.io.File; 7 | import java.io.OutputStream; 8 | import java.util.concurrent.Future; 9 | 10 | public class CropRequest { 11 | 12 | private final CropView cropView; 13 | private Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG; 14 | private int quality = CropViewConfig.DEFAULT_IMAGE_QUALITY; 15 | 16 | CropRequest(@NonNull CropView cropView) { 17 | Utils.checkNotNull(cropView, "cropView == null"); 18 | this.cropView = cropView; 19 | } 20 | 21 | /** 22 | * Compression format to use, defaults to {@link Bitmap.CompressFormat#JPEG}. 23 | * 24 | * @return current request for chaining. 25 | */ 26 | public CropRequest format(@NonNull Bitmap.CompressFormat format) { 27 | Utils.checkNotNull(format, "format == null"); 28 | this.format = format; 29 | return this; 30 | } 31 | 32 | /** 33 | * Compression quality to use (must be 0..100), defaults to {@value CropViewConfig#DEFAULT_IMAGE_QUALITY}. 34 | * 35 | * @return current request for chaining. 36 | */ 37 | public CropRequest quality(int quality) { 38 | Utils.checkArg(quality >= 0 && quality <= 100, "quality must be 0..100"); 39 | this.quality = quality; 40 | return this; 41 | } 42 | 43 | /** 44 | * Asynchronously flush cropped bitmap into provided file, creating parent directory if required. This is performed in another 45 | * thread. 46 | * 47 | * @param file Must have permissions to write, will be created if doesn't exist or overwrite if it does. 48 | * @return {@link Future} used to cancel or wait for this request. 49 | */ 50 | public Future into(@NonNull File file) { 51 | final Bitmap croppedBitmap = cropView.crop(); 52 | return Utils.flushToFile(croppedBitmap, format, quality, file); 53 | } 54 | 55 | /** 56 | * Asynchronously flush cropped bitmap into provided stream. 57 | * 58 | * @param outputStream Stream to write to 59 | * @param closeWhenDone wetter or not to close provided stream once flushing is done 60 | * @return {@link Future} used to cancel or wait for this request. 61 | */ 62 | public Future into(@NonNull OutputStream outputStream, boolean closeWhenDone) { 63 | final Bitmap croppedBitmap = cropView.crop(); 64 | return Utils.flushToStream(croppedBitmap, format, quality, outputStream, closeWhenDone); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/CropView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.app.Activity; 19 | import android.app.Fragment; 20 | import android.content.Context; 21 | import android.content.Intent; 22 | import android.graphics.Bitmap; 23 | import android.graphics.BitmapFactory; 24 | import android.graphics.Canvas; 25 | import android.graphics.Matrix; 26 | import android.graphics.Paint; 27 | import android.graphics.Path; 28 | import android.graphics.RectF; 29 | import android.graphics.drawable.BitmapDrawable; 30 | import android.graphics.drawable.Drawable; 31 | import android.net.Uri; 32 | import android.support.annotation.ColorInt; 33 | import android.support.annotation.DrawableRes; 34 | import android.support.annotation.IntDef; 35 | import android.support.annotation.NonNull; 36 | import android.support.annotation.Nullable; 37 | import android.util.AttributeSet; 38 | import android.view.MotionEvent; 39 | import android.widget.ImageView; 40 | import java.io.File; 41 | import java.io.OutputStream; 42 | import java.lang.annotation.Retention; 43 | import java.lang.annotation.RetentionPolicy; 44 | 45 | /** 46 | * An {@link ImageView} with a fixed viewport and cropping capabilities. 47 | */ 48 | public class CropView extends ImageView { 49 | 50 | private TouchManager touchManager; 51 | private CropViewConfig config; 52 | 53 | private Paint viewportPaint = new Paint(); 54 | private Paint bitmapPaint = new Paint(); 55 | 56 | private Bitmap bitmap; 57 | private Matrix transform = new Matrix(); 58 | private Extensions extensions; 59 | 60 | /** Corresponds to the values in {@link com.lyft.android.scissors2.R.attr#cropviewShape} */ 61 | @Retention(RetentionPolicy.SOURCE) 62 | @IntDef({ Shape.RECTANGLE, Shape.OVAL }) 63 | public @interface Shape { 64 | 65 | int RECTANGLE = 0; 66 | int OVAL = 1; 67 | } 68 | 69 | @Shape 70 | private int shape = Shape.RECTANGLE; 71 | private Path ovalPath; 72 | private RectF ovalRect; 73 | 74 | public CropView(Context context) { 75 | super(context); 76 | initCropView(context, null); 77 | } 78 | 79 | public CropView(Context context, AttributeSet attrs) { 80 | super(context, attrs); 81 | 82 | initCropView(context, attrs); 83 | } 84 | 85 | void initCropView(Context context, AttributeSet attrs) { 86 | config = CropViewConfig.from(context, attrs); 87 | 88 | touchManager = new TouchManager(this, config); 89 | 90 | bitmapPaint.setFilterBitmap(true); 91 | setViewportOverlayColor(config.getViewportOverlayColor()); 92 | shape = config.shape(); 93 | 94 | // We need anti-aliased Paint to smooth the curved edges 95 | viewportPaint.setFlags(viewportPaint.getFlags() | Paint.ANTI_ALIAS_FLAG); 96 | } 97 | 98 | @Override 99 | protected void onDraw(Canvas canvas) { 100 | super.onDraw(canvas); 101 | 102 | if (bitmap == null) { 103 | return; 104 | } 105 | 106 | drawBitmap(canvas); 107 | if (shape == Shape.RECTANGLE) { 108 | drawSquareOverlay(canvas); 109 | } else { 110 | drawOvalOverlay(canvas); 111 | } 112 | } 113 | 114 | private void drawBitmap(Canvas canvas) { 115 | transform.reset(); 116 | touchManager.applyPositioningAndScale(transform); 117 | 118 | canvas.drawBitmap(bitmap, transform, bitmapPaint); 119 | } 120 | 121 | private void drawSquareOverlay(Canvas canvas) { 122 | final int viewportWidth = touchManager.getViewportWidth(); 123 | final int viewportHeight = touchManager.getViewportHeight(); 124 | final int left = (getWidth() - viewportWidth) / 2; 125 | final int top = (getHeight() - viewportHeight) / 2; 126 | 127 | canvas.drawRect(0, top, left, getHeight() - top, viewportPaint); // left 128 | canvas.drawRect(0, 0, getWidth(), top, viewportPaint); // top 129 | canvas.drawRect(getWidth() - left, top, getWidth(), getHeight() - top, viewportPaint); // right 130 | canvas.drawRect(0, getHeight() - top, getWidth(), getHeight(), viewportPaint); // bottom 131 | } 132 | 133 | private void drawOvalOverlay(Canvas canvas) { 134 | if (ovalRect == null) { 135 | ovalRect = new RectF(); 136 | } 137 | if (ovalPath == null) { 138 | ovalPath = new Path(); 139 | } 140 | 141 | final int viewportWidth = touchManager.getViewportWidth(); 142 | final int viewportHeight = touchManager.getViewportHeight(); 143 | final int left = (getWidth() - viewportWidth) / 2; 144 | final int top = (getHeight() - viewportHeight) / 2; 145 | final int right = getWidth() - left; 146 | final int bottom = getHeight() - top; 147 | ovalRect.left = left; 148 | ovalRect.top = top; 149 | ovalRect.right = right; 150 | ovalRect.bottom = bottom; 151 | 152 | // top left arc 153 | ovalPath.reset(); 154 | ovalPath.moveTo(left, getHeight() / 2); // middle of the left side of the circle 155 | ovalPath.arcTo(ovalRect, 180, 90, false); // draw arc to top 156 | ovalPath.lineTo(left, top); // move to top-left corner 157 | ovalPath.lineTo(left, getHeight() / 2); // move back to origin 158 | ovalPath.close(); 159 | canvas.drawPath(ovalPath, viewportPaint); 160 | 161 | // top right arc 162 | ovalPath.reset(); 163 | ovalPath.moveTo(getWidth() / 2, top); // middle of the top side of the circle 164 | ovalPath.arcTo(ovalRect, 270, 90, false); // draw arc to the right 165 | ovalPath.lineTo(right, top); // move to top-right corner 166 | ovalPath.lineTo(getWidth() / 2, top); // move back to origin 167 | ovalPath.close(); 168 | canvas.drawPath(ovalPath, viewportPaint); 169 | 170 | // bottom right arc 171 | ovalPath.reset(); 172 | ovalPath.moveTo(right, getHeight() / 2); // middle of the right side of the circle 173 | ovalPath.arcTo(ovalRect, 0, 90, false); // draw arc to the bottom 174 | ovalPath.lineTo(right, bottom); // move to bottom-right corner 175 | ovalPath.lineTo(right, getHeight() / 2); // move back to origin 176 | ovalPath.close(); 177 | canvas.drawPath(ovalPath, viewportPaint); 178 | 179 | // bottom left arc 180 | ovalPath.reset(); 181 | ovalPath.moveTo(getWidth() / 2, bottom); // middle of the bottom side of the circle 182 | ovalPath.arcTo(ovalRect, 90, 90, false); // draw arc to the left 183 | ovalPath.lineTo(left, bottom); // move to bottom-left corner 184 | ovalPath.lineTo(getWidth() / 2, bottom); // move back to origin 185 | ovalPath.close(); 186 | canvas.drawPath(ovalPath, viewportPaint); 187 | 188 | // Draw the square overlay as well 189 | drawSquareOverlay(canvas); 190 | } 191 | 192 | @Override 193 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 194 | super.onSizeChanged(w, h, oldw, oldh); 195 | resetTouchManager(); 196 | } 197 | 198 | /** 199 | * Sets the color of the viewport overlay 200 | * 201 | * @param viewportOverlayColor The color to use for the viewport overlay 202 | */ 203 | public void setViewportOverlayColor(@ColorInt int viewportOverlayColor) { 204 | viewportPaint.setColor(viewportOverlayColor); 205 | config.setViewportOverlayColor(viewportOverlayColor); 206 | } 207 | 208 | /** 209 | * Sets the padding for the viewport overlay 210 | * 211 | * @param viewportOverlayPadding The new padding of the viewport overlay 212 | */ 213 | public void setViewportOverlayPadding(int viewportOverlayPadding) { 214 | config.setViewportOverlayPadding(viewportOverlayPadding); 215 | resetTouchManager(); 216 | invalidate(); 217 | } 218 | 219 | /** 220 | * Returns the native aspect ratio of the image. 221 | * 222 | * @return The native aspect ratio of the image. 223 | */ 224 | public float getImageRatio() { 225 | Bitmap bitmap = getImageBitmap(); 226 | return bitmap != null ? (float) bitmap.getWidth() / (float) bitmap.getHeight() : 0f; 227 | } 228 | 229 | /** 230 | * Returns the aspect ratio of the viewport and crop rect. 231 | * 232 | * @return The current viewport aspect ratio. 233 | */ 234 | public float getViewportRatio() { 235 | return touchManager.getAspectRatio(); 236 | } 237 | 238 | /** 239 | * Sets the aspect ratio of the viewport and crop rect. Defaults to 240 | * the native aspect ratio if ratio == 0. 241 | * 242 | * @param ratio The new aspect ratio of the viewport. 243 | */ 244 | public void setViewportRatio(float ratio) { 245 | if (Float.compare(ratio, 0) == 0) { 246 | ratio = getImageRatio(); 247 | } 248 | touchManager.setAspectRatio(ratio); 249 | resetTouchManager(); 250 | invalidate(); 251 | } 252 | 253 | @Override 254 | public void setImageResource(@DrawableRes int resId) { 255 | final Bitmap bitmap = resId > 0 256 | ? BitmapFactory.decodeResource(getResources(), resId) 257 | : null; 258 | setImageBitmap(bitmap); 259 | } 260 | 261 | @Override 262 | public void setImageDrawable(@Nullable Drawable drawable) { 263 | final Bitmap bitmap; 264 | if (drawable instanceof BitmapDrawable) { 265 | BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; 266 | bitmap = bitmapDrawable.getBitmap(); 267 | } else if (drawable != null) { 268 | bitmap = Utils.asBitmap(drawable, getWidth(), getHeight()); 269 | } else { 270 | bitmap = null; 271 | } 272 | 273 | setImageBitmap(bitmap); 274 | } 275 | 276 | @Override 277 | public void setImageURI(@Nullable Uri uri) { 278 | extensions().load(uri); 279 | } 280 | 281 | @Override 282 | public void setImageBitmap(@Nullable Bitmap bitmap) { 283 | this.bitmap = bitmap; 284 | resetTouchManager(); 285 | invalidate(); 286 | } 287 | 288 | /** 289 | * @return Current working Bitmap or null if none has been set yet. 290 | */ 291 | @Nullable 292 | public Bitmap getImageBitmap() { 293 | return bitmap; 294 | } 295 | 296 | private void resetTouchManager() { 297 | final boolean invalidBitmap = bitmap == null; 298 | final int bitmapWidth = invalidBitmap ? 0 : bitmap.getWidth(); 299 | final int bitmapHeight = invalidBitmap ? 0 : bitmap.getHeight(); 300 | touchManager.resetFor(bitmapWidth, bitmapHeight, getWidth(), getHeight()); 301 | } 302 | 303 | @Override 304 | public boolean dispatchTouchEvent(MotionEvent event) { 305 | boolean result = super.dispatchTouchEvent(event); 306 | 307 | if (!isEnabled()) { 308 | return result; 309 | } 310 | 311 | touchManager.onEvent(event); 312 | invalidate(); 313 | return true; 314 | } 315 | 316 | /** 317 | * Performs synchronous image cropping based on configuration. 318 | * 319 | * @return A {@link Bitmap} cropped based on viewport and user panning and zooming or null if no {@link Bitmap} has been 320 | * provided. 321 | */ 322 | @Nullable 323 | public Bitmap crop() { 324 | if (bitmap == null) { 325 | return null; 326 | } 327 | 328 | final Bitmap src = bitmap; 329 | final Bitmap.Config srcConfig = src.getConfig(); 330 | final Bitmap.Config config = srcConfig == null ? Bitmap.Config.ARGB_8888 : srcConfig; 331 | final int viewportHeight = touchManager.getViewportHeight(); 332 | final int viewportWidth = touchManager.getViewportWidth(); 333 | 334 | final Bitmap dst = Bitmap.createBitmap(viewportWidth, viewportHeight, config); 335 | 336 | Canvas canvas = new Canvas(dst); 337 | final int left = (getRight() - viewportWidth) / 2; 338 | final int top = (getBottom() - viewportHeight) / 2; 339 | canvas.translate(-left, -top); 340 | 341 | drawBitmap(canvas); 342 | 343 | return dst; 344 | } 345 | 346 | /** 347 | * Obtain current viewport width. 348 | * 349 | * @return Current viewport width. 350 | *

Note: It might be 0 if layout pass has not been completed.

351 | */ 352 | public int getViewportWidth() { 353 | return touchManager.getViewportWidth(); 354 | } 355 | 356 | /** 357 | * Obtain current viewport height. 358 | * 359 | * @return Current viewport height. 360 | *

Note: It might be 0 if layout pass has not been completed.

361 | */ 362 | public int getViewportHeight() { 363 | return touchManager.getViewportHeight(); 364 | } 365 | 366 | /** 367 | * Offers common utility extensions. 368 | * 369 | * @return Extensions object used to perform chained calls. 370 | */ 371 | public Extensions extensions() { 372 | if (extensions == null) { 373 | extensions = new Extensions(this); 374 | } 375 | return extensions; 376 | } 377 | 378 | /** 379 | * Get the transform matrix 380 | */ 381 | public Matrix getTransformMatrix() { 382 | return transform; 383 | } 384 | 385 | /** 386 | * Optional extensions to perform common actions involving a {@link CropView} 387 | */ 388 | public static class Extensions { 389 | 390 | private final CropView cropView; 391 | 392 | Extensions(CropView cropView) { 393 | this.cropView = cropView; 394 | } 395 | 396 | /** 397 | * Load a {@link Bitmap} using an automatically resolved {@link BitmapLoader} which will attempt to scale image to fill view. 398 | * 399 | * @param model Model used by {@link BitmapLoader} to load desired {@link Bitmap} 400 | * @see PicassoBitmapLoader 401 | * @see GlideBitmapLoader 402 | */ 403 | public void load(@Nullable Object model) { 404 | new LoadRequest(cropView) 405 | .load(model); 406 | } 407 | 408 | /** 409 | * Load a {@link Bitmap} using given {@link BitmapLoader}, you must call {@link LoadRequest#load(Object)} afterwards. 410 | * 411 | * @param bitmapLoader {@link BitmapLoader} used to load desired {@link Bitmap} 412 | * @see PicassoBitmapLoader 413 | * @see GlideBitmapLoader 414 | */ 415 | public LoadRequest using(@Nullable BitmapLoader bitmapLoader) { 416 | return new LoadRequest(cropView).using(bitmapLoader); 417 | } 418 | 419 | public enum LoaderType { 420 | PICASSO, 421 | GLIDE, 422 | UIL, 423 | CLASS_LOOKUP 424 | } 425 | 426 | /** 427 | * Load a {@link Bitmap} using a reference to a {@link BitmapLoader}, you must call {@link LoadRequest#load(Object)} afterwards. 428 | * 429 | * Please ensure that the library for the {@link BitmapLoader} you reference is available on the classpath. 430 | * 431 | * @param loaderType the {@link BitmapLoader} to use to load desired (@link Bitmap} 432 | * @see PicassoBitmapLoader 433 | * @see GlideBitmapLoader 434 | */ 435 | public LoadRequest using(@NonNull LoaderType loaderType) { 436 | return new LoadRequest(cropView).using(loaderType); 437 | } 438 | 439 | /** 440 | * Perform an asynchronous crop request. 441 | * 442 | * @return {@link CropRequest} used to chain a configure cropping request, you must call either one of: 443 | *
    444 | *
  • {@link CropRequest#into(File)}
  • 445 | *
  • {@link CropRequest#into(OutputStream, boolean)}
  • 446 | *
447 | */ 448 | public CropRequest crop() { 449 | return new CropRequest(cropView); 450 | } 451 | 452 | /** 453 | * Perform a pick image request using {@link Activity#startActivityForResult(Intent, int)}. 454 | */ 455 | public void pickUsing(@NonNull Activity activity, int requestCode) { 456 | CropViewExtensions.pickUsing(activity, requestCode); 457 | } 458 | 459 | /** 460 | * Perform a pick image request using {@link Fragment#startActivityForResult(Intent, int)}. 461 | */ 462 | public void pickUsing(@NonNull Fragment fragment, int requestCode) { 463 | CropViewExtensions.pickUsing(fragment, requestCode); 464 | } 465 | } 466 | } 467 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/CropViewConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.content.Context; 19 | import android.content.res.TypedArray; 20 | import android.util.AttributeSet; 21 | 22 | class CropViewConfig { 23 | 24 | public static final float DEFAULT_VIEWPORT_RATIO = 0f; 25 | public static final float DEFAULT_MAXIMUM_SCALE = 10f; 26 | public static final float DEFAULT_MINIMUM_SCALE = 0f; 27 | public static final int DEFAULT_IMAGE_QUALITY = 100; 28 | public static final int DEFAULT_VIEWPORT_OVERLAY_PADDING = 0; 29 | public static final int DEFAULT_VIEWPORT_OVERLAY_COLOR = 0xC8000000; // Black with 200 alpha 30 | public static final int DEFAULT_SHAPE = CropView.Shape.RECTANGLE; 31 | 32 | private float viewportRatio = DEFAULT_VIEWPORT_RATIO; 33 | private float maxScale = DEFAULT_MAXIMUM_SCALE; 34 | private float minScale = DEFAULT_MINIMUM_SCALE; 35 | private int viewportOverlayPadding = DEFAULT_VIEWPORT_OVERLAY_PADDING; 36 | private int viewportOverlayColor = DEFAULT_VIEWPORT_OVERLAY_COLOR; 37 | private @CropView.Shape int shape = DEFAULT_SHAPE; 38 | 39 | public int getViewportOverlayColor() { 40 | return viewportOverlayColor; 41 | } 42 | 43 | void setViewportOverlayColor(int viewportOverlayColor) { 44 | this.viewportOverlayColor = viewportOverlayColor; 45 | } 46 | 47 | public int getViewportOverlayPadding() { 48 | return viewportOverlayPadding; 49 | } 50 | 51 | void setViewportOverlayPadding(int viewportOverlayPadding) { 52 | this.viewportOverlayPadding = viewportOverlayPadding; 53 | } 54 | 55 | public float getViewportRatio() { 56 | return viewportRatio; 57 | } 58 | 59 | void setViewportRatio(float viewportRatio) { 60 | this.viewportRatio = viewportRatio <= 0 ? DEFAULT_VIEWPORT_RATIO : viewportRatio; 61 | } 62 | 63 | public float getMaxScale() { 64 | return maxScale; 65 | } 66 | 67 | void setMaxScale(float maxScale) { 68 | this.maxScale = maxScale <= 0 ? DEFAULT_MAXIMUM_SCALE : maxScale; 69 | } 70 | 71 | public float getMinScale() { 72 | return minScale; 73 | } 74 | 75 | void setMinScale(float minScale) { 76 | this.minScale = minScale <= 0 ? DEFAULT_MINIMUM_SCALE : minScale; 77 | } 78 | 79 | public @CropView.Shape int shape() { 80 | return shape; 81 | } 82 | 83 | public void setShape(@CropView.Shape int shape) { 84 | this.shape = shape; 85 | } 86 | 87 | public static CropViewConfig from(Context context, AttributeSet attrs) { 88 | final CropViewConfig cropViewConfig = new CropViewConfig(); 89 | 90 | if (attrs == null) { 91 | return cropViewConfig; 92 | } 93 | 94 | TypedArray attributes = context.obtainStyledAttributes( 95 | attrs, 96 | R.styleable.CropView); 97 | 98 | cropViewConfig.setViewportRatio( 99 | attributes.getFloat(R.styleable.CropView_cropviewViewportRatio, 100 | CropViewConfig.DEFAULT_VIEWPORT_RATIO)); 101 | 102 | cropViewConfig.setMaxScale( 103 | attributes.getFloat(R.styleable.CropView_cropviewMaxScale, 104 | CropViewConfig.DEFAULT_MAXIMUM_SCALE)); 105 | 106 | cropViewConfig.setMinScale( 107 | attributes.getFloat(R.styleable.CropView_cropviewMinScale, 108 | CropViewConfig.DEFAULT_MINIMUM_SCALE)); 109 | 110 | cropViewConfig.setViewportOverlayColor( 111 | attributes.getColor(R.styleable.CropView_cropviewViewportOverlayColor, 112 | CropViewConfig.DEFAULT_VIEWPORT_OVERLAY_COLOR)); 113 | 114 | cropViewConfig.setViewportOverlayPadding( 115 | attributes.getDimensionPixelSize(R.styleable.CropView_cropviewViewportOverlayPadding, 116 | CropViewConfig.DEFAULT_VIEWPORT_OVERLAY_PADDING)); 117 | 118 | @CropView.Shape int shape = attributes.getInt( 119 | R.styleable.CropView_cropviewShape, CropViewConfig.DEFAULT_SHAPE); 120 | cropViewConfig.setShape(shape); 121 | 122 | attributes.recycle(); 123 | 124 | return cropViewConfig; 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/CropViewExtensions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.app.Activity; 19 | import android.app.Fragment; 20 | import android.content.Intent; 21 | import android.graphics.Rect; 22 | 23 | import static com.lyft.android.scissors2.CropView.Extensions.LoaderType; 24 | 25 | class CropViewExtensions { 26 | 27 | static void pickUsing(Activity activity, int requestCode) { 28 | activity.startActivityForResult( 29 | createChooserIntent(), 30 | requestCode); 31 | } 32 | 33 | static void pickUsing(Fragment fragment, int requestCode) { 34 | fragment.startActivityForResult( 35 | createChooserIntent(), 36 | requestCode); 37 | } 38 | 39 | private static Intent createChooserIntent() { 40 | Intent intent = new Intent(); 41 | intent.setType("image/*"); 42 | intent.setAction(Intent.ACTION_GET_CONTENT); 43 | intent.addCategory(Intent.CATEGORY_OPENABLE); 44 | 45 | return Intent.createChooser(intent, null); 46 | } 47 | 48 | final static boolean HAS_PICASSO = canHasClass("com.squareup.picasso.Picasso"); 49 | final static boolean HAS_GLIDE = canHasClass("com.bumptech.glide.Glide"); 50 | final static boolean HAS_UIL = canHasClass("com.nostra13.universalimageloader.core.ImageLoader"); 51 | 52 | static BitmapLoader resolveBitmapLoader(CropView cropView, LoaderType loaderType) { 53 | switch (loaderType) { 54 | case PICASSO: 55 | return PicassoBitmapLoader.createUsing(cropView); 56 | case GLIDE: 57 | return GlideBitmapLoader.createUsing(cropView); 58 | case UIL: 59 | return UILBitmapLoader.createUsing(cropView); 60 | case CLASS_LOOKUP: 61 | break; 62 | default: 63 | throw new IllegalStateException("Unsupported type of loader = " + loaderType); 64 | } 65 | 66 | if (HAS_PICASSO) { 67 | return PicassoBitmapLoader.createUsing(cropView); 68 | } 69 | if (HAS_GLIDE) { 70 | return GlideBitmapLoader.createUsing(cropView); 71 | } 72 | if (HAS_UIL) { 73 | return UILBitmapLoader.createUsing(cropView); 74 | } 75 | throw new IllegalStateException("You must provide a BitmapLoader."); 76 | } 77 | 78 | static boolean canHasClass(String className) { 79 | try { 80 | Class.forName(className); 81 | return true; 82 | } catch (ClassNotFoundException e) { 83 | } 84 | return false; 85 | } 86 | 87 | static Rect computeTargetSize(int sourceWidth, int sourceHeight, int viewportWidth, int viewportHeight) { 88 | 89 | if (sourceWidth == viewportWidth && sourceHeight == viewportHeight) { 90 | return new Rect(0, 0, viewportWidth, viewportHeight); // Fail fast for when source matches exactly on viewport 91 | } 92 | 93 | float scale; 94 | if (sourceWidth * viewportHeight > viewportWidth * sourceHeight) { 95 | scale = (float) viewportHeight / (float) sourceHeight; 96 | } else { 97 | scale = (float) viewportWidth / (float) sourceWidth; 98 | } 99 | final int recommendedWidth = (int) ((sourceWidth * scale) + 0.5f); 100 | final int recommendedHeight = (int) ((sourceHeight * scale) + 0.5f); 101 | return new Rect(0, 0, recommendedWidth, recommendedHeight); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/GlideBitmapLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.support.annotation.NonNull; 19 | import android.support.annotation.Nullable; 20 | import android.widget.ImageView; 21 | 22 | import com.bumptech.glide.Glide; 23 | import com.bumptech.glide.RequestManager; 24 | import com.bumptech.glide.load.engine.DiskCacheStrategy; 25 | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; 26 | import com.bumptech.glide.request.RequestOptions; 27 | 28 | /** 29 | * A {@link BitmapLoader} with transformation for {@link Glide} image library. 30 | * 31 | * @see GlideBitmapLoader#createUsing(CropView) 32 | * @see GlideBitmapLoader#createUsing(CropView, RequestManager) 33 | */ 34 | public class GlideBitmapLoader implements BitmapLoader { 35 | 36 | private final RequestManager requestManager; 37 | private final BitmapTransformation transformation; 38 | 39 | public GlideBitmapLoader(@NonNull RequestManager requestManager, @NonNull BitmapTransformation transformation) { 40 | this.requestManager = requestManager; 41 | this.transformation = transformation; 42 | } 43 | 44 | @Override 45 | public void load(@Nullable Object model, @NonNull ImageView imageView) { 46 | RequestOptions requestOptions = new RequestOptions(); 47 | requestOptions.skipMemoryCache(true) 48 | .diskCacheStrategy(DiskCacheStrategy.DATA) 49 | .transform(transformation); 50 | 51 | requestManager.asBitmap() 52 | .load(model) 53 | .apply(requestOptions); 54 | } 55 | 56 | public static BitmapLoader createUsing(@NonNull CropView cropView) { 57 | return createUsing(cropView, Glide.with(cropView.getContext())); 58 | } 59 | 60 | public static BitmapLoader createUsing(@NonNull CropView cropView, @NonNull RequestManager requestManager) { 61 | return new GlideBitmapLoader(requestManager, 62 | GlideFillViewportTransformation.createUsing(cropView.getViewportWidth(), cropView.getViewportHeight())); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/GlideFillViewportTransformation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.graphics.Bitmap; 19 | import android.graphics.Rect; 20 | 21 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 22 | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; 23 | 24 | import java.nio.charset.Charset; 25 | import java.security.MessageDigest; 26 | 27 | class GlideFillViewportTransformation extends BitmapTransformation { 28 | 29 | private static final String ID = "com.lyft.android.scissors.GlideFillViewportTransformation"; 30 | private static final byte[] ID_BYTES = ID.getBytes(Charset.defaultCharset()); 31 | 32 | private final int viewportWidth; 33 | private final int viewportHeight; 34 | 35 | public GlideFillViewportTransformation(int viewportWidth, int viewportHeight) { 36 | this.viewportWidth = viewportWidth; 37 | this.viewportHeight = viewportHeight; 38 | } 39 | 40 | @Override 41 | protected Bitmap transform(BitmapPool bitmapPool, Bitmap source, int outWidth, int outHeight) { 42 | int sourceWidth = source.getWidth(); 43 | int sourceHeight = source.getHeight(); 44 | 45 | Rect target = CropViewExtensions.computeTargetSize(sourceWidth, sourceHeight, viewportWidth, viewportHeight); 46 | 47 | int targetWidth = target.width(); 48 | int targetHeight = target.height(); 49 | 50 | return Bitmap.createScaledBitmap( 51 | source, 52 | targetWidth, 53 | targetHeight, 54 | true); 55 | } 56 | 57 | @Override 58 | public boolean equals(Object obj) { 59 | if (obj instanceof GlideFillViewportTransformation) { 60 | GlideFillViewportTransformation other = (GlideFillViewportTransformation) obj; 61 | return other.viewportWidth == viewportWidth && other.viewportHeight == viewportHeight; 62 | } 63 | return false; 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | int hash = viewportWidth * 31 + viewportHeight; 69 | return hash * 17 + ID.hashCode(); 70 | } 71 | 72 | @Override 73 | public void updateDiskCacheKey(MessageDigest messageDigest) { 74 | messageDigest.update(ID_BYTES); 75 | } 76 | 77 | public static BitmapTransformation createUsing(int viewportWidth, int viewportHeight) { 78 | return new GlideFillViewportTransformation(viewportWidth, viewportHeight); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/LoadRequest.java: -------------------------------------------------------------------------------- 1 | package com.lyft.android.scissors2; 2 | 3 | import android.graphics.Bitmap; 4 | import android.support.annotation.Nullable; 5 | import android.view.ViewTreeObserver; 6 | 7 | import static com.lyft.android.scissors2.CropView.Extensions.LoaderType; 8 | import static com.lyft.android.scissors2.CropViewExtensions.resolveBitmapLoader; 9 | 10 | public class LoadRequest { 11 | 12 | private final CropView cropView; 13 | private BitmapLoader bitmapLoader; 14 | private LoaderType loaderType = LoaderType.CLASS_LOOKUP; 15 | 16 | LoadRequest(CropView cropView) { 17 | Utils.checkNotNull(cropView, "cropView == null"); 18 | this.cropView = cropView; 19 | } 20 | 21 | /** 22 | * Load a {@link Bitmap} using given {@link BitmapLoader}, you must call {@link LoadRequest#load(Object)} afterwards. 23 | * 24 | * @param bitmapLoader {@link BitmapLoader} to use 25 | * @return current request for chaining, you should call {@link #load(Object)} afterwards. 26 | */ 27 | public LoadRequest using(@Nullable BitmapLoader bitmapLoader) { 28 | this.bitmapLoader = bitmapLoader; 29 | return this; 30 | } 31 | 32 | /** 33 | * Load a {@link Bitmap} using the {@link BitmapLoader} specified by {@code loaderType}, you must call {@link 34 | * LoadRequest#load(Object)} afterwards. 35 | * 36 | * @param loaderType a reference to the {@link BitmapLoader} to use 37 | * @return current request for chaining, you should call {@link #load(Object)} afterwards. 38 | */ 39 | public LoadRequest using(LoaderType loaderType) { 40 | this.loaderType = loaderType; 41 | return this; 42 | } 43 | 44 | /** 45 | * Load a {@link Bitmap} using a {@link BitmapLoader} into {@link CropView} 46 | * 47 | * @param model Model used by {@link BitmapLoader} to load desired {@link Bitmap} 48 | */ 49 | public void load(@Nullable Object model) { 50 | if (cropView.getWidth() == 0 && cropView.getHeight() == 0) { 51 | // Defer load until layout pass 52 | deferLoad(model); 53 | return; 54 | } 55 | performLoad(model); 56 | } 57 | 58 | void performLoad(Object model) { 59 | if (bitmapLoader == null) { 60 | bitmapLoader = resolveBitmapLoader(cropView, loaderType); 61 | } 62 | bitmapLoader.load(model, cropView); 63 | } 64 | 65 | void deferLoad(final Object model) { 66 | if (!cropView.getViewTreeObserver().isAlive()) { 67 | return; 68 | } 69 | cropView.getViewTreeObserver().addOnGlobalLayoutListener( 70 | new ViewTreeObserver.OnGlobalLayoutListener() { 71 | @Override 72 | public void onGlobalLayout() { 73 | if (cropView.getViewTreeObserver().isAlive()) { 74 | //noinspection deprecation 75 | cropView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 76 | } 77 | performLoad(model); 78 | } 79 | } 80 | ); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/PicassoBitmapLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.net.Uri; 19 | import android.support.annotation.NonNull; 20 | import android.support.annotation.Nullable; 21 | import android.widget.ImageView; 22 | import com.squareup.picasso.Picasso; 23 | import com.squareup.picasso.RequestCreator; 24 | import com.squareup.picasso.Transformation; 25 | import java.io.File; 26 | 27 | /** 28 | * A {@link BitmapLoader} with transformation for {@link Picasso} image library. 29 | * 30 | * @see PicassoBitmapLoader#createUsing(CropView) 31 | * @see PicassoBitmapLoader#createUsing(CropView, Picasso) 32 | */ 33 | public class PicassoBitmapLoader implements BitmapLoader { 34 | 35 | private final Picasso picasso; 36 | private final Transformation transformation; 37 | 38 | public PicassoBitmapLoader(Picasso picasso, Transformation transformation) { 39 | this.picasso = picasso; 40 | this.transformation = transformation; 41 | } 42 | 43 | @Override 44 | public void load(@Nullable Object model, @NonNull ImageView imageView) { 45 | final RequestCreator requestCreator; 46 | 47 | if (model instanceof Uri || model == null) { 48 | requestCreator = picasso.load((Uri) model); 49 | } else if (model instanceof String) { 50 | requestCreator = picasso.load((String) model); 51 | } else if (model instanceof File) { 52 | requestCreator = picasso.load((File) model); 53 | } else if (model instanceof Integer) { 54 | requestCreator = picasso.load((Integer) model); 55 | } else { 56 | throw new IllegalArgumentException("Unsupported model " + model); 57 | } 58 | 59 | requestCreator 60 | .skipMemoryCache() 61 | .transform(transformation) 62 | .into(imageView); 63 | } 64 | 65 | public static BitmapLoader createUsing(CropView cropView) { 66 | return createUsing(cropView, Picasso.with(cropView.getContext())); 67 | } 68 | 69 | public static BitmapLoader createUsing(CropView cropView, Picasso picasso) { 70 | return new PicassoBitmapLoader(picasso, 71 | PicassoFillViewportTransformation.createUsing(cropView.getViewportWidth(), cropView.getViewportHeight())); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/PicassoFillViewportTransformation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.graphics.Bitmap; 19 | import android.graphics.Rect; 20 | import com.squareup.picasso.Transformation; 21 | 22 | class PicassoFillViewportTransformation implements Transformation { 23 | 24 | private final int viewportWidth; 25 | private final int viewportHeight; 26 | 27 | public PicassoFillViewportTransformation(int viewportWidth, int viewportHeight) { 28 | this.viewportWidth = viewportWidth; 29 | this.viewportHeight = viewportHeight; 30 | } 31 | 32 | @Override 33 | public Bitmap transform(Bitmap source) { 34 | int sourceWidth = source.getWidth(); 35 | int sourceHeight = source.getHeight(); 36 | 37 | Rect target = CropViewExtensions.computeTargetSize(sourceWidth, sourceHeight, viewportWidth, viewportHeight); 38 | final Bitmap result = Bitmap.createScaledBitmap( 39 | source, 40 | target.width(), 41 | target.height(), 42 | true); 43 | 44 | if (result != source) { 45 | source.recycle(); 46 | } 47 | 48 | return result; 49 | } 50 | 51 | @Override 52 | public String key() { 53 | return viewportWidth + "x" + viewportHeight; 54 | } 55 | 56 | public static Transformation createUsing(int viewportWidth, int viewportHeight) { 57 | return new PicassoFillViewportTransformation(viewportWidth, viewportHeight); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/TouchManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.animation.Animator; 19 | import android.animation.AnimatorListenerAdapter; 20 | import android.animation.AnimatorSet; 21 | import android.animation.ValueAnimator; 22 | import android.annotation.TargetApi; 23 | import android.graphics.Matrix; 24 | import android.graphics.Rect; 25 | import android.os.Build; 26 | import android.support.annotation.IntDef; 27 | import android.view.GestureDetector; 28 | import android.view.MotionEvent; 29 | import android.view.ScaleGestureDetector; 30 | import android.view.animation.AccelerateDecelerateInterpolator; 31 | import android.view.animation.DecelerateInterpolator; 32 | import android.view.animation.Interpolator; 33 | import android.widget.ImageView; 34 | import android.widget.OverScroller; 35 | 36 | import java.lang.annotation.Retention; 37 | import java.lang.annotation.RetentionPolicy; 38 | 39 | class TouchManager { 40 | 41 | private static final int MINIMUM_FLING_VELOCITY = 2500; 42 | 43 | private final CropViewConfig cropViewConfig; 44 | 45 | private final ScaleGestureDetector scaleGestureDetector; 46 | private final GestureDetector gestureDetector; 47 | 48 | private float minimumScale; 49 | private float maximumScale; 50 | private Rect imageBounds; 51 | private float aspectRatio; 52 | private int viewportWidth; 53 | private int viewportHeight; 54 | private int bitmapWidth; 55 | private int bitmapHeight; 56 | 57 | private int verticalLimit; 58 | private int horizontalLimit; 59 | 60 | private float scale = -1.0f; 61 | private final TouchPoint position = new TouchPoint(); 62 | 63 | private final ImageView imageView; 64 | 65 | private final GestureAnimator gestureAnimator = new GestureAnimator(new GestureAnimator.OnAnimationUpdateListener() { 66 | @Override 67 | public void onAnimationUpdate(@GestureAnimator.AnimationType int animationType, float animationValue) { 68 | if(animationType == GestureAnimator.ANIMATION_X) { 69 | position.set(animationValue, position.getY()); 70 | ensureInsideViewport(); 71 | } 72 | else if(animationType == GestureAnimator.ANIMATION_Y) { 73 | position.set(position.getX(), animationValue); 74 | ensureInsideViewport(); 75 | } 76 | else if(animationType == GestureAnimator.ANIMATION_SCALE) { 77 | scale = animationValue; 78 | setLimits(); 79 | } 80 | 81 | imageView.invalidate(); 82 | } 83 | 84 | @Override 85 | public void onAnimationFinished() { 86 | ensureInsideViewport(); 87 | } 88 | }); 89 | 90 | private final ScaleGestureDetector.OnScaleGestureListener scaleGestureListener = new ScaleGestureDetector.OnScaleGestureListener() { 91 | @Override 92 | public boolean onScale(ScaleGestureDetector detector) { 93 | scale = calculateScale(detector.getScaleFactor()); 94 | setLimits(); 95 | return true; 96 | } 97 | 98 | @Override public boolean onScaleBegin(ScaleGestureDetector detector) {return true;} 99 | @Override public void onScaleEnd(ScaleGestureDetector detector) {} 100 | }; 101 | 102 | private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { 103 | @Override 104 | public boolean onDown(MotionEvent e) { 105 | return true; 106 | } 107 | 108 | @Override 109 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 110 | if (e2.getPointerCount() != 1) { 111 | return true; 112 | } 113 | 114 | TouchPoint delta = new TouchPoint(-distanceX, -distanceY); 115 | position.add(delta); 116 | ensureInsideViewport(); 117 | return true; 118 | } 119 | 120 | @Override 121 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 122 | velocityX /= 2; 123 | velocityY /= 2; 124 | 125 | if(Math.abs(velocityX) < MINIMUM_FLING_VELOCITY) { 126 | velocityX = 0; 127 | } 128 | if(Math.abs(velocityY) < MINIMUM_FLING_VELOCITY) { 129 | velocityY = 0; 130 | } 131 | 132 | if(velocityX == 0 && velocityY == 0) { 133 | return true; 134 | } 135 | 136 | int width = (int) (imageBounds.right * scale); 137 | int height = (int) (imageBounds.bottom * scale); 138 | 139 | OverScroller scroller = new OverScroller(imageView.getContext()); 140 | scroller.fling((int) e1.getX(), (int) e1.getY(), (int) velocityX, (int) velocityY, -width, width, -height, height); 141 | 142 | TouchPoint target = new TouchPoint(scroller.getFinalX(), scroller.getFinalY()); 143 | float x = velocityX == 0 ? position.getX() : target.getX() * scale; 144 | float y = velocityY == 0 ? position.getY() : target.getY() * scale; 145 | 146 | gestureAnimator.animateTranslation(position.getX(), x, position.getY(), y); 147 | 148 | return true; 149 | } 150 | 151 | @Override 152 | public boolean onDoubleTap(MotionEvent e) { 153 | final float fromX, toX, fromY, toY, targetScale; 154 | 155 | TouchPoint eventPoint = new TouchPoint(e.getX(), e.getY()); 156 | if(scale == minimumScale) { 157 | targetScale = maximumScale / 2; 158 | TouchPoint translatedTargetPosition = mapTouchCoordinateToMatrix(eventPoint, targetScale); 159 | TouchPoint centeredTargetPosition = centerCoordinates(translatedTargetPosition); 160 | fromX = position.getX(); 161 | toX = centeredTargetPosition.getX(); 162 | fromY = position.getY(); 163 | toY = centeredTargetPosition.getY(); 164 | } 165 | else { 166 | targetScale = minimumScale; 167 | TouchPoint translatedPosition = mapTouchCoordinateToMatrix(eventPoint, scale); 168 | TouchPoint centeredTargetPosition = centerCoordinates(translatedPosition); 169 | fromX = centeredTargetPosition.getX(); 170 | toX = 0; 171 | fromY = centeredTargetPosition.getY(); 172 | toY = 0; 173 | } 174 | 175 | gestureAnimator.animateDoubleTap(fromX, toX, fromY, toY, scale, targetScale); 176 | return true; 177 | } 178 | 179 | private TouchPoint centerCoordinates(TouchPoint coordinates) { 180 | float x = coordinates.getX() + (imageBounds.right / 2); 181 | float y = coordinates.getY() + (imageBounds.bottom / 2); 182 | return new TouchPoint(x, y); 183 | } 184 | }; 185 | 186 | public TouchManager(final ImageView imageView, final CropViewConfig cropViewConfig) { 187 | this.imageView = imageView; 188 | scaleGestureDetector = new ScaleGestureDetector(imageView.getContext(), scaleGestureListener); 189 | gestureDetector = new GestureDetector(imageView.getContext(), gestureListener); 190 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 191 | scaleGestureDetector.setQuickScaleEnabled(true); 192 | } 193 | 194 | this.cropViewConfig = cropViewConfig; 195 | 196 | minimumScale = cropViewConfig.getMinScale(); 197 | maximumScale = cropViewConfig.getMaxScale(); 198 | } 199 | 200 | @TargetApi(Build.VERSION_CODES.FROYO) 201 | public void onEvent(MotionEvent event) { 202 | scaleGestureDetector.onTouchEvent(event); 203 | gestureDetector.onTouchEvent(event); 204 | 205 | if (isUpAction(event.getActionMasked())) { 206 | ensureInsideViewport(); 207 | } 208 | } 209 | 210 | public void applyPositioningAndScale(Matrix matrix) { 211 | matrix.postTranslate(-bitmapWidth / 2.0f, -bitmapHeight / 2.0f); 212 | matrix.postScale(scale, scale); 213 | matrix.postTranslate(position.getX(), position.getY()); 214 | } 215 | 216 | public void resetFor(int bitmapWidth, int bitmapHeight, int availableWidth, int availableHeight) { 217 | aspectRatio = cropViewConfig.getViewportRatio(); 218 | imageBounds = new Rect(0, 0, availableWidth / 2, availableHeight / 2); 219 | setViewport(bitmapWidth, bitmapHeight, availableWidth, availableHeight); 220 | 221 | this.bitmapWidth = bitmapWidth; 222 | this.bitmapHeight = bitmapHeight; 223 | if (bitmapWidth > 0 && bitmapHeight > 0) { 224 | setMinimumScale(); 225 | setLimits(); 226 | resetPosition(); 227 | ensureInsideViewport(); 228 | } 229 | } 230 | 231 | public int getViewportWidth() { 232 | return viewportWidth; 233 | } 234 | 235 | public int getViewportHeight() { 236 | return viewportHeight; 237 | } 238 | 239 | public float getAspectRatio() { 240 | return aspectRatio; 241 | } 242 | 243 | public void setAspectRatio(float ratio) { 244 | aspectRatio = ratio; 245 | cropViewConfig.setViewportRatio(ratio); 246 | } 247 | 248 | private TouchPoint mapTouchCoordinateToMatrix(TouchPoint coordinate, float targetScale) { 249 | float width = bitmapWidth * targetScale; 250 | float height = bitmapHeight * targetScale; 251 | 252 | float x0 = width / 2; 253 | float y0 = height / 2; 254 | 255 | float newX = coordinate.getX() * targetScale; 256 | newX = -(newX - x0); 257 | 258 | float newY = coordinate.getY() * targetScale; 259 | if(newY > y0) { 260 | newY = -(newY - y0); 261 | } 262 | else { 263 | newY = y0 - newY; 264 | } 265 | 266 | return new TouchPoint(newX, newY); 267 | } 268 | 269 | private void ensureInsideViewport() { 270 | if (imageBounds == null) { 271 | return; 272 | } 273 | 274 | float newY = position.getY(); 275 | int bottom = imageBounds.bottom; 276 | 277 | 278 | if (bottom - newY >= verticalLimit) { 279 | newY = bottom - verticalLimit; 280 | } else if (newY - bottom >= verticalLimit) { 281 | newY = bottom + verticalLimit; 282 | } 283 | 284 | float newX = position.getX(); 285 | int right = imageBounds.right; 286 | if (newX <= right - horizontalLimit) { 287 | newX = right - horizontalLimit; 288 | } else if (newX > right + horizontalLimit) { 289 | newX = right + horizontalLimit; 290 | } 291 | 292 | position.set(newX, newY); 293 | } 294 | 295 | private void setViewport(int bitmapWidth, int bitmapHeight, int availableWidth, int availableHeight) { 296 | final float imageAspect = (float) bitmapWidth / bitmapHeight; 297 | final float viewAspect = (float) availableWidth / availableHeight; 298 | 299 | float ratio = cropViewConfig.getViewportRatio(); 300 | if (Float.compare(0f, ratio) == 0) { 301 | // viewport ratio of 0 means match native ratio of bitmap 302 | ratio = imageAspect; 303 | } 304 | 305 | if (ratio > viewAspect) { 306 | // viewport is wider than view 307 | viewportWidth = availableWidth - cropViewConfig.getViewportOverlayPadding() * 2; 308 | viewportHeight = (int) (viewportWidth * (1 / ratio)); 309 | } else { 310 | // viewport is taller than view 311 | viewportHeight = availableHeight - cropViewConfig.getViewportOverlayPadding() * 2; 312 | viewportWidth = (int) (viewportHeight * ratio); 313 | } 314 | } 315 | 316 | private void setLimits() { 317 | horizontalLimit = computeLimit((int) (bitmapWidth * scale), viewportWidth); 318 | verticalLimit = computeLimit((int) (bitmapHeight * scale), viewportHeight); 319 | } 320 | 321 | private void resetPosition() { 322 | position.set(imageBounds.right, imageBounds.bottom); 323 | } 324 | 325 | private void setMinimumScale() { 326 | final float fw = (float) viewportWidth / bitmapWidth; 327 | final float fh = (float) viewportHeight / bitmapHeight; 328 | minimumScale = Math.max(fw, fh); 329 | scale = Math.max(scale, minimumScale); 330 | } 331 | 332 | private float calculateScale(float newScaleDelta) { 333 | return Math.max(minimumScale, Math.min(scale * newScaleDelta, maximumScale)); 334 | } 335 | 336 | private static int computeLimit(int bitmapSize, int viewportSize) { 337 | return (bitmapSize - viewportSize) / 2; 338 | } 339 | 340 | private static boolean isUpAction(int actionMasked) { 341 | return actionMasked == MotionEvent.ACTION_POINTER_UP || actionMasked == MotionEvent.ACTION_UP; 342 | } 343 | 344 | private static class GestureAnimator { 345 | @IntDef({ANIMATION_X, ANIMATION_Y, ANIMATION_SCALE}) 346 | @Retention(RetentionPolicy.SOURCE) 347 | public @interface AnimationType {} 348 | public static final int ANIMATION_X = 0; 349 | public static final int ANIMATION_Y = 1; 350 | public static final int ANIMATION_SCALE = 2; 351 | 352 | interface OnAnimationUpdateListener { 353 | void onAnimationUpdate(@AnimationType int animationType, float animationValue); 354 | void onAnimationFinished(); 355 | } 356 | 357 | private ValueAnimator xAnimator; 358 | private ValueAnimator yAnimator; 359 | private ValueAnimator scaleAnimator; 360 | 361 | private AnimatorSet animator; 362 | 363 | private final OnAnimationUpdateListener listener; 364 | 365 | public GestureAnimator(OnAnimationUpdateListener listener) { 366 | this.listener = listener; 367 | } 368 | 369 | final ValueAnimator.AnimatorUpdateListener updateListener = new ValueAnimator.AnimatorUpdateListener() { 370 | @Override 371 | public void onAnimationUpdate(ValueAnimator animation) { 372 | float val = ((float) animation.getAnimatedValue()); 373 | 374 | if(animation == xAnimator) { 375 | listener.onAnimationUpdate(ANIMATION_X, val); 376 | } 377 | else if(animation == yAnimator) { 378 | listener.onAnimationUpdate(ANIMATION_Y, val); 379 | } 380 | else if(animation == scaleAnimator) { 381 | listener.onAnimationUpdate(ANIMATION_SCALE, val); 382 | } 383 | } 384 | }; 385 | 386 | private final Animator.AnimatorListener animatorListener = new AnimatorListenerAdapter() { 387 | @Override 388 | public void onAnimationEnd(Animator animation) { 389 | if(xAnimator != null) xAnimator.removeUpdateListener(updateListener); 390 | if(yAnimator != null) yAnimator.removeUpdateListener(updateListener); 391 | if(scaleAnimator != null) scaleAnimator.removeUpdateListener(updateListener); 392 | animator.removeAllListeners(); 393 | listener.onAnimationFinished(); 394 | } 395 | }; 396 | 397 | public void animateTranslation(float fromX, float toX, float fromY, float toY) { 398 | if(animator != null) { 399 | animator.cancel(); 400 | } 401 | 402 | xAnimator = ValueAnimator.ofFloat(fromX, toX); 403 | yAnimator = ValueAnimator.ofFloat(fromY, toY); 404 | scaleAnimator = null; 405 | 406 | xAnimator.addUpdateListener(updateListener); 407 | yAnimator.addUpdateListener(updateListener); 408 | 409 | animate(new DecelerateInterpolator(), 250, xAnimator, yAnimator); 410 | } 411 | 412 | public void animateDoubleTap(float fromX, float toX, float fromY, float toY, float fromScale, float toScale) { 413 | if(animator != null) { 414 | animator.cancel(); 415 | } 416 | 417 | xAnimator = ValueAnimator.ofFloat(fromX, toX); 418 | yAnimator = ValueAnimator.ofFloat(fromY, toY); 419 | scaleAnimator = ValueAnimator.ofFloat(fromScale, toScale); 420 | 421 | xAnimator.addUpdateListener(updateListener); 422 | yAnimator.addUpdateListener(updateListener); 423 | scaleAnimator.addUpdateListener(updateListener); 424 | 425 | animate(new AccelerateDecelerateInterpolator(), 500, scaleAnimator, xAnimator, yAnimator); 426 | } 427 | 428 | private void animate(Interpolator interpolator, long duration, ValueAnimator first, ValueAnimator... animators) { 429 | animator = new AnimatorSet(); 430 | animator.setDuration(duration); 431 | animator.setInterpolator(interpolator); 432 | animator.addListener(animatorListener); 433 | AnimatorSet.Builder builder = animator.play(first); 434 | for(ValueAnimator valueAnimator : animators) { 435 | builder.with(valueAnimator); 436 | } 437 | animator.start(); 438 | } 439 | } 440 | } 441 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/TouchPoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | class TouchPoint { 19 | 20 | private float x; 21 | private float y; 22 | 23 | public TouchPoint() { 24 | } 25 | 26 | public TouchPoint(float x, float y) { 27 | this.x = x; 28 | this.y = y; 29 | } 30 | 31 | public float getX() { 32 | return x; 33 | } 34 | 35 | public float getY() { 36 | return y; 37 | } 38 | 39 | public float getLength() { 40 | return (float) Math.sqrt(x * x + y * y); 41 | } 42 | 43 | public TouchPoint copy(TouchPoint other) { 44 | x = other.getX(); 45 | y = other.getY(); 46 | return this; 47 | } 48 | 49 | public TouchPoint set(float x, float y) { 50 | this.x = x; 51 | this.y = y; 52 | return this; 53 | } 54 | 55 | public TouchPoint add(TouchPoint value) { 56 | this.x += value.getX(); 57 | this.y += value.getY(); 58 | return this; 59 | } 60 | 61 | public static TouchPoint subtract(TouchPoint lhs, TouchPoint rhs) { 62 | return new TouchPoint(lhs.x - rhs.x, lhs.y - rhs.y); 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return String.format("(%.4f, %.4f)", x, y); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/UILBitmapLoader.java: -------------------------------------------------------------------------------- 1 | package com.lyft.android.scissors2; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.annotation.Nullable; 5 | import android.widget.ImageView; 6 | 7 | import com.nostra13.universalimageloader.core.DisplayImageOptions; 8 | import com.nostra13.universalimageloader.core.ImageLoader; 9 | import com.nostra13.universalimageloader.core.display.BitmapDisplayer; 10 | 11 | /** 12 | * A {@link BitmapLoader} with transformation for {@link ImageLoader} image library. 13 | * 14 | * @see UILBitmapLoader#createUsing(CropView) 15 | * @see UILBitmapLoader#createUsing(CropView, ImageLoader) 16 | */ 17 | public class UILBitmapLoader implements BitmapLoader { 18 | 19 | private final ImageLoader imageLoader; 20 | private final BitmapDisplayer bitmapDisplayer; 21 | 22 | public UILBitmapLoader(ImageLoader imageLoader, BitmapDisplayer bitmapDisplayer) { 23 | this.imageLoader = imageLoader; 24 | this.bitmapDisplayer = bitmapDisplayer; 25 | } 26 | 27 | public static BitmapLoader createUsing(CropView cropView) { 28 | return createUsing(cropView, ImageLoader.getInstance()); 29 | } 30 | 31 | public static BitmapLoader createUsing(CropView cropView, ImageLoader imageLoader) { 32 | return new UILBitmapLoader(imageLoader, UILFillViewportDisplayer.createUsing(cropView.getViewportWidth(), cropView.getViewportHeight())); 33 | } 34 | 35 | @Override 36 | public void load(@Nullable Object model, @NonNull ImageView view) { 37 | final DisplayImageOptions options = new DisplayImageOptions.Builder() 38 | .cacheInMemory(false) 39 | .cacheOnDisk(false) 40 | .displayer(bitmapDisplayer) 41 | .build(); 42 | 43 | if (model instanceof String || model == null) { 44 | imageLoader.displayImage((String) model, view, options); 45 | } else { 46 | throw new IllegalArgumentException("Unsupported model " + model); 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/UILFillViewportDisplayer.java: -------------------------------------------------------------------------------- 1 | package com.lyft.android.scissors2; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Rect; 5 | 6 | import com.nostra13.universalimageloader.core.assist.LoadedFrom; 7 | import com.nostra13.universalimageloader.core.display.BitmapDisplayer; 8 | import com.nostra13.universalimageloader.core.imageaware.ImageAware; 9 | 10 | class UILFillViewportDisplayer implements BitmapDisplayer { 11 | private final int viewportWidth; 12 | private final int viewportHeight; 13 | 14 | public UILFillViewportDisplayer(int viewportWidth, int viewportHeight) { 15 | this.viewportWidth = viewportWidth; 16 | this.viewportHeight = viewportHeight; 17 | } 18 | 19 | public static BitmapDisplayer createUsing(int viewportWidth, int viewportHeight) { 20 | return new UILFillViewportDisplayer(viewportWidth, viewportHeight); 21 | } 22 | 23 | @Override 24 | public void display(Bitmap source, ImageAware imageAware, LoadedFrom loadedFrom) { 25 | int sourceWidth = source.getWidth(); 26 | int sourceHeight = source.getHeight(); 27 | 28 | Rect target = CropViewExtensions.computeTargetSize(sourceWidth, sourceHeight, viewportWidth, viewportHeight); 29 | final Bitmap result = Bitmap.createScaledBitmap( 30 | source, 31 | target.width(), 32 | target.height(), 33 | true); 34 | 35 | if (result != source) { 36 | source.recycle(); 37 | } 38 | 39 | imageAware.setImageBitmap(result); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /scissors2/src/main/java/com/lyft/android/scissors2/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Lyft, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.lyft.android.scissors2; 17 | 18 | import android.graphics.Bitmap; 19 | import android.graphics.Canvas; 20 | import android.graphics.Rect; 21 | import android.graphics.drawable.Drawable; 22 | import android.support.annotation.Nullable; 23 | import android.util.Log; 24 | 25 | import java.io.File; 26 | import java.io.FileOutputStream; 27 | import java.io.OutputStream; 28 | import java.util.concurrent.ExecutorService; 29 | import java.util.concurrent.Executors; 30 | import java.util.concurrent.Future; 31 | 32 | class Utils { 33 | 34 | public static void checkArg(boolean expression, String msg) { 35 | if (!expression) { 36 | throw new IllegalArgumentException(msg); 37 | } 38 | } 39 | 40 | public static void checkNotNull(Object object, String msg) { 41 | if (object == null) { 42 | throw new NullPointerException(msg); 43 | } 44 | } 45 | 46 | public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) { 47 | final Rect tmpRect = new Rect(); 48 | drawable.copyBounds(tmpRect); 49 | if (tmpRect.isEmpty()) { 50 | tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight())); 51 | drawable.setBounds(tmpRect); 52 | } 53 | Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888); 54 | drawable.draw(new Canvas(bitmap)); 55 | return bitmap; 56 | } 57 | 58 | private final static ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(); 59 | private static final String TAG = "scissors.Utils"; 60 | 61 | public static Future flushToFile(final Bitmap bitmap, 62 | final Bitmap.CompressFormat format, 63 | final int quality, 64 | final File file) { 65 | 66 | return EXECUTOR_SERVICE.submit(new Runnable() { 67 | @Override 68 | public void run() { 69 | OutputStream outputStream = null; 70 | 71 | try { 72 | file.getParentFile().mkdirs(); 73 | outputStream = new FileOutputStream(file); 74 | bitmap.compress(format, quality, outputStream); 75 | outputStream.flush(); 76 | } catch (final Throwable throwable) { 77 | if (BuildConfig.DEBUG) { 78 | Log.e(TAG, "Error attempting to save bitmap.", throwable); 79 | } 80 | } finally { 81 | closeQuietly(outputStream); 82 | } 83 | } 84 | }, null); 85 | } 86 | 87 | public static Future flushToStream(final Bitmap bitmap, 88 | final Bitmap.CompressFormat format, 89 | final int quality, 90 | final OutputStream outputStream, 91 | final boolean closeWhenDone) { 92 | 93 | return EXECUTOR_SERVICE.submit(new Runnable() { 94 | @Override 95 | public void run() { 96 | try { 97 | bitmap.compress(format, quality, outputStream); 98 | outputStream.flush(); 99 | } catch (final Throwable throwable) { 100 | if (BuildConfig.DEBUG) { 101 | Log.e(TAG, "Error attempting to save bitmap.", throwable); 102 | } 103 | } finally { 104 | if (closeWhenDone) { 105 | closeQuietly(outputStream); 106 | } 107 | } 108 | } 109 | }, null); 110 | } 111 | 112 | private static void closeQuietly(@Nullable OutputStream outputStream) { 113 | try { 114 | if (outputStream != null) { 115 | outputStream.close(); 116 | } 117 | } catch (Exception e) { 118 | Log.e(TAG, "Error attempting to close stream.", e); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /scissors2/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /scissors2/src/test/java/com/lyft/android/scissors2/TargetSizeTest.java: -------------------------------------------------------------------------------- 1 | package com.lyft.android.scissors2; 2 | 3 | import android.graphics.Rect; 4 | import java.util.Arrays; 5 | import java.util.Collection; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.robolectric.ParameterizedRobolectricTestRunner; 9 | import org.robolectric.annotation.Config; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.assertj.core.api.Assertions.fail; 13 | 14 | @RunWith(ParameterizedRobolectricTestRunner.class) 15 | @Config(manifest = Config.NONE) 16 | public class TargetSizeTest { 17 | 18 | @ParameterizedRobolectricTestRunner.Parameters(name = "{2} viewport = [{0}x{1}]") 19 | public static Collection data() { 20 | return Arrays.asList(new Object[][] { 21 | { 100, 100, SQUARED }, 22 | { 100, 56, LANDSCAPE }, 23 | { 56, 100, PORTRAIT } 24 | }); 25 | } 26 | 27 | final Rect viewport; 28 | final String orientation; 29 | 30 | public TargetSizeTest(int viewportW, int viewportH, String orientation) { 31 | this.viewport = new Rect(0, 0, viewportW, viewportH); 32 | this.orientation = orientation; 33 | } 34 | 35 | @Test 36 | public void targetFitsViewport() { 37 | final int sourceW = viewport.width(); 38 | final int sourceH = viewport.height(); 39 | 40 | Rect target = CropViewExtensions.computeTargetSize(sourceW, sourceH, viewport.width(), viewport.height()); 41 | 42 | assertThat(target.width()).isEqualTo(viewport.width()); 43 | assertThat(target.height()).isEqualTo(viewport.height()); 44 | } 45 | 46 | @Test 47 | public void targetScalesUpToViewport() { 48 | final int sourceW = viewport.width() - 11; 49 | final int sourceH = viewport.height() - 11; 50 | 51 | Rect target = CropViewExtensions.computeTargetSize(sourceW, sourceH, viewport.width(), viewport.height()); 52 | 53 | switch (orientation) { 54 | case LANDSCAPE: 55 | assertThat(target.width()).isGreaterThan(viewport.width()); 56 | assertThat(target.height()).isEqualTo(viewport.height()); 57 | break; 58 | case PORTRAIT: 59 | assertThat(target.width()).isEqualTo(viewport.width()); 60 | assertThat(target.height()).isGreaterThan(viewport.height()); 61 | break; 62 | case SQUARED: 63 | assertThat(target.width()).isEqualTo(viewport.width()); 64 | assertThat(target.height()).isEqualTo(viewport.height()); 65 | break; 66 | default: 67 | fail("Unexpected orientation " + orientation); 68 | } 69 | } 70 | 71 | @Test 72 | public void targetScalesDownIfBiggerThanViewport() { 73 | final int sourceW = viewport.width() + 101; 74 | final int sourceH = viewport.height() + 101; 75 | 76 | Rect target = CropViewExtensions.computeTargetSize(sourceW, sourceH, viewport.width(), viewport.height()); 77 | 78 | switch (orientation) { 79 | case LANDSCAPE: 80 | assertThat(target.width()).isEqualTo(viewport.width()); 81 | assertThat(target.height()).isGreaterThan(viewport.height()); 82 | break; 83 | case PORTRAIT: 84 | assertThat(target.width()).isGreaterThan(viewport.width()); 85 | assertThat(target.height()).isEqualTo(viewport.height()); 86 | break; 87 | case SQUARED: 88 | assertThat(target.width()).isEqualTo(viewport.width()); 89 | assertThat(target.height()).isEqualTo(viewport.height()); 90 | break; 91 | default: 92 | fail("Unexpected orientation " + orientation); 93 | } 94 | } 95 | 96 | static final String SQUARED = "Squared"; 97 | static final String LANDSCAPE = "Landscape"; 98 | static final String PORTRAIT = "Portrait"; 99 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'scissors2', 'scissors-sample' 2 | --------------------------------------------------------------------------------