Android将一张Bitmap处理成圆形是十分常见的,经常见的场合就是作为用户头像,我们可以Canvas来辅助实现这个功能,代码如下
1 public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { 2 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 3 Canvas canvas = new Canvas(output); 4 final int color = 0xff424242; 5 final Paint paint = new Paint(); 6 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 7 final RectF rectF = new RectF(rect); 8 final float roundPx = pixels; 9 paint.setAntiAlias(true); 10 canvas.drawARGB(0, 0, 0, 0); 11 paint.setColor(color); 12 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 13 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 14 canvas.drawBitmap(bitmap, rect, rect, paint); 15 return output; 16 }
pixels即为图片的半径
原文:http://www.cnblogs.com/recwert/p/4444999.html