How to make an ImageView with rounded corners?

In Android, ImageView is a rectangle by default. How to make it a rounded rectangle in ImageView (cut all four corners of Bitmap into rounded rectangles)?

#1 building

Thank you very much for answering first. This is a modified version that converts a rectangular image to a square image (and rounds it) with a fill color as a parameter.

public static Bitmap getRoundedBitmap(Bitmap bitmap, int pixels, int color) {

    Bitmap inpBitmap = bitmap;
    int width = 0;
    int height = 0;
    width = inpBitmap.getWidth();
    height = inpBitmap.getHeight();

    if (width <= height) {
        height = width;
    } else {
        width = height;

    Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(inpBitmap, rect, rect, paint);

    return output;

#2 building

Why not crop draw()?

This is my solution:

  • Extend RelativeLayout with crop
  • Place ImageView (or other views) in the layout:


public class RoundRelativeLayout extends RelativeLayout {

    private final float radius;

    public RoundRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray attrArray = context.obtainStyledAttributes(attrs,
        radius = attrArray.getDimension(
                R.styleable.RoundRelativeLayout_radius, 0);

    private boolean isPathValid;
    private final Path path = new Path();

    private Path getRoundRectPath() {
        if (isPathValid) {
            return path;


        int width = getWidth();
        int height = getHeight();
        RectF bounds = new RectF(0, 0, width, height);

        path.addRoundRect(bounds, radius, radius, Direction.CCW);
        isPathValid = true;
        return path;

    protected void dispatchDraw(Canvas canvas) {

    public void draw(Canvas canvas) {

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int oldWidth = getMeasuredWidth();
        int oldHeight = getMeasuredHeight();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newWidth = getMeasuredWidth();
        int newHeight = getMeasuredHeight();
        if (newWidth != oldWidth || newHeight != oldHeight) {
            isPathValid = false;


#3 building

While the above answers are valid, Romain Guy (Android's core developer) shows it on his blog A better way , the Method Use less memory by using shaders instead of creating copies of bitmaps. The basic points of this function are as follows:

BitmapShader shader;
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

Paint paint = new Paint();

RectF rect = new RectF(0.0f, 0.0f, width, height);

// rect contains the bounds of the shape
// radius is the radius in pixels of the rounded corners
// paint contains the shader that will texture the shape
canvas.drawRoundRect(rect, radius, radius, paint);

Compared with other methods, it has the following advantages:

  • A separate copy of the bitmap is not created because it consumes a lot of memory and large images [compared to most of the other answers here]
  • Anti aliasing support [vs clipPath method]
  • alpha [vs xfermode + porterduff method] supported
  • Support hardware acceleration [vs clipPath method]
  • You can only paint once on the canvas [vs xfermode and clippath methods]

Based on this code, I created RoundedImageView , which wraps the logic into ImageView and adds the appropriate ScaleType support and optional rounded borders.

#4 building

Here's Roman guy.

The smaller version is as follows.

Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.image)).getBitmap();

Bitmap bitmapRounded = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(bitmapRounded);
Paint paint = new Paint();
paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect((new RectF(0.0f, 0.0f, bitmap.getWidth(), bitmap.getHeight())), 10, 10, paint);


#5 building

Apply the shape to imageView as follows:

<shape xmlns:android=""
    android:shape="rectangle" >
    <solid android:color="#faf5e6" />
        android:color="#808080" />
    <corners android:radius="15dp" />
        android:top="5dp" />

This may help your friends.

Tags: Android less

Posted on Sun, 12 Jan 2020 01:01:37 -0800 by frizzo