-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Home
public class CropImageView
extends FrameLayout
.
compile 'com.theartofdev.edmodo:android-image-cropper:1.2.+'
Add com.theartofdev.edmodo.cropper.CropImageView
in your layout XML.
<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"
custom:cropScaleType="fitCenter"
custom:cropShowGuidelines="onTouch"
custom:cropFixAspectRatio="true"
custom:cropShape="oval"/>
You can, alternatively, modify attributes 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(ImageView.ScaleType.FIT_CENTER);
To set image to crop using existing bitmap use setImageBitmap(myBitmap)
or setImageBitmap(myBitmap, exif)
to rotate the image correctly.
To set image to crop using Android URI (receiving from image picker) use setImageUri(imageUri)
, it will automatically read image exif data to rotate it.
cropImageView.setImageBitmap(myBitmap, exif);
cropImageView.setImageUri(imageUri);
To retrieve the crop rectangle of the crop window use getActualCropRect()
or to retrieve image contained within the crop window use getCroppedImage()
.
If the image was loaded using URI you can specific required size so the cropped image will be sampled to match the size.
Rect rect = cropImageView.getActualCropRect();
Bitmap bitmap = cropImageView.getCroppedImage();
Bitmap bitmap = cropImageView.getCroppedImage(500,500);
To rotate the image clockwise, call rotateImage(int degrees)
, where degrees is a value between 0 and 360. The widget will automatically scale the image to fit current boundaries.
cropImageView.rotateImage(90);
Note: android:layout_width
and android:layout_height
will set the FrameLayout of the crop widget, but not the image itself. However, the widget is built to automatically resize the given image within the FrameLayout according to ImageView.ScaleType.CENTER_INSIDE
or ImageView.ScaleType.FIT_CENTER
. If the original image dimensions are smaller than the FrameLayout both in width and height, it will center itself in the frame without any re-sizing or fit the available size preserving image ratio.
Sets the bitmap of the imageView by taking in an image ID or reference. If no ID is entered, Cropper will simply display nothing.
Sets whether the guidelines within the crop window will be displayed. Setting the XML value "off" corresponds to an integer value of 0, indicating no guidelines will be displayed. Setting the XML value "onTouch" corresponds to an integer value of 1, indicating guidelines will be displayed when the cropWindow is touched. Setting the XML value "on" corresponds to an integer value of 2, indicating guidelines will always be displayed. Setting an integer value outside of 0-2 will cause a NullPointerException.
Fixes the aspect ratio. If this is turned off, the crop window will reset and default to the size of the image, but with 10% padding on each side. If this is turned on, the crop window will reset and expand as much as possible given the aspect ratio.
Sets the X value of the aspect ratio, where the aspect ratio is equivalent to X / Y. Defaults to 1 / 1.
Sets the Y value of the aspect ratio, where the aspect ratio is equivalent to X / Y. Defaults to 1 / 1.