在ImageView中设置圆角图像 [英] Set Round Corner image in ImageView

查看:2525
本文介绍了在ImageView中设置圆角图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上搜索了更多内容,并得到了如下类似的建议

I have searched more on websites and got many suggestions like below

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

  • Changing background with custom style to set the corner radius and padding ( Getting image as rectangle and background as rounded corner)

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

Changing Given image to Bitmap by decoding the image and cropping functionality by passing this bitmap and width ( its taking more time to load)

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

请问我能为我推荐最好的方法,如在WhatsApp中用舍入图像创建列表视图模板吗?

Could you please suggest me the best way to create listview template with rounded images as in WhatsApp?

我希望通过在xml页面中设置样式详细信息来用圆角更改图像. (图片的宽度和高度为60 dp,我的列表视图也包含约60个项目)

I am expecting to change the image with rounded corner by setting the style details in xml page. (image width and height is 60 dp, also my listview having around 60 items)

参考文献:

  • https://xjaphx.wordpress.com/2011/06/23/image-processing-image-with-round-corner/
  • How to Make an ImageView in Circular Shape?
  • How to create a circular ImageView in Android?

res/mylayout.xml:

<ImageView
        android:id="@+id/contactssnap"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/contactssnap"
        android:background="@drawable/roundimage" />

res/drawable/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>

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

推荐答案

来自sats的答案有效.但是RoundedBitmapDrawable不能应用于scaleType:centerCrop的ImageView.

The answer from sats is worked. But RoundedBitmapDrawable cannot be applied to an ImageView with scaleType: centerCrop.

更新.

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

If you are using Android API at level 21. We have a simple solution here.

首先,为imageview创建可绘制背景作为背景

1st, create drawable as background for the 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>

接下来,将ImageView的属性setClipToOutline更改为true.并将舍入的可绘制对象设置为ImageView的背景.

Next, change ImageView's property setClipToOutline to true. And set rounded drawable as ImageView's background.

就是这样.

在演示中,我使用此方法对图像进行了四舍五入.可以在这里找到它 cuberto的对话框

In my demo, I using this method for rounded imageview. Can find it here cuberto's dialog

这篇关于在ImageView中设置圆角图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