public static Bitmap getCircleBitmap( Bitmap sourceBitmap) {
if(null == sourceBitmap) {
return null;
}
Bitmap output = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(sourceBitmap.getWidth() / 2, sourceBitmap.getHeight() / 2, sourceBitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sourceBitmap, rect, rect, paint);
return output;
}
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap) {
if (null == bitmap) {
return null;
}
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
// 得到画布
Canvas canvas = new Canvas(output);
// 将画布的四角圆化
final int color = Color.RED;
final Paint paint = new Paint();
// 得到与图像相同大小的区域 由构造的四个值决定区域的位置以及大小
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
// 值越大角度越明显
float desity = context.getResources().getDisplayMetrics().density;
final float roundPx = 20 * desity;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// drawRoundRect的第2,3个参数一样则画的是正圆的一角,如果数值不同则是椭圆的一角
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
圆角图标