6

How to have a curcular ImageView. This is my xml

<RelativeLayout
            android:id="@+id/imgUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="@drawable/circle_border">

            <ImageView
                android:id="@+id/studentimg"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_margin="9dp"
                android:adjustViewBounds="true"
                android:background="@drawable/circlepf"
                android:padding="3dp"
                android:scaleType="centerInside"
                android:src="@drawable/ic_profile" />
</RelativeLayout>

this is my problem image sample

1

but I want this image to show in circular view

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30

2 Answers2

23

You can use the Material Components Library.
With the version 1.2.0-alpha03 there is the new ShapeableImageView.

  <com.google.android.material.imageview.ShapeableImageView
      ...
      android:scaleType="centerInside"
      android:adjustViewBounds="true"
      app:shapeAppearanceOverlay="@style/circleImageView"
      app:srcCompat="@drawable/ic_profile" />

with:

  <style name="circleImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

I recently found this library in Github. https://github.com/christophesmet/android_maskable_layout

Any layout or imageview wrapped in the MaskableFrameLayout will be cropped in the desired shape by just providing the required shape to the mask attribute of the maskLayout as shown below:

<com.christophesmet.android.views.maskableframelayout.MaskableFrameLayout
    android:id="@+id/frm_mask_animated"
    android:layout_width="100dp"
    app:porterduffxfermode="DST_IN"
    app:mask="@drawable/drawable_circular"
    android:layout_height="100dp">

    <ImageView android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:scaleType="centerCrop"
               android:src="@drawable/unicorn"/>

</com.christophesmet.android.views.maskableframelayout.MaskableFrameLayout>

Also, you have Circleimageview library available :https://github.com/hdodenhof/CircleImageView

Basi
  • 3,009
  • 23
  • 28