1

How can I add rounded corners to images and a little drop shadow so it looks like the attached?

Here's what I have in my activity

        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- Content here -->

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="107dp"
            android:layout_height="166dp"
            android:layout_marginStart="20dp"
            android:elevation="5dp"
            android:scaleType="fitCenter"
            android:translationZ="12dp"
            android:layout_marginTop="15dp" />

enter image description here

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165
  • https://stackoverflow.com/questions/60941554/imageview-rejecting-rounded-corners-and-borders-of-parent/60951786#60951786 – MMG Apr 14 '20 at 18:49
  • Does this answer your question? [ImageView rejecting rounded corners and borders of parent](https://stackoverflow.com/questions/60941554/imageview-rejecting-rounded-corners-and-borders-of-parent) – MMG Apr 14 '20 at 18:49
  • use a `CardView` as root layout .. – ADM Apr 14 '20 at 18:50

2 Answers2

1

You have different options.

  • You can use a CardView as root layout.
  • You can apply rounded corners to your LinearLayout using a MaterialShapeDrawable (check this answer)
  • starting with the version 1.2.0-alpha03 of the the Material Components Library you can change the ImageView to the new ShapeableImageView.

Something like:

  <com.google.android.material.imageview.ShapeableImageView
      ...
      app:shapeAppearanceOverlay="@style/roundedImageView"
      app:srcCompat="@drawable/ic_custom_image" />

with:

  <style name="roundedImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>

Or programmatically:

float radius = getResources().getDimension(R.dimen.corner_radius);
imageView.setShapeAppearanceModel(imageView.getShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,radius)
    .build());

enter image description here

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

You can use something ready such that https://github.com/hdodenhof/CircleImageView

or make your custom background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- View background color -->
    <solid
        android:color="@color/colorPrimary" >
    </solid>
    <!-- The radius makes the corners rounded -->
    <corners
        android:radius="8dp" >
    </corners>

</shape>

you can change the radius in corners tag then mark it as background

 <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"/>