Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Last active March 25, 2024 13:55
Show Gist options
  • Save kaushikgopal/6c25bc9470bbd11fcf7c to your computer and use it in GitHub Desktop.
Save kaushikgopal/6c25bc9470bbd11fcf7c to your computer and use it in GitHub Desktop.
RoundedImageView - drop dead easy way to do this with RoundedBitmapDrawable
// hat tip to the amazing [Chris Banes](https://chris.banes.me/)
// for pointing out the existance of [RoundedBitmapDrawable in the support libs](https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawable.html)
Bitmap batmapBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.batman);
RoundedBitmapDrawable circularBitmapDrawable = //
RoundedBitmapDrawableFactory.create(getResources(), batmapBitmap);
circularBitmapDrawable.setCornerRadius(batmapBitmap.getWidth());
myImageView.setImageDrawable(circularBitmapDrawable);
System.out.prinln("MyActivity", "That's all folks");
// While this is now super simple to do, I would encourage you to:
// 1. read Evelio's blog post http://evel.io/2013/07/21/rounded-avatars-in-android/ (excellent for understanding different approaches)
// 2. See Vince's code in this library https://github.com/vinc3m1/RoundedImageView
@csorgod
Copy link

csorgod commented Nov 20, 2016

Awesome!

You can use circularBitmapDrawable.setCircular(true) instead of "circularBitmapDrawable.setCornerRadius(batmapBitmap.getWidth());".

@kaushikgopal
Copy link
Author

@csorgod : hot damn! oddly i've never run into it. Thanks! will add that usage too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment