Scissors: an image cropping library for Android
After we introduced profiles a few months ago, Helen — an Engineer in one of our features team — was tasked to implement the second iteration of profiles for our Android app. This update to profiles included numerous improvements including the ability to set a custom profile picture, either from the camera or picking it from external applications like Gallery or Photos.
Before uploading the picture to our servers it had to be cropped following certain requirements, among which were:
- Pinch to zoom up to 200%
- Panning and snapping to the viewport
- Maintaining a certain ratio regardless of screen density
- Cropping based on current viewport dimensions
At Lyft we love open source, so naturally we researched existing solutions. None of them fulfilled the requirements we needed so we decided to build our own. Fast forward a few months and we are now open sourcing the view that represents the core of this feature: Scissors.
Scissors
What does it do?
Scissors provides a view called CropView, which extends ImageView offering familiar ways to provide a Bitmap to crop, for example, using setImageBitmap. Once the user has panned and zoomed around (constrained by cropviewMaxScale and cropviewMinScale) all you have to do is call
Bitmap croppedBitmap = cropView.crop();
The returned Bitmap matches the viewport dimensions, which can be controlled by using cropviewViewportHeightRatio.
Extensions
We also added some handy extensions to help with common tasks like:
cropView.extensions()
.load(galleryUri);
You can also create a custom BitmapLoader to provide a Bitmap just the way you want it.
- Saving a cropped Bitmap into a File or stream without blocking the main thread
cropView.extensions()
.crop()
.quality(87)
.format(PNG)
.into(croppedFile);
You can also provide format and quality of resulting File/stream.
Future work
We want to make Scissors as sharp as possible so in the future we aim to add double tap drag to zoom support, among other fixes and optimizations. We hope you find Scissors useful and it will become the library for all your image cropping needs.
Get started with Scissors on Github
Interested in open source, Android and cool features? Lyft is hiring!
N E X T → Announcing Confidant, an open source secret management service from Lyft