Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Round Corner image in ImageView

Tags:

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

like image 552
Ponmalar Avatar asked Jul 28 '15 11:07

Ponmalar


People also ask

How do you round corners in ImageView?

If you MUST use background to set your image instead of src, you can use this workaround: Create a layout and set its background to your shape drawable. Wrap that layout around your ImageView (with no padding) The ImageView (including anything else in the layout) will now display with rounded layout shape.

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 make a picture rounded on Android?

Simply put a circular_crop. png in your drawable folder which is in the shape of your image dimensions (a square in my case) with a white background and a transparent circle in the center. You can use this image if you have want a square imageview. Just download the picture above.


2 Answers

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>

enter image description here

*Note: it requires at least the version 1.2.0-alpha03.

like image 67
Gabriele Mariotti Avatar answered Sep 26 '22 11:09

Gabriele Mariotti


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.

enter image description here

In my demo, I using this method for rounded imageview. Can find it here cuberto's dialog

like image 21
Cuong Vo Avatar answered Sep 26 '22 11:09

Cuong Vo