在ImageView中设置圆角图像

Pon*_*lar 7 android android-listview android-xml android-imageview

我在网站上搜索了更多,并得到了如下的许多建议

  • 使用自定义样式更改背景以设置角半径和填充(将图像设置为矩形,将背景设置为圆角)

  • 通过传递此位图和宽度(它需要更多时间加载)来解码图像和裁剪功能,从而将给定图像更改为位图

我查过了WhatsApp Chat history list and it has rounded corner images but its loaded with minimum time.

你能否建议我用WhatsApp创建带有圆形图像的listview模板的最佳方法?

我希望通过在xml页面中设置样式详细信息更改带圆角的图像.(图像宽度和高度是60 dp,我的listview也有大约60项)

参考文献:

RES/mylayout.xml:

<ImageView
        android:id="@+id/contactssnap"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/contactssnap"
        android:background="@drawable/roundimage" />
Run Code Online (Sandbox Code Playgroud)

RES /绘制/ roundimage.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ffffffff" />
    <stroke
        android:width="2dp"
        android:color="#90a4ae" />
    <size
        android:height="50dp"
        android:width="50dp" />
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
    <corners android:radius="100dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

注意:支持的Android版本从14到22

Gab*_*tti 21

材料组件库提供了新的ShapeableImageView.
只需使用如下布局:

  <com.google.android.material.imageview.ShapeableImageView
      ...
      app:shapeAppearanceOverlay="@style/roundedCorners"
      app:srcCompat="@drawable/...." />
Run Code Online (Sandbox Code Playgroud)

使用该shapeAppearanceOverlay属性,您可以应用自定义形状,在本例中为圆角:

  <style name="roundedCorners" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">xxdp</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

  • 这是我见过的最优雅的方法之一,也非常适用于毕加索错误/占位符! (3认同)

Cuo*_* Vo 11

sats的答案是有效的.但是RoundedBitmapDrawable似乎无法应用于scaleType为centerCrop的ImageView.

更新.

如果您在21级使用Android API.我们在这里有一个简单的解决方案.

1,创建drawable作为imageview的背景

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="@android:color/white" />

<stroke
    android:width="1dp"
    android:color="@color/colorWhite" />
</shape>
Run Code Online (Sandbox Code Playgroud)

接下来,将ImageView的属性setClipToOutline更改为true.并将roundled drawable设置为ImageView的背景.

而已.

在此输入图像描述

在我的演示中,我使用此方法进行圆形imageview.可以在cuberto的对话框中找到它


Mos*_*adi 7

将imageView放入CardView并为CardView设置CornerRadius

<android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp">

        <ImageView
            android:id="@+id/imgTourismHr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY" />

    </android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)


Cod*_*fee 7

查看带有圆角边框的圆角矩形图像输出

现在设置带有圆角边框的圆角矩形图像。

您可以使用app:cardCornerRadius="30dp"app:cardUseCompatPadding="true"app:cardElevation="0dp"在 CardView 中简单地声明 CardView 。

请参阅下面的 XML 代码

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                app:cardCornerRadius="30dp"
                app:cardUseCompatPadding="true"
                app:cardElevation="0dp"
                android:layout_margin="5dp"
                >
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/temp"
                    android:scaleType="fitXY"
                    android:layout_gravity="center"
                    />
            </androidx.cardview.widget.CardView>
        </androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)


sat*_*ats 5

您可以使用Google提供的标准API创建圆形ImageView。

ImageView imageView = (ImageView) findViewById(R.id.circleImageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCircular(true);
imageView.setImageDrawable(roundedBitmapDrawable);
Run Code Online (Sandbox Code Playgroud)

另请参阅https://www.youtube.com/watch?v=zVC-YAnDlgk


Ham*_*eza 0

你检查过android中的9个补丁技术吗?

如果您正在尝试创建类似带有圆角的应用程序气泡聊天之类的内容,我建议您使用此技术而不是使用 XML 位图解码器。

这是一个例子:链接

根据此示例,您可以使用 9 个补丁生成器来创建您自己的自定义形状和位图图像,这些形状和位图图像会自动调整大小以适应视图内容和屏幕大小。图像的选定部分基于图像内绘制的指示符水平或垂直缩放。

在此输入图像描述