Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 354
Android Killer Avatar asked Dec 07 '22 19:12

Android Killer


1 Answers

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; 
}
like image 81
ilango j Avatar answered Dec 21 '22 23:12

ilango j