public static Bitmap drawCircle(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
第一个参数bitmap是目标处理图片(本地图片可用bitmapFactory转成bitmap,这里经常OOM,f@ck)。
第二个参数是圆角半径,按需求来设定。这里提示一下,如果要改成圆形,半径roundPx == bitmap.width() (当然这里是正方形的时候,长方形的时候就选择最小的边作为半径。不明白的可以脑补一下)
效果如下: