65

Has anyone managed to use RoundedBitmapDrawable? Correct me if I'm wrong, but to my understanding, it makes a circular image from a regular rectangular image.

What I've tried so far is this

RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))

What I tried to achieve: transform any image to a circular image and show it using an ImageView.

In case I mixed things up and all that I said is non-sense. Is it possible (or simpler) to do it with any of the new framework? (Android L or new Support Library)

gian1200
  • 3,567
  • 2
  • 27
  • 57

8 Answers8

122

You need to set the corner radius.

Resources res = getResources();
Bitmap src = BitmapFactory.decodeResource(res, iconResource);
RoundedBitmapDrawable dr =
    RoundedBitmapDrawableFactory.create(res, src);
dr.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 2.0f);
imageView.setImageDrawable(dr);
Deepak Goel
  • 5,278
  • 5
  • 36
  • 50
alanv
  • 22,812
  • 4
  • 87
  • 77
  • 1
    Thanks for answering. I found that method too. Just one question, why Math.max and getHeight/getwitdth /2; and not `dr.setCornerRadius(Math.min(dr.getMinimumWidth(),dr.getMinimumHeight`? – gian1200 Jul 23 '14 at 03:51
  • You're right, that's better. I think they might actually return the same result due to RoundedBitmapDrawable constraining the corner radius. Just passing in the width or height would probably work, as well. – alanv Jul 23 '14 at 04:06
  • RoundedBitmapDrawableFactory only has a "create()" function and not "createRoundedBitmapDrawable", both get the same parameters, what am I doing wrong ? – Miko Diko Apr 10 '15 at 13:36
  • 17
    There is `dr.setCircular(true)` – don't panic Jun 16 '16 at 03:18
  • toolbar logo doesnt accept `RoundedBitmapDrawable` so how do i get rounded bitmap from this? `getBitmap` returns the original bitmap – Cerlin Feb 06 '17 at 07:50
  • 2
    What about non-square images as input? According to what I see, the output is stretched in this case. Any way to use center crop? – android developer May 24 '17 at 12:03
67

It may be a late reply but i hope that it will be helpful to others

If your image has same width and height then you can simply set the setCircular to true to get Rounded Bitmap as follows

RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),your_bitmap);
drawable.setCircular(true);
Vamsi Smart
  • 878
  • 9
  • 16
9

i am also finding rounded image view for efficiency i have search all third party library i found that all of them they are creating new bitmap which is tedious task in list its consuming more memory

refereed library:

  1. http://ruibm.com/2009/06/16/rounded-corner-bitmaps-on-android/
  2. https://github.com/vinc3m1/RoundedImageView
  3. https://github.com/lopspower/CircularImageView

from this library i have used

https://github.com/vinc3m1/RoundedImageView

because A fast ImageView (and Drawable) that supports rounded corners (and ovals or circles) based on the original example from Romain Guy

  • does not create a copy of the original bitmap
  • does not use a clipPath which is not hardware accelerated and not anti-aliased.
  • does not use setXfermode to clip the bitmap and draw twice to the canvas.
MilapTank
  • 9,383
  • 7
  • 35
  • 52
5

Full code:

ImageView img= (ImageView) findViewById(R.id.yourimageid);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.yourpictureresource);

RoundedBitmapDrawable rnd = (RoundedBitmapDrawable) RoundedBitmapDrawableFactory.create(getResources(), bitmap);
rnd.setCircular(true);
img.setImageDrawable(rnd);
Lakhani Aliraza
  • 191
  • 3
  • 8
3

I created a Utility class to create RoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb

It works for circles and rounded squares.

public class RoundedBitmapDrawableUtility {

    public static RoundedBitmapDrawable getRoundedSquareBitmapDrawable(Context context, Bitmap originalBitmap, int cornerRadius){
        return getRoundedSquareBitmapDrawable(context, originalBitmap, cornerRadius, -1, -1);
    }


