圆角 ImageView 显示不正常

3

我正在尝试制作一个圆形的图像视图,但输出结果不正确。我附上了照片。

Output for Rounded Image View

这些是用于此的 XML 文件

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

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_add_photo"/>

</layer-list>

这个的图片视图是:

<ImageView
    android:id="@+id/iv_dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/iv_person"
    android:layout_below="@+id/iv_person"
    android:layout_marginTop="37dp"
    android:adjustViewBounds="true"
    android:background="@drawable/img"
    android:cropToPadding="true" />

将 android:width="10dp" 改为 android:width="1dp" 并查看变化。 - Nabin
https://dev59.com/w2Eh5IYBdhLWcg3wtFX4#41397749 - Bugs Buggy
3个回答

6

您可以使用 CirlcleImageView 库来创建圆形的 ImageView

编译此库

 compile 'de.hdodenhof:circleimageview:2.2.0'

用法

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

如需了解更多信息,请点击此链接


0

你可以使用外部库,比如

dependencies {
    ...
    compile 'de.hdodenhof:circleimageview:2.1.0'
}

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

或者使用自定义的方式

<shape
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1"
    android:useLevel="false" >

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

    <stroke
        android:width="100dp"
        android:color="#FFFFFFFF" />
</shape>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/your_image" />
    <item android:drawable="@drawable/circle" />
</layer-list>

-1

试试这个

  you can use 'Glide'

  compile 'com.github.bumptech.glide:glide:3.7.0'

  basically it is a network library but you can also use this for circular imageView and gif it have a very good memory management


  ImageView img=(ImageView)findViewById(R.id.img_test);

   Glide.with(this).load("your image path").asBitmap().placeholder(R.drawable.logo_bk).centerCrop().into(new BitmapImageViewTarget(img) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(this.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            img.setImageDrawable(circularBitmapDrawable);
        }
    });

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