Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView rounded corners [duplicate]

I wanted image to have rounded corners. I implement this xml code and use this in my image view. but image overlap the shape. I am downloading the image through async task.

<?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle" >  <corners android:radius="20dip" /> </shape>   <ImageView     android:id="@+id/trVouchersImage"     android:layout_width="55dp"     android:layout_height="55dp"     android:layout_marginLeft="8dp"     android:layout_centerVertical="true"     android:layout_centerHorizontal="true"     android:layout_alignParentLeft="true"     android:background="@drawable/ash_arrow"  /> 
like image 902
FIXI Avatar asked Dec 23 '13 12:12

FIXI


People also ask

How do I make rounded corners in paint?

In the upper left corner, select “Draw Filled Shape“. Draw the rounded rectangle over the area you would like to keep for your rounded corners image. Use the Magic Wand to select the area of the rounded rectangle.

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.


1 Answers

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

like image 165
Nihal Avatar answered Oct 12 '22 00:10

Nihal