Skip to content

Using CropImageView in own Activity

Arthur edited this page Jun 3, 2016 · 7 revisions
  1. Include the library
compile 'com.theartofdev.edmodo:android-image-cropper:2.2.+'
  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.setImageBitmap(bitmap);
// or
cropImageView.setImageUriAsync(uri);
  1. Get cropped image
Bitmap cropped = cropImageView.getCroppedImage();
// or (must subscribe to async event using cropImageView.setOnGetCroppedImageCompleteListener(listener))
cropImageView.setOnGetCroppedImageCompleteListener(new CropImageView.OnGetCroppedImageCompleteListener() {
  @Override
  public void onGetCroppedImageComplete(CropImageView view, Bitmap bitmap, Exception error) {
  }
});
cropImageView.getCroppedImageAsync();

You can modify programmatically by CropImageView methods:

CropImageView cropImageView = (CropImageView) findViewById(R.id.cropImageView);
cropImageView.setAspectRatio(5, 10);
cropImageView.setFixedAspectRatio(true);
cropImageView.setGuidelines(1);
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);

Clone this wiki locally