How to use RoundedBitmapDrawable

AndroidAndroid DrawableAndroid Bitmap

Android Problem Overview


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)

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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

Solution 6 - Android

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

Solution 7 - Android

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: https://stackoverflow.com/q/35481807/878126

Solution 8 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongian1200View Question on Stackoverflow
Solution 1 - AndroidalanvView Answer on Stackoverflow
Solution 2 - AndroidVamsi SmartView Answer on Stackoverflow
Solution 3 - AndroidMilapTankView Answer on Stackoverflow
Solution 4 - AndroidLakhani AlirazaView Answer on Stackoverflow
Solution 5 - AndroidEtienne LawlorView Answer on Stackoverflow
Solution 6 - AndroidGal RomView Answer on Stackoverflow
Solution 7 - Androidandroid developerView Answer on Stackoverflow
Solution 8 - AndroidRajeev JayaswalView Answer on Stackoverflow