【问题标题】:How to set Rounded Corners of Image View? [duplicate]如何设置图像视图的圆角? [复制]
【发布时间】:2020-10-09 00:51:26
【问题描述】:

如何设置ImageView的圆角而不是内部图像。

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

我在CardView 中创建ImageView。我在ImageView 中使用了捏缩放手势。 当我使用 glide 设置 ImageView 的圆角然后 glide 设置图像的角而不是 ImageViewCardView 形状相同。

【问题讨论】:

    标签: java android xml image imageview


    【解决方案1】:

    你可以使用这个库https://github.com/siyamed/android-shape-imageview 它提供了很多形状并且可以帮助你!

    首先在你的 build.gradle (app) 中添加这个依赖

    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

    【讨论】:

    • 感谢您回答我...但我正在使用捏合来缩放图像视图中的手势。
    • 我更新了我的问题。现在你可以尝试回答了
    • @AkashAttal 尝试将 cardview 的角设置为圆角?
    【解决方案2】:

    您可以在自定义 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-30
      • 2013-01-24
      • 1970-01-01
      • 2017-01-19
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      相关资源
      最近更新 更多