ImageView循环通过xml

use*_*910 180 xml android imageview

我想把我的任何图像ImageView变成带边框的圆形.

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

我如何通过xml实现这一点:ImageView使用某个src 创建一个并使其带边框的圆形?

shr*_*hat 259

这是我设计的最简单的方法.试试这个.

dependencies: compile 'com.android.support:appcompat-v7:23.1.1'
              compile 'com.android.support:design:23.1.1'
              compile 'com.android.support:cardview-v7:23.1.1'

<android.support.v7.widget.CardView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:elevation="12dp"
    android:id="@+id/view2"
   app:cardCornerRadius="40dp"
    android:layout_centerHorizontal="true"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1.9">
    <ImageView
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:id="@+id/imageView1"
        android:src="@drawable/YOUR_IMAGE"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
    </ImageView>
 </android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

如果您正在使用lollipop以上的Android版本

<android.support.v7.widget.CardView
android:layout_width="80dp"
android:layout_height="80dp"
android:elevation="12dp"
android:id="@+id/view2"
app:cardCornerRadius="40dp"
android:layout_centerHorizontal="true">
<ImageView
    android:layout_height="80dp"
    android:layout_width="match_parent"
    android:id="@+id/imageView1"
    android:src="@drawable/YOUR_IMAGE"
    android:scaleType="centerCrop"/>
  </android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

  • 这使得cardview四舍五入,但图像仍然是正方形 (16认同)
  • 尝试使用它,看起来它不适用于5.0以下的Android.即使图像视图是圆形的,图像也较小,方形,看起来很糟糕.有什么建议吗?5.0及以上工作正常. (11认同)
  • 这里的创意很好 (6认同)
  • 我的图像不适合使用此方法的CardView (5认同)
  • 我认为设置图像视图的缩放类型是有意义的:`android:scaleType="centerCrop"` (3认同)
  • 谢谢,这里的关键是卡片半径必须是宽度和高度的一半才能形成一个适当的圆。 (3认同)
  • 这是行不通的。图像与卡片重叠并变成正方形。 (2认同)

Orh*_*but 208

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

// 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>
Run Code Online (Sandbox Code Playgroud)

然后创建一个可绘制的图层列表,并将其作为背景添加到您的图像视图中.

// 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/ic_launcher"/>
    <item android:drawable="@drawable/circle"/>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

并将其作为您的imageview的背景.

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/img"/>
Run Code Online (Sandbox Code Playgroud)

你会有类似的东西.

在此输入图像描述

  • 如果采用这种方式,如何在java代码中更改ImageView的src? (71认同)
  • 然后我将该形状设置为我的imageview的背景?我试过了,但没有奏效.我的图片仍然是矩形的:( (13认同)
  • 无用的,硬编码的 img 源,我想用代码更改 (5认同)
  • 但还是有问题.除了图像前面有圆形边框外,它在圆圈外面不透明.我的意思是,圆圈在那里,然而,图像角落是可见的,并超越它背后的圆形区域 (4认同)
  • 是的,使用XML,因为使用Java代码对于移动设备而言非常昂贵。使用此代码,它只会在我的图像后面出现一个圆圈,而图像本身不是一个圆圈。我想是为了边框,我想让我的图像在这个范围内圆整 (3认同)
  • 不幸的是,它不适用于从 URL 捕获的毕加索图像。 (2认同)

San*_*iya 107

我希望这对你有所帮助.

1)使用第三方库

在此输入图像描述

<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
Run Code Online (Sandbox Code Playgroud)

注意:在项目中,打开your_app> Gradle Scripts> build.gradle(Module:app)并将以下实现语句添加到依赖项{}

     implementation 'de.hdodenhof:circleimageview:3.0.0'   
Run Code Online (Sandbox Code Playgroud)

有关完整说明,请在此处查看:来源.

2)没有第三方库

<com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/image"
        app:civ_border_color="#EEEEEE"
        app:civ_border_width="4dp"
        app:civ_shadow="true"
        app:civ_shadow_radius="10"
        app:civ_shadow_color="#8BC34A"/>
