A fast ImageView (and Drawable) that supports rounded corners based on the original example from Romain Guy
There are many ways to create rounded corners in android, but this is the fastest and best one that I know of because it:
- 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.
If you know of a better method, let me know and I'll implement it!
Also has proper support for:
- Borders
- All
ScaleType
s- Borders are drawn at view edge, not bitmap edge.
- Except on edges where the bitmap is smaller than the view
- Borders are not scaled up/down with the image (correct width and radius are maintained)
- Anti-aliasing
- Transparent backgrounds
- Hardware acceleration
- Support for TransitionDrawables (XML attrs only)
Import Library: RoundedImageView/library/
Define in xml:
<com.makeramen.rounded.RoundedImageView
xmlns:makeramen="http://schemas.android.com/apk/res/YOUR_PACKAGE_NAME"
android:id="@+id/imageView1"
android:src="@drawable/photo1"
android:scaleType="centerCrop"
makeramen:corner_radius="30dip"
makeramen:border="2dip"
makeramen:border_color="#333333"
makeramen:round_background="true" />
Or in code:
RoundedImageView iv = new RoundedImageView(context);
iv.setScaleType(ScaleType.CENTER_CROP);
iv.setCornerRadius(10);
iv.setBorderWidth(2);
iv.setBorderColor(Color.DKGRAY);
iv.setRoundedBackground(true);
iv.setImageDrawable(drawable);
iv.setBackground(backgroundDrawable);
- Does not round images set by
setImageResource(int resId)
. This causes bitmaps to be inflated on the UI thread anyway, so you should usesetImageDrawable
orBitmapFactory
andsetImageBitmap()
instead. - Programmatically setting attributes with TransitionDrawables not yet supported.
- Only tested support for BitmapDrawables and TransitionDrawables (with BitmapDrawables in them). Other types might work but may have unexpected behavior.