SoFunction
Updated on 2025-04-07

Three ways to implement image rounded corners in Android

In Android development, images are often processed repeatedly, such as adding rounded corner effects or displaying round pictures;

Method 1

Set the rounded corner effect through the third-party framework Glide;

Writing method 1:

RequestOptions options = new RequestOptions().error(.img_load_failure).bitmapTransform(new RoundedCorners(30));//The rounded corners of the picture are 30(this).load(URL) //Picture address                .apply(options)
                .into(ImagView);

Writing method 2:

RequestOptions requestOptions = new RequestOptions();
(.ic_launcher_background);
();
( new RoundedCorners(30));
(this).load(URL) //Picture address                .apply(options)
                .into(ImagView);

Writing method 3:

RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30)); 
(this).load(URL) //Picture address                .apply(options)
                .into(ImagView);
public class RoundTransform extends BitmapTransformation { 
    private static float radius = 0f; 
    public RoundTransform(Context context) { 
        this(context, 4); 
    } 
   
    public RoundTransform(Context context, int dp) { 
        super(context); 
         = ().getDisplayMetrics().density * dp; 
    } 
   
    @Override 
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
        Bitmap bitmap = (pool, toTransform, outWidth, outHeight); 
        return roundCrop(pool, bitmap); 
    } 
   
    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { 
        if (source == null) return null; 
        Bitmap result = ((), (), .ARGB_8888); 
        if (result == null) { 
            result = ((), (), .ARGB_8888); 
        } 
   
        Canvas canvas = new Canvas(result); 
        Paint paint = new Paint(); 
        (new BitmapShader(source, , )); 
        (true); 
        RectF rectF = new RectF(0f, 0f, (), ()); 
        (rectF, radius, radius, paint); 
        return result; 
    } 
   
    public String getId() { 
        return getClass().getName() + (radius); 
    } 
   
    @Override 
    public void updateDiskCacheKey(MessageDigest messageDigest) { 
   
    }
}

Method 2

Custom ImageView Set the rounded corner effect;

<ImageView
        android:
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        />
ImageView iv = findViewById(); 
Bitmap bitmap =(getResources(), );
        Bitmap outBitmap =getRoundBitmapByShader(bitmap, 500,300,20, 3);
        (outBitmap);
public class RoundRectImageView extends ImageView{
 
    private Paint paint;
 
    public RoundRectImageView(Context context) {
        this(context,null);
    }
 
    public RoundRectImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
 
    public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint  = new Paint();
    }
 
    /**
      * Draw a rounded rectangle picture
      */
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null != drawable) {
            Bitmap bitmap = getBitmapFromDrawable(drawable);
//            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0);
            final Rect rectSrc = new Rect(0, 0, (), ());
            final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
            ();
            (b, rectSrc, rectDest, paint);
 
        } else {
            (canvas);
        }
    }
 
    /**
      * Convert resource images to Bitmap
      * @param drawable
      * Resource Pictures
      * @return bitmap
      */
    public static Bitmap getBitmapFromDrawable(Drawable drawable) {
        int width = ();
        int height = ();
        Bitmap bitmap = (width, height, drawable
                .getOpacity() !=  ? .ARGB_8888
                : .RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //(-4, -4, width + 4, height + 4);
        (canvas);
        return bitmap;
    }
 
    public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
        if (bitmap == null) {
            return null;
        }
        int width = ();
        int height = ();
        float widthScale = outWidth * 1f / width;
        float heightScale = outHeight * 1f / height;
 
        Matrix matrix = new Matrix();
        (widthScale, heightScale);
        //Create the output bitmap        Bitmap desBitmap = (outWidth, outHeight, .ARGB_8888);
        //Create canvas and pass in desBitmap, so that the drawn content will be on desBitmap        Canvas canvas = new Canvas(desBitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //Create a shader        BitmapShader bitmapShader = new BitmapShader(bitmap, , );
        //Configure matrix for shaders        (matrix);
        (bitmapShader);
        //Create a rectangular area and reserve a border        RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
        //Draw the incoming bitmap into the rounded rectangle area        (rect, radius, radius, paint);
 
        if (boarder &gt; 0) {
            //Draw the boarder            Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            ();
            ();
            (boarder);
            (rect, radius, radius, boarderPaint);
        }
        return desBitmap;
    }
 
}

Method 3

Process the image and add borders;

/**
  * Implement circular borders through BitmapShader
  * @param bitmap
  * @param outWidth The image width of the output
  * @param outHeight The image height of the output
  * @param radius rounded corner size
  * @param boarder Border width
  */
public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
    if (bitmap == null) {
        return null;
    }
    int height = ();
    int width = ();
    
    float widthScale = outWidth * 1f / width;
    float heightScale = outHeight * 1f / height;
 
    Matrix matrix = new Matrix();
    (widthScale, heightScale);
    //Create the output bitmap    Bitmap desBitmap = (outWidth, outHeight, .ARGB_8888);
    //Create canvas and pass in desBitmap, so that the drawn content will be on desBitmap    Canvas canvas = new Canvas(desBitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //Create a shader    BitmapShader bitmapShader = new BitmapShader(bitmap, , );
    //Configure matrix for shaders    (matrix);
    (bitmapShader);
    //Create a rectangular area and reserve a border    RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
    //Draw the incoming bitmap into the rounded rectangle area    (rect, radius, radius, paint);
 
    if (boarder &gt; 0) {
        //Draw the boarder        Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ();
        ();
        (boarder);
        (rect, radius, radius, boarderPaint);
    }
    return desBitmap;
}

Implement circles and borders:

/**
  * Implement circular borders through BitmapShader
  * @param bitmap
  * @param outWidth The image width of the output
  * @param outHeight The image height of the output
  * @param boarder Border size
  */
public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {
int radius;
int width = ();
int height = ();
float widthScale = outWidth * 1f / width;
float heightScale = outHeight * 1f / height;
 
Bitmap desBitmap = (outWidth, outHeight, .ARGB_8888);
if (outHeight &gt; outWidth) {
    radius = outWidth / 2;
} else {
    radius = outHeight / 2;
}
//Create canvasCanvas canvas = new Canvas(desBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader bitmapShader = new BitmapShader(bitmap, , );
Matrix matrix = new Matrix();
(widthScale, heightScale);
(matrix);
(bitmapShader);
(outWidth / 2, outHeight / 2, radius - boarder, paint);
if (boarder &gt; 0) {
    //Draw the boarder    Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ();
    ();
    (boarder);
    (outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);
}
return desBitmap;
}

This is the end of this article about the three methods of implementing rounded images in Android. For more related content on Android, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!