Create A Custom ImageView Android

Narayan Panthi
May 26, 2019

--

Rounded Corner ImageView Android without a library.

Here, You will find a JAVA Class that will help you to create a custom image view.

public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
public static float radius = 10.0f;

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

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

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

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

You can draw any canvas and use it in your XML as.

<CustomImageView
android:id="@+id/custom_image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

That’s all.

Thank you ⛳️

--

--

Narayan Panthi
Narayan Panthi

Written by Narayan Panthi

Software Engineer | Writes about Android |🇳🇵

No responses yet