Rounded ImageView not getting displayed properly

2020-07-22 20:10发布

I am trying to make a rounded Image View but I am getting an improper output. I am attaching the photo

Output for Rounded Image View

These are the xml files used for this

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>

The image view for this is:

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

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-07-22 20:37

you can use external lib like

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

Or use custom as

<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>
查看更多
The star\"
3楼-- · 2020-07-22 20:40

You can use CirlcleImageView library for rounded ImageView:

compile this libray

 compile 'de.hdodenhof:circleimageview:2.2.0'

Usage

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

for more information follow this link

查看更多
乱世女痞
4楼-- · 2020-07-22 20:48

Try this

  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);
        }
    });
查看更多
登录 后发表回答