(Android)处理图片成圆形

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即为图片的半径

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。