Run Code Online (Sandbox Code Playgroud)

RES /值/ attrs.xml

     implementation 'com.mikhaellopez:circularimageview:3.2.0'   
Run Code Online (Sandbox Code Playgroud)

布局

<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么人们讨厌第三方 (14认同)
  • 自发布以来已有更新.border_colour和border_width已重命名.查看github页面以获取更多信息:https://github.com/hdodenhof/CircleImageView (2认同)

Gab*_*tti 76

使用材料组件库只需使用ShapeableImageView.
类似的东西:

<com.google.android.material.imageview.ShapeableImageView
    app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
    app:strokeColor="@color/....."
    app:strokeWidth="1dp"
    ...
    />
Run Code Online (Sandbox Code Playgroud)

和:

  <style name="roundedImageViewRounded">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意:它至少需要版本1.2.0.


使用jetpack compose, 1.0.x您可以clip使用以下命令应用修改器CircleShape

Image(
    painter = painterResource(R.drawable.xxxx),
    contentDescription = "xxxx",
    contentScale = ContentScale.Crop,
    modifier = Modifier
        .size(100.dp)
        .clip(CircleShape)
        .border(2.dp, Color.Blue, CircleShape)
)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 这应该是公认的答案。最后,像 Google Material 这样的人提出了一个圆角 imageVview 解决方案,它消除了人们仍在使用的所有编写糟糕的第三方库。另外,我不建议在 Glide 上使用转换,因为如果您使用占位符,Glide 也不允许您将转换添加到占位符,遗憾的是。所以这个答案解决了所有问题并且最容易使用。 (12认同)
  • 使用 android:scaleType="centerCrop" 几乎可以解决非完美正方形图像的问题 (4认同)
  • 正常工作,并且应该成为公认的答案。我唯一注意到的是图像必须是一个完美的正方形(这不是我的情况,因为我有一个带有与约束匹配的可绘制的约束布局)。加布里埃尔一如既往地干得好! (2认同)

Jyo*_*ngh 30

如果您使用该src属性,上述方法似乎不起作用.我做的是将两个图像视图放在一个框架布局内,一个在另一个上面,如下所示:

<FrameLayout android:id="@+id/frame"
             android:layout_width="40dp"
             android:layout_height="40dp">

    <ImageView android:id="@+id/pic"
               android:layout_width="40dp"
               android:layout_height="40dp"
               android:src="@drawable/my_picture" />

    <ImageView android:id="@+id/circle_crop"
               android:layout_width="40dp"
               android:layout_height="40dp"
               android:src="@drawable/circle_crop" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

只需将circular_crop.png放在您的可绘制文件夹中,该文件夹的形状为图像尺寸(在我的情况下为正方形),中间有白色背景和透明圆圈.如果您需要方形图像视图,可以使用此图像.

圆形图像

只需下载上面的图片.

  • 这只适用于白色背景? (8认同)
  • 最佳方案!最棒的表演.更改位图是内存问题 (2认同)
  • 你可以给圆形可绘制的颜色过滤器:circleCrop.setColorFilter(getResources().getColor(R.color.white))。这样,您就可以绘制任何颜色的单个圆圈,并在应用程序中的任何背景上重复使用它。只需每次设置具有适当颜色资源的滤色器即可。 (2认同)

Chi*_*ang 28

借助滑动库和RoundedBitmapDrawableFactory类,它很容易实现.您可能需要创建圆形占位符图像.

滑翔V4:

Glide.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView);
Run Code Online (Sandbox Code Playgroud)

Glide V3:

    Glide.with(context)
        .load(imgUrl)
        .asBitmap()
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.placeholder)
        .into(new BitmapImageViewTarget(imgProfilePicture) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
                        Bitmap.createScaledBitmap(resource, 50, 50, false));
                drawable.setCircular(true);
                imgProfilePicture.setImageDrawable(drawable);
            }
        });
