10

Does anyone know how to crop an image\bitmap to a circle? I can not find any solution, sorry ..

AlexMrKlim
  • 270
  • 1
  • 3
  • 15
  • @user1281750 read question detail exactly! It's not duplicate any how! – Paresh Mayani Feb 13 '14 at 13:57
  • possible duplicate of [Cropping Circular Area from bitmap in android](http://stackoverflow.com/questions/11932805/cropping-circular-area-from-bitmap-in-android) – Adinia Mar 17 '14 at 11:15

6 Answers6

9

For having rounded corners for ImageView, convert your image into bitmap and then try following code :

private Bitmap getRoundedCroppedBitmap(Bitmap bitmap) {
    int widthLight = bitmap.getWidth();
    int heightLight = bitmap.getHeight();
    
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            Config.ARGB_8888);
        
    Canvas canvas = new Canvas(output);
    Paint paintColor = new Paint();
    paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);
        
    RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
        
    canvas.drawRoundRect(rectF, widthLight / 2, heightLight / 2, paintColor);
        
    Paint paintImage = new Paint();
    paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
    canvas.drawBitmap(bitmap, 0, 0, paintImage);
        
    return output;
}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Aj 27
  • 2,316
  • 21
  • 29
2

Romain Guy, formerly an engineer on the Android team at Google, posted an excellent article on drawing images with rounded corners. This idea could be easily extended to a circle, for example, by changing the rounded rectangle radius so that it creates a complete circle.

From the article:

To generate the rounded images I simply wrote a custom Drawable that draws a rounded rectangle using Canvas.drawRoundRect(). The trick is to use a Paint with a BitmapShader to fill the rounded rectangle with a texture instead of a simple color. Here is what the code looks like:

BitmapShader shader; shader = new BitmapShader(bitmap,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

Paint paint = new Paint(); paint.setAntiAlias(true);
paint.setShader(shader);

RectF rect = new RectF(0.0f, 0.0f, width, height);

// rect contains the bounds of the shape
// radius is the radius in pixels of the rounded corners
// paint contains the shader that will texture the shape
canvas.drawRoundRect(rect, radius, radius, paint);
Scott W
  • 9,742
  • 2
  • 38
  • 53
  • 1
    Canvas also exposes a [drawCircle](http://developer.android.com/reference/android/graphics/Canvas.html) method which might be more appropriate for this case. – greg7gkb Mar 12 '15 at 00:29
1

Class:

  public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {

    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                        targetHeight,Bitmap.Config.ARGB_8888);
                        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),
      Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, 
                            new Rect(0, 0, sourceBitmap.getWidth(),
      sourceBitmap.getHeight()), 
                            new Rect(0, 0, targetWidth,
      targetHeight), null);
    return targetBitmap;
   }

View:

<ImageView
        android:id="@+id/imgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnEdit"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:background="@drawable/rounded"
        android:adjustViewBounds="true"
        android:gravity="center"
        android:src="@drawable/happy"/>

Additional styles:

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="@android:color/white" />

<stroke
    android:width="3dip"
    android:color="#FF0000" />

<corners android:radius="10dp" />

<padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
1

Wiseman Designs, have an open source Circular ImageView ready for use

https://github.com/wisemandesigns/CircularImageView

This uses XML in your layouts, which makes life easier. You can set the source in XML or with some minor modification could easily use a Bitmap.

Disclaimer: I work for Wiseman Designs

Graham Smith
  • 25,627
  • 10
  • 46
  • 69
0

Try with below code :

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
  // TODO Auto-generated method stub
  int targetWidth = 50;
  int targetHeight = 50;
  Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                            targetHeight,Bitmap.Config.ARGB_8888);

                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),
          Path.Direction.CCW);

                canvas.clipPath(path);
  Bitmap sourceBitmap = scaleBitmapImage;
  canvas.drawBitmap(sourceBitmap, 
                                new Rect(0, 0, sourceBitmap.getWidth(),
    sourceBitmap.getHeight()), 
                                new Rect(0, 0, targetWidth,
    targetHeight), null);
  return targetBitmap;
 }
Robban
  • 6,729
  • 2
  • 39
  • 47
0
finalBitmapShader shader = newBitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mBitmapWidth=mBitmap.getWidth();
mBitmapHeight=mBitmap.getHeight();
}
@Override
public void draw(Canvas canvas{ 
  canvas.drawOval(mRectF,mPaint);
}
@Override
protected void onBoundsChange(Rect bounds) {
  super.onBoundsChange(bounds);
  mRectF.set(bounds);
}

here I found a sample tutorial of it at http://androidgreeve.blogspot.in/2014/09/facebook-messanger-like-profile-image.html?m=1

kmas
  • 6,401
  • 13
  • 40
  • 62