码迷,mamicode.com
首页 > 移动开发 > 详细

(Android)处理图片成圆形

时间:2015-04-21 19:57:44      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

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

(Android)处理图片成圆形

标签:

原文地址:http://www.cnblogs.com/recwert/p/4444999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!