Set Round Corner image in ImageView
Solution 1
The Material Components Library provides the new ShapeableImageView
.
Just use a layout like:
<com.google.android.material.imageview.ShapeableImageView
...
app:shapeAppearanceOverlay="@style/roundedCorners"
app:srcCompat="@drawable/...." />
With the shapeAppearanceOverlay
attribute you can apply a custom shape, in this case rounded corners:
<style name="roundedCorners" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">xxdp</item>
</style>
*Note: it requires at least the version 1.2.0-alpha03
.
Solution 2
The answer from sats is worked. But RoundedBitmapDrawable cannot be applied to an ImageView with scaleType: centerCrop.
An updated.
If you are using Android API at level 21. We have a simple solution here.
1st, create drawable as background for the imageview
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@color/colorWhite" />
</shape>
Next, change ImageView's property setClipToOutline to true. And set rounded drawable as ImageView's background.
That's it.
In my demo, I using this method for rounded imageview. Can find it here cuberto's dialog
Solution 3
Put imageView in CardView and set CornerRadius for CardView
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">
<ImageView
android:id="@+id/imgTourismHr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
</android.support.v7.widget.CardView>
Solution 4
you can use the standard API's provided by Google to create a circular ImageView.
ImageView imageView = (ImageView) findViewById(R.id.circleImageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCircular(true);
imageView.setImageDrawable(roundedBitmapDrawable);
Also refer https://www.youtube.com/watch?v=zVC-YAnDlgk
Solution 5
See the rounded rectangle image Output with Rounded Corner border
Now to Set Rounded Rectangle Image with rounded Border.
You Can Simple Declare CardView inside CardView With app:cardCornerRadius="30dp", app:cardUseCompatPadding="true", app:cardElevation="0dp".
See the Below Xml Code
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:cardCornerRadius="30dp"
app:cardUseCompatPadding="true"
app:cardElevation="0dp"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/temp"
android:scaleType="fitXY"
android:layout_gravity="center"
/>
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
Related videos on Youtube
Ponmalar
Updated on November 03, 2021Comments
-
Ponmalar over 2 years
I have searched more on websites and got many suggestions like below
Changing background with custom style to set the corner radius and padding ( Getting image as rectangle and background as rounded corner)
Changing Given image to Bitmap by decoding the image and cropping functionality by passing this bitmap and width ( its taking more time to load)
I have checked the
WhatsApp Chat history list and it has rounded corner images but its loaded with minimum time
.Could you please suggest me the best way to create listview template with rounded images as in WhatsApp?
I am expecting to change the image with rounded corner by setting the style details in xml page. (image width and height is 60 dp, also my listview having around 60 items)
References :
- https://xjaphx.wordpress.com/2011/06/23/image-processing-image-with-round-corner/
- How to Make an ImageView in Circular Shape?
- How to create a circular ImageView in Android?
res/mylayout.xml:
<ImageView android:id="@+id/contactssnap" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/contactssnap" android:background="@drawable/roundimage" />
res/drawable/roundimage.xml:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ffffffff" /> <stroke android:width="2dp" android:color="#90a4ae" /> <size android:height="50dp" android:width="50dp" /> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /> <corners android:radius="100dp" /> </shape>
Note: Supported Android Version from 14 to 22
-
adnan_e almost 9 yearsHow are you setting the ImageView? From resource or do you download the bitmap from somewhere else?
-
bGorle almost 9 yearsstackover flow already have an answer refer this stackoverflow.com/questions/2459916/…
-
Ponmalar almost 9 years@bGorle: yes i tried this and its taking more time to load images
-
pskink almost 9 yearsuse
android.support.v4.graphics.drawable.RoundedBitmapDrawable
-
AskNilesh about 4 yearsCheck this now we have
ShapeableImageView
to make circular or rounded imageView stackoverflow.com/a/61086632/7666442
-
smoothumut almost 5 yearsthis is the only solution worked for me for transparent rounded corners
-
Smit Thakkar over 4 yearsThanks this look like works for me but the only problem I'm having is the second parameter of
decodeResource
. I have aimageUrl
which loads the image. how do I achieve this in this case ? -
AskNilesh about 4 yearsCheck this now we have
ShapeableImageView
to make circular or rounded imageView stackoverflow.com/a/61086632/7666442 -
AskNilesh about 4 yearsCheck this now we have
ShapeableImageView
to make circular or rounded imageView stackoverflow.com/a/61086632/7666442 -
Shazbot over 3 yearsThis is one of the most elegant ways I've seen, applies very well for Picasso error / placeholders, too!
-
X6Entrepreneur almost 2 yearsbest answer.. no need to redevelop the wheel... thanks to google material.