思路:这里用到的是图像求交。现在画布上画上圆形,即所需要的圆形头像,然后设置paint的属性设置为求交集,再将bitmap划到canvas上面就好了。
代码如下:
1 private static Bitmap getCircleBitmap(Bitmap bitmap) { 2 int width = bitmap.getWidth(); 3 int height = bitmap.getHeight(); 4 int r = width > height ? height : width; 5 Bitmap bmp = Bitmap.createBitmap(width, height, bitmap.getConfig()); 6 Canvas canvas = new Canvas(bmp); 7 Paint paint = new Paint(); 8 paint.setAntiAlias(true); 9 RectF rect = new RectF(0, 0, r, r); 10 canvas.drawRoundRect(rect, r / 2, r / 2, paint); 11 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 12 canvas.drawBitmap(bitmap, null, rect, paint); 13 return bmp; 14 }