Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RoundedBitmapDrawable

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)

like image 279
gian1200 Avatar asked Jul 22 '14 04:07

gian1200


3 Answers

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);
like image 168
alanv Avatar answered Oct 23 '22 05:10

alanv


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);
like image 34
Vamsi Smart Avatar answered Oct 23 '22 07:10

Vamsi Smart


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.
like image 9
MilapTank Avatar answered Oct 23 '22 06:10

MilapTank