开发者

How to make an image corner rounded programmatically

I am using a text view. It has one image 开发者_Go百科as background. How do I programmatically round this image's corner?


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; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