step1: 获取裁剪后的圆形图片
public class CircleDrawableUtils {
/**
* 获取裁剪后的圆形图片
*
* @param radius
* 半径
*/
public static Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
Bitmap scaledSrcBmp;
Bitmap squareBitmap;
int diameter = radius * 2;
// 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
int squareWidth = 0, squareHeight = 0;
int x = 0, y = 0;
// 高大于宽
if (bmpHeight > bmpWidth) {
squareWidth = squareHeight = bmpWidth;
x = 0;
y = (bmpHeight - bmpWidth) / 2;
squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, // 截取正方形图片
squareHeight);
}
// 宽大于高
else if (bmpHeight < bmpWidth) {
squareWidth = squareHeight = bmpHeight;
x = (bmpWidth - bmpHeight) / 2;
y = 0;
squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
squareHeight);
}
else
squareBitmap = bmp;
if (squareBitmap.getWidth() != diameter
|| squareBitmap.getHeight() != diameter) {
scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, //进行缩放
diameter, true);
}
else
scaledSrcBmp = squareBitmap;
Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),
scaledSrcBmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),
scaledSrcBmp.getHeight());
//图像处理
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,
paint);
//设置圆形和方形区域相交显示效果
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
bmp = null;
squareBitmap = null;
scaledSrcBmp = null;
return output;
}
}
step2:圆形ImageView
public class CircleImageView extends ImageView {
Path path;
public PaintFlagsDrawFilter mPaintFlagsDrawFilter;// 毛边过滤
Paint paint;
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public CircleImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public void init(){
mPaintFlagsDrawFilter = new PaintFlagsDrawFilter(0,
Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas cns) {
// TODO Auto-generated method stub
float h = getMeasuredHeight()- 3.0f;
float w = getMeasuredWidth()- 3.0f;
if (path == null) {
path = new Path();
path.addCircle(
w/2.0f
, h/2.0f
, (float) Math.min(w/2.0f, (h / 2.0))
, Path.Direction.CCW);
path.close();
}
cns.drawCircle(w/2.0f, h/2.0f, Math.min(w/2.0f, h / 2.0f) + 1.5f, paint);
int saveCount = cns.getSaveCount();
cns.save();
cns.setDrawFilter(mPaintFlagsDrawFilter);
cns.clipPath(path,Region.Op.REPLACE);
cns.setDrawFilter(mPaintFlagsDrawFilter);
cns.drawColor(Color.WHITE);
super.onDraw(cns);
cns.restoreToCount(saveCount);
}
}