1

I am trying to crop my images as a circle, for this I am using the provided library of Android-Image-Cropper - 'com.theartofdev.edmodo:android-image-cropper:2.4.+' At the moment I am able to choose an image, the crop window appears as a circle, although it doesnt then crop to a circle but instead a square.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {
        Uri imageUri = data.getData();
        CropImage.activity(imageUri)
                .setAspectRatio(150,150)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setCropShape(CropImageView.CropShape.OVAL)
                .start(this);
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            mImageUri = result.getUri();
            mSetupImageBtn.setImageURI(mImageUri);
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }
}
1
  • Might be easier to keep the image rectangular, and use Picasso with a circle transform whenever you display the image in the app. Commented Aug 21, 2017 at 16:41

2 Answers 2

1

you can use glide for the same as mentioned in the first comment How to round an image with Glide library?

secondly, you can use the below library compile 'de.hdodenhof:circleimageview:1.2.1'

using Picasso you can use above circle image library ab can load image in it using picasso like this:

Picasso.with(activity).load(url).into(ImageView);

example :

<de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/img_profile"
                        android:layout_width="100dp"
                        android:layout_height="100dp" />



Picasso.with(activity).load(url).into(ImageView);

if you don't want to use circleImageView then you can create a transformation as used in this links comment

android: create circular image with picasso

1
  • ahh you know how i can do this with Picasso Commented Aug 21, 2017 at 18:10
0

ArthurHub/Android-Image-Cropper is not supported anymore (read more here). So you can migrate to a new project CanHub/Android-Image-Cropper which supports the circle crop function.

Example below:

Add CropImageVIew to your markup:

<com.canhub.cropper.CropImageView
    android:id="@+id/cropView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:cornerShape="rectangle" />

Setup image in the fragment or activity:

binding.cropView.setImageUriAsync(imageUri)

Add result listener to cropView:

binding.cropView.setOnCropImageCompleteListener { view, result ->
    //  !!! You should use result.getBitmap(ctx), because result.bitmap returns null !!!
    // Read more here: https://github.com/CanHub/Android-Image-Cropper/pull/70

    val imageBitmap = result.getBitmap(requireContext())!!
    val ovalBitmap = CropImage.toOvalBitmap(imageBitmap)

    // Now you can save ovalBitmap to file or set it to ImageView with setImageBitmap()
}

Add a click listener to your "crop button":

binding.cropImageButton.setOnClickListener {
    binding.cropView.croppedImageAsync()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.