通过 XML 循环的 ImageView

新手上路,请多包涵

我想将我的 ImageView 中的任何图像制作成带边框的圆形。

我搜索但找不到任何有用的信息(我尝试过的任何东西都不起作用)。

我怎样才能通过 XML 实现这一点:使用某些 src 创建一个 ImageView 并使其带有边框的圆形?

原文由 user3050910 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 486
2 个回答

您可以制作一个带有白色边框的简单圆圈和带有形状的透明内容。

 // res/drawable/circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1.9"
    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="10dp"
        android:color="@android:color/white" />
</shape>

然后制作一个可绘制的图层列表并将其作为图像视图的背景。

 // res/drawable/img.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/circle"/>
    <item android:drawable="@drawable/ic_launcher"/>

</layer-list>

并将其作为图像视图的背景。

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/img"/>

你会有类似的东西。

在此处输入图像描述

原文由 Orhan Obut 发布,翻译遵循 CC BY-SA 4.0 许可协议

另一种不使用任何库的方法是使用 ImageFilterView 并为视图设置圆形百分比将使圆形变为圆形

应用程序:roundPercent=“1”

   <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/ivProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/custom_button_1"
        app:roundPercent="1"
        android:scaleType="fitXY"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/etName"/>

原文由 Yahya M 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题