uCrop - Image Cropping Library for Android
This project aims to provide an ultimate and flexible image cropping experience.
Usage
- Include the library as local library project.
compile 'com.yalantis:ucrop:1.0.1'
- Add UCropActivity into your AndroidManifest.xml
<activity android:name="com.yalantis.ucrop.UCropActivity" android:screenOrientation="portrait"/>
- The uCrop configuration is created using the builder pattern.
UCrop.of(sourceUri, destinationUri) .withAspectRatio(16, 9) .withMaxResultSize(maxWidth, maxHeight) .start(context);
- Override
onActivityResult
method and handle uCrop result.@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) { final Uri resultUri = UCrop.getOutput(data); } else if (resultCode == UCrop.RESULT_ERROR) { final Throwable cropError = UCrop.getError(data); } }
Customization
If you want to let your users choose crop ratio dynamically, just do not call
withAspectRatio(x, y)
.
uCrop builder class has method
withOptions(UCrop.Options option)
which extends library configurations.
Currently you can change:
- image compression format (e.g. PNG, JPEG, WEBP), compression
- image compression quality [0 - 100]. PNG which is lossless, will ignore the quality setting.
- whether all gestures are enabled simultaneously
- maximum size for Bitmap that is decoded from source Uri and used within crop view. If you want to override default behaviour.
- more coming... (e.g. color pallet)
Compatibility
- Library - Android GINGERBREAD 2.3+
- Sample - Android ICS 4.0+
No comments:
Post a Comment