Skip to content Skip to sidebar Skip to footer

How To Make An Image Corner Rounded Programmatically

I am using a text view. It has one image as background. How do I programmatically round this image's corner?

Solution 1:

Convert your image to bitmap and then convert that bitmap with rounded corners bitmap. Finally apply that bitmap to your textview background. The below code is for convert bitmap to rounded bitmap image.

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,int roundPixelSize) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = roundPixelSize;
    paint.setAntiAlias(true);
    canvas.drawRoundRect(rectF,roundPx,roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint); 
    return output; 
}

Post a Comment for "How To Make An Image Corner Rounded Programmatically"