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;
}
精彩评论