Thursday , November 21 2024

How To Make Circular Imageview In Android

In this tutorial we are going to learn to make circular ImageView in android. The default ImageView in android is rectangle so there are situations where we will like to create a circular ImageView in android.

In this tutorial we will create a custom class to round a Imageview. And you have to use the package name of this class in your xml file instead of simple Imageview.

1) Fistly we have to make a custom class which extends with Imageview class

public class RoundedImageView extends ImageView {

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

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

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

@Override
protected void onDraw(Canvas canvas) {
float radius = 10.0f; // angle of round corners
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);
}
}

2) To use it in XML, simply do it like this:

<my.package.RoundedImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage" />

 

About admin

Check Also

Binding JavaScript and Android Code – Example

When developing a web application that’s designed specifically for the WebView in your Android application, …

Leave a Reply