Wednesday, 19 June 2013

Crop image in circular shape in android

Crop image in circular shape in android

Hi guys Today very use full code I am going to share with you,
 In one of my project I need image set in rounded shape , but when i select image from camera or gallery it is in square shapes so i found this very useful code 
that take your Image Bitmap and return Rounded image bitmap :) So I made its simple class 
you can just add class in your project and call it when it requires.

 Public Class Rounder{
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 125;
  int targetHeight = 125;
  Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
  Canvas canvas = new Canvas(targetBitmap);
  Path path = new Path();
  path.addCircle(((float) targetWidth - 1) / 2,
  ((float) targetHeight - 1) / 2,
  (Math.min(((float) targetWidth), 
                ((float) targetHeight)) / 2),
  Bitmap sourceBitmap = scaleBitmapImage;
                                new Rect(0, 0, sourceBitmap.getWidth(),
                                new Rect(0, 0, targetWidth,
    targetHeight), null);
  return targetBitmap;

and use it as 
Bitmap roundedBitmapImage=new Rounder().getRoundedShape(YourNormalBitmapImage);

Hope This will help Ful :)
Happy Codddddding :)