ImageView rounded corners [duplicate] ImageView rounded corners [duplicate] xml xml

ImageView rounded corners [duplicate]


SIMPLEST APPROACH:

Create an xml file rounded_fg.xml under res/drawable/ folder of your app. The content of rounded_fg.xml is as follows,

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:innerRadiusRatio="2"    android:shape="ring"    android:thicknessRatio="1"    android:useLevel="false">    <gradient        android:type="radial"        android:gradientRadius="8dp"        android:endColor="@color/white"       /></shape>

You can match endColor with ImageView container layout background & gradientRadius may be any value as per your requirements (<=36dp).

Now use this drawable as foreground for your imageview as follows,

 <ImageView     android:layout_width="55dp"     android:layout_height="55dp"      android:foreground="@drawable/rounded_fg" />

Works perfect with square images and/or imageview.

Square Image/ImageView:

Square Image/ImageView

Rectangular Image/ImageView:

Rectangular Image/ImageView

Foreground applied over a button:

Foreground applied over a button


I use Universal Image loader library to download and round the corners of image, and it worked for me.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(thisContext)            // You can pass your own memory cache implementation           .discCacheFileNameGenerator(new HashCodeFileNameGenerator())           .build();DisplayImageOptions options = new DisplayImageOptions.Builder()            .displayer(new RoundedBitmapDisplayer(10)) //rounded corner bitmap            .cacheInMemory(true)            .cacheOnDisc(true)            .build();ImageLoader imageLoader = ImageLoader.getInstance();imageLoader.init(config);imageLoader.displayImage(image_url,image_view, options );


you can do by XML like this way

<stroke android:width="3dp"        android:color="#ff000000"/><padding android:left="1dp"         android:top="1dp"         android:right="1dp"         android:bottom="1dp"/> <corners android:radius="30px"/> 

and pragmatically you can create rounded bitmap and set in ImageView.

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),    bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);final int color = 0xff424242;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);final float roundPx = 12;paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);return output;}

For Universal lazy loader you can use this wat also.

DisplayImageOptions options = new DisplayImageOptions.Builder()        .displayer(new RoundedBitmapDisplayer(25)) // default        .build();