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.