How to use RoundedBitmapDrawable

39,017

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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;
    }
}
Share:
39,017
gian1200
Author by

gian1200

I'm a human being who uses devices to access and use internet

Updated on May 16, 2021

Comments

  • gian1200
    gian1200 about 3 years

    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
    gian1200 almost 10 years
    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.getMinim‌​umHeight?
  • alanv
    alanv almost 10 years
    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.
  • Miko Diko
    Miko Diko about 9 years
    RoundedBitmapDrawableFactory only has a "create()" function and not "createRoundedBitmapDrawable", both get the same parameters, what am I doing wrong ?
  • don't panic
    don't panic almost 8 years
    There is dr.setCircular(true)
  • Tash Pemhiwa
    Tash Pemhiwa almost 8 years
    Simple, brilliant!
  • Cerlin
    Cerlin over 7 years
    toolbar logo doesnt accept RoundedBitmapDrawable so how do i get rounded bitmap from this? getBitmap returns the original bitmap
  • Cerlin
    Cerlin over 7 years
    toolbar logo doesnt accept RoundedBitmapDrawable so how do i get rounded bitmap from this? getBitmap returns the original bitmap
  • android developer
    android developer almost 7 years
    When the input image is not square (a long rectangle, for example), the output circular image content is stretched inside. Is there any way to achieve a center-crop mechanism for this?
  • android developer
    android developer almost 7 years
    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?