Run Code Online (Sandbox Code Playgroud)

  • 在4.6.1版中使用`.apply(RequestOptions.circleCropTransform())` (3认同)
  • 不知道你在这个例子中使用了什么版本的滑动,但是使用v4.0.0你可以简单地使用他们的RequestOptions而不是RoundedBitmapDrawable:`Glide.with(context).load(imgUrl).apply(new RequestOptions().centerCrop( )).into(imageView)` (2认同)
  • 不是吗?Glide.with(context).load(imgUrl).apply(new RequestOptions().circleCrop()).into(imageView)? (2认同)
  • 在Glide v.4中,如果在应用程序中配置了“ GlideApp”:`GlideApp.with(this).load(url).circleCrop()。into(imageView);`。 (2认同)

Bug*_*ggy 19

以下是最简单的方法之一,使用以下代码:

依赖

dependencies {
    ...
    compile 'de.hdodenhof:circleimageview:2.1.0'      // use this or use the latest compile version. In case u get bug.
}
Run Code Online (Sandbox Code Playgroud)

XML代码

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="96dp"             //  here u can adjust the width 
    android:layout_height="96dp"            //  here u can adjust the height 
    android:src="@drawable/profile"         //  here u can change the image 
    app:civ_border_width="2dp"              //  here u can adjust the border of the circle.  
    app:civ_border_color="#FF000000"/>      //  here u can adjust the border color
Run Code Online (Sandbox Code Playgroud)

截图:

截图

来源: Circular ImageView GitHub存储库

在此输入图像描述

  • 不支持比例类型.:( (6认同)

小智 13

您可以简单地使用 AndroidX ImageFilterView

 <androidx.constraintlayout.utils.widget.ImageFilterView
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginStart="@dimen/margin_medium"
        android:layout_marginBottom="@dimen/margin_medium"
        android:background="@color/white"
        android:padding="@dimen/margin_small"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:roundPercent="1"
        app:srcCompat="@drawable/ic_gallery" />
Run Code Online (Sandbox Code Playgroud)

ImageFilterView 示例图像


Ras*_*iri 9

您不需要任何第三方库。

您可以ShapeableImageView在材料中使用 。

implementation 'com.google.android.material:material:1.2.0'
Run Code Online (Sandbox Code Playgroud)

样式文件

<style name="ShapeAppearanceOverlay.App.CornerSize">
     <item name="cornerSize">50%</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在布局中

<com.google.android.material.imageview.ShapeableImageView
     android:layout_width="100dp"
     android:layout_height="100dp"
     app:srcCompat="@drawable/ic_profile"
     app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize"
/>
Run Code Online (Sandbox Code Playgroud)

你可以看到这个

https://developer.android.com/reference/com/google/android/material/imageview/ShapeableImageView

或这个

https://medium.com/android-beginners/shapeableimageview-material-components-for-android-cac6edac2c0d


Abd*_*pal 6

您可以简单地使用 CardView 而无需任何外部库

  <androidx.cardview.widget.CardView
                    android:id="@+id/roundCardView"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_centerHorizontal="true"
                    android:elevation="0dp"
                    app:cardCornerRadius="20dp">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:src="@drawable/profile" />
</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)


小智 6

发布此答案以供将来参考。您可以使用ShapeableImageView可用的com.google.android.material:material.

<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" />
Run Code Online (Sandbox Code Playgroud)

imageViewRounded在 styles.xml 中添加样式

<style name="imageViewRounded">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">51%</item>
</style>
Run Code Online (Sandbox Code Playgroud)

如果没有添加,您可以添加材料设计依赖项。

    implementation 'com.google.android.material:material:1.4.0'
Run Code Online (Sandbox Code Playgroud)

设计看起来像

在此输入图像描述


小智 5

这将达到目的:

矩形文件

<?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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

您的布局

 <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"/>
Run Code Online (Sandbox Code Playgroud)


FEE*_*LIX 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>```

Run Code Online (Sandbox Code Playgroud)


Hit*_*ahu 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'
Run Code Online (Sandbox Code Playgroud)

XML

<ImageView
    android:id="@+id/vinyl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ContentDescription" />
Run Code Online (Sandbox Code Playgroud)

在代码中

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

在此输入图像描述