通过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个回答

6
发布此答案供将来参考。您可以使用com.google.android.material:material中提供的ShapeableImageView
<com.google.android.material.imageview.ShapeableImageView
    android:id="@+id/img_launcher_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_margin="5dp"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:elevation="5dp"
    android:maxWidth="50dp"
    android:maxHeight="50dp"
    android:scaleType="fitXY"
    android:src="@mipmap/ic_launcher"
    app:shapeAppearance="?attr/actionButtonStyle"
    app:shapeAppearanceOverlay="@style/imageViewRounded"
    app:strokeColor="@android:color/white" />

在styles.xml中添加imageViewRounded样式。
<style name="imageViewRounded">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">51%</item>
</style>

如未添加,可添加Material Design依赖。

    implementation 'com.google.android.material:material:1.4.0'

设计看起来像

在此输入图片描述


很棒的答案!它运行得很好。 - matdev

6
这会解决问题:
矩形.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <padding android:bottom="-14dp" android:left="-14dp" android:right="-14dp" android:top="-14dp" />

</shape>

circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="oval"

    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="15dp"
        android:color="@color/verification_contact_background" />

</shape>

profile_image.xml(图层列表)

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

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

</layer-list>

您的布局
 <ImageView
        android:id="@+id/profile_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/default_org"
        android:src="@drawable/profile_image"/>

默认_org是什么? - John Joe
(此处放置任意占位图片) - Nidhi
在矩形尺寸中,图像边缘将会跨过圆形的边界。我该如何确切地将每个图像在圆内的各个维度和形式上定位? - inverted_index

5

更新 2021: 使用 Glide v4 的 CircleCrop,请参见https://bumptech.github.io/glide/doc/generatedapi.html

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

XML

<ImageView
    android:id="@+id/vinyl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ContentDescription" />

在代码中

    Glide.with(this)
            .load("https://images.pexels.com/photos/3828241/pexels-photo-3828241.jpeg")
            .transform(CircleCrop())
            .into(rootView.findViewById<ImageView>(R.id.vinyl))

enter image description here


谢谢,我想点赞但不想鼓励不端行为。 - fullmoon

5

