Skip to content

Using CropImageView in own Activity

rumwerfer edited this page Dec 10, 2020 · 7 revisions
  1. Include the library
compile 'com.theartofdev.edmodo:android-image-cropper:2.5.+'
  1. Add CropImageView into your activity
<!-- Image Cropper fill the remaining available height -->
<com.theartofdev.edmodo.cropper.CropImageView
   xmlns:custom="http://schemas.android.com/apk/res-auto"
   android:id="@+id/cropImageView"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"/>
  1. Set image to crop
cropImageView.setImageUriAsync(uri);
// or (prefer using uri for performance and better user experience)
cropImageView.setImageBitmap(bitmap);
  1. Get cropped image
// subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener)
cropImageView.setOnCropImageCompleteListener(new CropImageView.OnCropImageCompleteListener() {
   @Override
   public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
   }
 });
cropImageView.getCroppedImageAsync();

// or
Bitmap cropped = cropImageView.getCroppedImage();

You can modify programmatically by CropImageView methods:

CropImageView cropImageView = (CropImageView) findViewById(R.id.cropImageView);
cropImageView.setAspectRatio(5, 10);
cropImageView.setFixedAspectRatio(true);
cropImageView.setGuidelines(CropImageView.Guidelines.ON);
cropImageView.setCropShape(CropImageView.CropShape.OVAL);
cropImageView.setScaleType(CropImageView.ScaleType.FIT_CENTER);
cropImageView.setAutoZoomEnabled(true);
cropImageView.setShowProgressBar(true);
cropImageView.setCropRect(new Rect(0, 0, 800, 500));

Retrieving cropped data

Instead of getting the cropped image bitmap you can get the requested cropped data: the crop rectangle and rotation.

Rect rect = cropImageView.getCropRect();
int rotation = cropImageView.getRotatedDegrees();

Rotation

To rotate the image call rotateImage(int degrees), where degrees is a value between -360 and 360. Positive value will rotate clockwise and negative counter-clockwise. The widget will automatically scale the image to fit current boundaries.

cropImageView.rotateImage(90);