How To Draw A Circle Around A Bitmap

Excellence is not enough!

How To Draw A Circle Around A Bitmap

Do You Want To Draw A Border Around Your Images Like A Picture Frame , Then Read This.

Let’s Put The Code Right Away:

public static Bitmap roundCorner(Bitmap src , float radius){
        int width = src.getWidth() ;
        int height = src.getHeight() ;
        Bitmap result = Bitmap.createBitmap(width ,height , src.getConfig()) ;

        Canvas canvas = new Canvas(result) ;
        canvas.drawARGB(0 , 0 , 0 , 0);

        final  Paint paint = new Paint() ;
        paint.setAntiAlias(true);
        paint.setColor(Color.CYAN);

        final Rect rect = new Rect(0 , 0 , width , height) ;
        final RectF rectF = new RectF(rect) ;

        canvas.drawRoundRect(rectF , radius , radius , paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)) ;
        canvas.drawBitmap(src , rect , rect , paint);
        return result ;
    }

First I Create A Similar Bitmap To Apply Effect On It .

 int width = src.getWidth() ; 

int height = src.getHeight() ;

 Bitmap result = Bitmap.createBitmap(width ,height , src.getConfig()) ;

Then Creating A Canvas From That Bitmap To Draw Shape On It And Set A White Color On It:

Canvas canvas = new Canvas(result) ; 

canvas.drawARGB(0 , 0 , 0 , 0);

Then  Create A Paint , Rect And RectF From Rect  Objects To Draw My Circle Around.

Now It’s Time To Draw The Shape  :

canvas.drawRoundRect(rectF , radius , radius , paint);

Now We Have A Image That Has A Round Frame.

For The Final Phase Just Need To Draw Our Original Bitmap On The Canvas:

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)) ; 

canvas.drawBitmap(src , rect , rect , paint); 

return result ;

Just That .