Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Last active March 7, 2024 07:50
Show Gist options
  • Star 82 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save gabrielemariotti/446c63ea2aed81aafc0bdec1488e27c9 to your computer and use it in GitHub Desktop.
Save gabrielemariotti/446c63ea2aed81aafc0bdec1488e27c9 to your computer and use it in GitHub Desktop.
How to use the ShapeableImageView.

The Material Components Library introduced with the 1.2.0-alpha03 the new ShapeableImageView.

In your layout you can use:

 <com.google.android.material.imageview.ShapeableImageView
      android:id="@+id/image_view"
      app:srcCompat="@drawable/..." />

Then in your code apply the ShapeAppearanceModel to define your custom corners:

  @ExperimentalImageView
  private void setup() {

    ShapeableImageView imageView = findViewById(R.id.image_view);

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

  }

image

Also you can use the shapeAppearanceOverlay attribute in your layout to define the shape with a style.
For example to achieve a circular image:

 <com.google.android.material.imageview.ShapeableImageView
      android:id="@+id/image_view"
      app:shapeAppearanceOverlay="@style/circleImageView"
      app:srcCompat="@drawable/..." />

with:

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

image

@mrzbn
Copy link

mrzbn commented Jun 26, 2022

i found a not very good workaround and that is to set alpha of ImageView to 0.99

@alansatsha
Copy link

How can i set an image from the internet?

@shalva97
Copy link

How can i set an image from the internet?

Use Glide, it also allows handling corners

@GOmoraes
Copy link

GOmoraes commented Mar 24, 2023

Got the same problem, removing android:hardwareAccelerated="false" from manifest solves the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment