如何设置图像视图的圆角?

时间:2020-06-18 20:30:47

标签: java android xml image imageview

如何设置ImageView的圆角代替内部图像。

我正在尝试使用滑行设置ImageView的圆角,但是滑行仅设置图像的圆角而不是ImageView

我在ImageView中创建了CardView。我用捏来放大ImageView中的手势。 当我使用滑行设置ImageView的圆角时,然后滑行设置图像的ImageView角,而不是CardView形状。

2 个答案:

答案 0 :(得分:1)

您可以使用此库https://github.com/siyamed/android-shape-imageview,它提供了很多形状,可以为您提供帮助!

首先,在您的build.gradle(应用程序)中添加此依赖项

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

现在需要使用圆角图像视图

<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"/>

并创建一个名为shape_rounded_rectangle的新可绘制文件,其中包含:

<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>

要使用带边框的矩形,只需使用

<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"/>

这就是您得到的:

Image without borders

还有Image with borders

答案 1 :(得分:0)

您可以在自定义ImageView中覆盖onDraw方法,并在画布上绘制RoundRect:

@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);
    }