How to use RoundedBitmapDrawable

点点圈 提交于 2019-12-18 10:10:13

问题


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)


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



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



回答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.



回答4:


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



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



回答6:


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



回答7:


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?



来源:https://stackoverflow.com/questions/24878740/how-to-use-roundedbitmapdrawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!