绘制QQ圆形图像

时间:2022-11-12 07:59:11

思路:这里用到的是图像求交。现在画布上画上圆形,即所需要的圆形头像,然后设置paint的属性设置为求交集,再将bitmap划到canvas上面就好了。

代码如下:

     private static Bitmap getCircleBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int r = width > height ? height : width;
Bitmap bmp = Bitmap.createBitmap(width, height, bitmap.getConfig());
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setAntiAlias(true);
RectF rect = new RectF(0, 0, r, r);
canvas.drawRoundRect(rect, r / 2, r / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, null, rect, paint);
return bmp;
}