public static Bitmap clipCircle ( @ NonNull final Bitmap bitmap , final int size , final int borderWidth , @ ColorInt final int borderColor ) { Condition . INSTANCE . ensureAtLeast ( borderWidth , 0 , "The border width must be at least 0" ) ; Bitmap clippedBitmap = clipCircle ( bitmap , size ) ; Bitmap result = Bitmap . createBitmap ( clippedBitmap . getWidth ( ) , clippedBitmap . getHeight ( ) , Bitmap . Config . ARGB_8888 ) ; Canvas canvas = new Canvas ( result ) ; float offset = borderWidth / 2.0f ; Rect src = new Rect ( 0 , 0 , clippedBitmap . getWidth ( ) , clippedBitmap . getHeight ( ) ) ; RectF dst = new RectF ( offset , offset , result . getWidth ( ) - offset , result . getHeight ( ) - offset ) ; canvas . drawBitmap ( clippedBitmap , src , dst , null ) ; if ( borderWidth > 0 && Color . alpha ( borderColor ) != 0 ) { Paint paint = new Paint ( ) ; paint . setFilterBitmap ( false ) ; paint . setAntiAlias ( true ) ; paint . setStrokeCap ( Paint . Cap . ROUND ) ; paint . setStyle ( Paint . Style . STROKE ) ; paint . setStrokeWidth ( borderWidth ) ; paint . setColor ( borderColor ) ; offset = borderWidth / 2.0f ; RectF bounds = new RectF ( offset , offset , result . getWidth ( ) - offset , result . getWidth ( ) - offset ) ; canvas . drawArc ( bounds , 0 , COMPLETE_ARC_ANGLE , false , paint ) ; } return result ; }
Clips the corners of a bitmap in order to transform it into a round shape . Additionally the bitmap is resized to a specific size and a border will be added . Bitmaps whose width and height are not equal will be clipped to a square beforehand .