-1

How to set rounded corners of ImageView instead inner Image.

I'm trying to set rounded corners of ImageView with glide but glide only set rounded corners of image not ImageView.

I create ImageView in CardView. I used pinch to zoom gesture in ImageView. when i using glide to set rounded corners of ImageView then glide set corners of image instead ImageView as same as CardView shape.

2 Answers2

1

You can use this library https://github.com/siyamed/android-shape-imageview it provides lots of shapes and could help you!

At first add this dependency in you build.gradle (app)

compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'

Now for getting rounded corners imageview you need to use

<com.github.siyamed.shapeimageview.mask.PorterShapeImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:siShape="@drawable/shape_rounded_rectangle"
    android:src="@drawable/neo"
    app:siSquare="true"/>

And create a new drawable file called shape_rounded_rectangle containing:

<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:topLeftRadius="18dp"
        android:topRightRadius="18dp"
        android:bottomLeftRadius="18dp"
        android:bottomRightRadius="18dp" />
    <solid android:color="@color/black" />
</shape>

To get the rectangle with borders just use

<com.github.siyamed.shapeimageview.RoundedImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/neo"
    app:siRadius="6dp"
    app:siBorderWidth="6dp"
    app:siBorderColor="@color/darkgray"
    app:siSquare="true"/>

And here is what you get:

Image without borders

And Image with borders

0

You can override onDraw method in your custom ImageView and draw RoundRect on canvas:

@Override
    protected void onDraw(Canvas canvas) {
        rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        path.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
Mariusz Brona
  • 1,549
  • 10
  • 12