最佳解决方案由https://www.youtube.com/watch?v=0MHoNU7ytaw提供,卡片视图的宽度和高度决定了它包含的图像的大小,设置如下:

  1. 在Gradle(模块)中添加依赖项。
  2. 将xml代码添加到activity.xml或fragment.xml文件中。
    implementation 'androidx.cardview:cardview:1.0.0'

   <androidx.cardview.widget.CardView
      android:layout_width="300dp"
      android:layout_height="270dp"
      android:layout_gravity="center"
      app:cardCornerRadius="150dp"
      app:cardBackgroundColor="@color/trans"
      >
    <ImageView
        android:id="@+id/resultImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/congrats"
        android:layout_gravity="center">

    </ImageView>


  </androidx.cardview.widget.CardView>```


4

我使用 shape="oval" 代替下面的 ring。这对我有用。为了保持图片在范围内,我在我的 <ImageView> 中使用 <padding> 并将 <adjustViewBounds> 设置为 true。我尝试过大小在 50 x 50 像素到 200 x 200 像素之间的图像。


你也可以添加以下内容:android:scaleType="fitCenter" - Ishrak

3

@Jyotman Singh的答案很好(适用于扎实的背景),我想通过共享可重新着色的矢量可绘制对象来增强它,而且由于矢量单个形状很好地可缩放,因此非常方便。

这是矩形圆形形状(@drawable/shape_round_profile_pic):

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="284"
    android:viewportHeight="284"
    android:width="284dp"
    android:height="284dp">
    <path
        android:pathData="M0 142L0 0l142 0 142 0 0 142 0 142 -142 0 -142 0zm165 137.34231c26.06742 -4.1212 52.67405 -17.543 72.66855 -36.65787 11.82805 -11.30768 20.55487 -22.85153 27.7633 -36.72531C290.23789 158.21592 285.62874 101.14121 253.48951 58.078079 217.58149 9.9651706 154.68849 -10.125717 98.348685 8.5190299 48.695824 24.95084 12.527764 67.047123 3.437787 118.98655 1.4806194 130.16966 1.511302 152.96723 3.4990422 164.5 12.168375 214.79902 47.646316 256.70775 96 273.76783c21.72002 7.66322 44.26673 9.48476 69 5.57448z"
        android:fillColor="#ffffff" /> // you can change frame color
</vector>

使用方法相同:

<FrameLayout
        android:layout_width="70dp"
        android:layout_height="70dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/YOUR_PICTURE" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shape_round_profile_pic"/>

    </FrameLayout>

这个应该怎么工作?你只是在图像上方显示一个圆圈,而实际上要求的是如何裁剪图像,使其看起来像一个圆形。 - Fattum
请查看@Jyotman Singh上面的答案。对我很有帮助,所以我稍微改进了一下。要裁剪图像,您可以使用:GlideApp.with(getApplicationContext()).asBitmap().load(profilePhotoUrl).circleCrop() - ekar
谢谢你提供圆形内嵌矩形的形状!正是我所需要的! - siralexsir88

2
只需使用以下代码即可完成:

这些代码就可以了:

<de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:clickable="true"
            app:civ_border_width="3dp"
            app:civ_border_color="#FFFFFFFF"
            android:id="@+id/profile"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_below="@+id/header_cover_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-130dp"
            android:elevation="5dp"
            android:padding="20dp"
            android:scaleType="centerCrop"
            android:src="@drawable/profilemain" />

在此输入图片描述

不要忘记导入:

import de.hdodenhof.circleimageview.CircleImageView;

在 build.gradle 中添加此库:
compile 'de.hdodenhof:circleimageview:2.1.0'

你的Java类可能缺少某些内容。 - John Joe

2

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

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

2
如果您在应用程序中使用Material Design,请使用此选项。
<com.google.android.material.card.MaterialCardView
            android:layout_width="75dp"
            android:layout_height="75dp"
            app:cardCornerRadius="50dp"
            app:strokeWidth="1dp"
            app:strokeColor="@color/black">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/circular_image"
                android:scaleType="fitCenter"
                android:src="@drawable/your_img" />
        </com.google.android.material.card.MaterialCardView>

1
我是这样做的,我在我的矢量图像中使用了我的背景颜色。

ic_bg_picture.xml

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100"
    android:viewportHeight="100">
  <path
      android:pathData="M100.6,95.5c0,-0.4 -0.1,-0.7 0,-1.1c-0.2,-0.7 -0.2,-1.4 -0.1,-2.1c0,-0.1 0,-0.2 0,-0.3c-0.1,-0.6 -0.1,-1.2 0,-1.8c-1,-1.3 -0.3,-2.9 -0.3,-4.3c-0.1,-28.7 -0.1,-57.3 -0.1,-86C68,-0.1 35.9,-0.1 3.8,-0.2C0.7,-0.2 0,0.5 0,3.6c0.1,32.1 0.1,64.2 0.1,96.2c31,0 62,-0.1 92.9,0.1c3.6,0 6.3,-0.2 7.5,-3.2C100.5,96.4 100.5,95.9 100.6,95.5zM46.3,95.2C26.4,94 2,74.4 3.8,46.8C5.1,27.2 24.4,2.7 52.6,4.6c20.2,1.4 43,21.3 41.5,45.1C96.1,72.4 73,96.8 46.3,95.2z"
      android:fillColor="#6200EE"/>
</vector>

在我的情况下,我创建了一个向量并将android:fillColor="#6200EE"更改为我的背景颜色。
  <ImageView
    android:id="@+id/iv_profile_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:contentDescription="@string/app_name"
    app:srcCompat="@color/colorPrimaryDark" />

<ImageView
    android:id="@+id/container_profile_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:contentDescription="@string/app_name"
    app:srcCompat="@drawable/ic_bg_picture"/>

example example 2


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