0

I have found a lot of posts about rounded rect for ImageViews in Android.

Currently I am using this solution: How to make an ImageView with rounded corners?

However, with various bitmap sizes, the rounded corner varies heavily depending on the sources size. I need constant rounded corners throughout the app, not depending on the bitmap size.

Does anyone know a solution to that?

Community
  • 1
  • 1
X.X_Mass_Developer
  • 717
  • 1
  • 8
  • 23

2 Answers2

0

this RoundedImageView Library works for me. I just set the corner radius as a dimension in the dimens.xml file and go from there with

<com.makeramen.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/imageView1"
        android:src="@drawable/photo1"
        android:scaleType="centerCrop"
        app:corner_radius="@dimen/mycorner_radius_in_dp"
        app:border_width="2dip"
        app:border_color="#333333"
        app:round_background="true"
        app:is_oval="true" />

it doesnt care about the image size

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
0

make a java file which you will use to create circular view.

public class CircularImageView extends ImageView {

//you can change the radius to modify the circlur shape into oval or rounded rectangle

public static float radius = 100.0f;

public CircularImageView(Context context) {
super(context);
}

public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}

After this use this java file in your layout like this:

<com.yourpackagename.CircularImageView
android:id="@+id/logo_ngo"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/circle"
android:src="@drawable/file_logo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="@string/content_description"
android:layout_margin="@dimen/medium_margin"
android:scaleType="centerCrop"/>

Here file_logo is imagefile AND circle is filename which you will use as xml below:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:shape="oval">

<stroke android1:width="1dp"
        android1:color="#000" >
</stroke>

<size
    android1:width="50dp"
    android1:height="50dp" >
</size>

<corners android1:radius="1dp" >
</corners>

</shape>

This worked for me, if anyone is having better solution kindly share yours too.

amit pandya
  • 1,384
  • 13
  • 22