通过XML实现圆形ImageView

295

我想让我的ImageView中的任何图像都带有圆形边框。

我搜索了但没有找到有用的信息(任何我尝试的方法都不起作用)。

如何通过XML实现这一点: 创建一个具有特定src的ImageView并使其成为圆形边框?


如何在Java代码中更改ImageView的src? - chenzhongpu
1
这里有一个好的简单解决方案:https://dev59.com/OGct5IYBdhLWcg3wpO_H#28096369 - 全部使用Java编写,因此您可以在运行时动态地将此格式应用于图像。 - Gene Bo
你必须将ImageView放在CardView内部,因为CardView只有访问圆角属性的功能。 - Vahag Chakhoyan
31个回答

-1

此外,这两个库也可以帮助您。

https://github.com/vinc3m1/RoundedImageView

实现以下代码:

implementation 'com.makeramen:roundedimageview:2.3.0'

简单用法:

<com.makeramen.roundedimageview.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/imageView1"
        android:src="@drawable/photo1"
        android:scaleType="fitCenter"
        app:riv_corner_radius="30dip"
        app:riv_border_width="2dip"
        app:riv_border_color="#333333"
        app:riv_mutate_background="true"
        app:riv_tile_mode="repeat"
        app:riv_oval="true" />

https://github.com/chirag-kachhadiya/RoundedImageView

简单用法:

实现以下代码:

implementation 'com.github.chirag-kachhadiya:RoundedImageView:1.0'



 <com.infinityandroid.roundedimageview.RoundedImageView
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:adjustViewBounds="true"
                android:src="@drawable/the_hundred"
                app:corner_radius="10" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接