代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public Bitmap getRoundBitmap(Bitmap bitmap) 
{
int d = Math.min(bitmap.getWidth(), bitmap.getHeight());

Bitmap output = Bitmap.createBitmap(d, d, Config.ARGB_8888);
Canvas canvas = new Canvas(output);

int r = d / 2;

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, d, d);
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(0xFFFFFFFF);
canvas.drawRoundRect(rectF, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

// 加上白边
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.WHITE);
p.setStyle(Style.STROKE);
p.setStrokeWidth(r/25);
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawCircle(r, r, r, p);

return output;
}

处理前

处理前

处理后

处理后