一个通过Canvas和Paint截取无锯齿圆形图片。
/** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public static Bitmap createCircleImage(Bitmap source, int min) { Paint paint = new Paint(); // paint.setStyle(Paint.Style.STROKE); // paint.setFlags(Paint.ANTI_ALIAS_FLAG); paint.setAntiAlias(true); paint.setFilterBitmap(true); Bitmap target = Bitmap.createBitmap(min, min, source.getConfig()); /** * 产生一个同样大小的画布 */ Canvas canvas = new Canvas(target); /** * 首先绘制圆形 */ canvas.drawCircle(min / 2, min / 2, min / 2, paint); /** * 使用SRC_IN */ paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); /** * 给Canvas加上抗锯齿标志 */ canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG)); /** * 绘制图片 */ canvas.drawBitmap(source, 0, 0, paint); return target; }