    public static RoundedBitmapDrawable getRoundedSquareBitmapDrawable(Context context, Bitmap originalBitmap, int cornerRadius, int borderWidth, int borderColor){
        int originalBitmapWidth = originalBitmap.getWidth();
        int originalBitmapHeight = originalBitmap.getHeight();

        if(borderWidth != -1 && borderColor != -1){
            Canvas canvas = new Canvas(originalBitmap);
            canvas.drawBitmap(originalBitmap, 0, 0, null);

            Paint borderPaint = new Paint();
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(borderWidth);
            borderPaint.setAntiAlias(true);
            borderPaint.setColor(borderColor);

            int roundedRectDelta = (borderWidth/3);
            RectF rectF = new RectF(0 + roundedRectDelta, 0 + roundedRectDelta, originalBitmapWidth - roundedRectDelta, originalBitmapHeight - roundedRectDelta);
            canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, borderPaint);
        }

        RoundedBitmapDrawable roundedImageBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), originalBitmap);
        roundedImageBitmapDrawable.setCornerRadius(cornerRadius);
        roundedImageBitmapDrawable.setAntiAlias(true);
        return roundedImageBitmapDrawable;
    }

    public static RoundedBitmapDrawable getCircleBitmapDrawable(Context context, Bitmap originalBitmap){
        return getCircleBitmapDrawable(context, originalBitmap, -1, -1);
    }

    public static RoundedBitmapDrawable getCircleBitmapDrawable(Context context, Bitmap originalBitmap, int borderWidth, int borderColor){
        if(borderWidth != -1 && borderColor != -1) {
            Canvas canvas = new Canvas(originalBitmap);
            canvas.drawBitmap(originalBitmap, 0, 0, null);

            Paint borderPaint = new Paint();
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(borderWidth);
            borderPaint.setAntiAlias(true);
            borderPaint.setColor(borderColor);

            int circleDelta = (borderWidth / 2) - DisplayUtility.dp2px(context, 1);
            int radius = (canvas.getWidth() / 2) - circleDelta;
            canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, radius, borderPaint);
        }

        RoundedBitmapDrawable roundedImageBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), originalBitmap);
        roundedImageBitmapDrawable.setCircular(true);
        roundedImageBitmapDrawable.setAntiAlias(true);
        return roundedImageBitmapDrawable;
    }
}
toobsco42
  • 6,131
  • 16
  • 73
  • 85
2

I would like to suggest another option:

you can use this method in order to scale it according to your needs and avoid this Exception:

public static Bitmap scaleBitmapAndKeepRation(Bitmap TargetBmp, int reqHeightInPixels, int reqWidthInPixels) {
        if (TargetBmp != null) {

            if (TargetBmp.getWidth() >= TargetBmp.getHeight()) {

                TargetBmp = Bitmap.createBitmap(
                        TargetBmp,
                        TargetBmp.getWidth() / 2 - TargetBmp.getHeight() / 2,
                        0,
                        TargetBmp.getHeight(),
                        TargetBmp.getHeight()
                );

            } else {

                TargetBmp = Bitmap.createBitmap(
                        TargetBmp,
                        0,
                        TargetBmp.getHeight() / 2 - TargetBmp.getWidth() / 2,
                        TargetBmp.getWidth(),
                        TargetBmp.getWidth()
                );
            }

            if (TargetBmp != null) {
                try {
                    Matrix m = new Matrix();
                    m.setRectToRect(new RectF(0, 0, TargetBmp.getWidth(), TargetBmp.getHeight()), new RectF(0, 0, reqWidthInPixels, reqHeightInPixels), Matrix.ScaleToFit.FILL);
                    Bitmap scaledBitmap = Bitmap.createBitmap(TargetBmp, 0, 0, TargetBmp.getWidth(), TargetBmp.getHeight(), m, true);
                    return scaledBitmap;
                } catch (Exception e) {
                    Log.e("Utils", e.toString());
                    return null;
                }
            }
            return null;
        } else
            return null;
    }
Gal Rom
  • 5,509
  • 2
  • 37
  • 30
2

The current way RoundedBitmapDrawable works on non-square images is not so well. It makes the content stretch.

I suggest using an alternative for this, as I've written here: How to have a circular, center-cropped imageView, without creating a new bitmap?

android developer
  • 106,412
  • 122
  • 641
  • 1,128
0

Incase if you have imagePath instead of bitmap, you can pass imagePath like below:

// set round image
RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(getResources(), avatarImgPath);
dr.setCircular(true);
avatarImageView.setImageDrawable(dr);
Rajeev Jayaswal
  • 949
  • 11
  • 20