Is there any way to round the corners of an ImageView
through xml layout
? And even if not, what are the ways we can achieve that?
PS: The image urls
are being fetched from an API. Not from drawable
folder
Is there any way to round the corners of an ImageView
through xml layout
? And even if not, what are the ways we can achieve that?
PS: The image urls
are being fetched from an API. Not from drawable
folder
by using Glide library
use
Glide
.with(context)
.load(url)
.apply(
RequestOptions()
.circleCrop())
.into(imageView)
You can achieve it using custom views like this
public class RoundImageView extends ImageView {
private float mRadius = 18.0f;
private Path mPath;
private RectF mRect;
public RoundImageView(Context context) {
super(context);
init();
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mPath = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
mRect = new RectF(0, 0, this.getWidth(), this.getHeight());
mPath.addRoundRect(mRect, mRadius, mRadius, Path.Direction.CW);
canvas.clipPath(mPath);
super.onDraw(canvas);
}
}
and use it in your XML
<your_pkag.RoundImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image" />
I don't know what you want to make. But for the borders.
You can create a drawable file like below.
line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<stroke android:width="2dp" android:color="#000" />
</shape>
activity_xxx.xml
<ImageView
android:layout_height:"200dp"
android:layout_width:"200dp"
android:background:"@drawable/line.xml"
/>