Crop Image as circle in Android

AlexMrKlim picture AlexMrKlim · Oct 18, 2012 · Viewed 31.1k times · Source

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

Answer

Aj 27 picture Aj 27 · Feb 5, 2015

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;
}