先看下未设置正常的图片:
设置圆形头像后的效果:
布局很简单,就一个ImageVIew,直接看下代码:
ImageView mIv = (ImageView) findViewById(R.id.iv);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.head);
//获取bitmap原始的大小
int width = bitmap.getWidth();
int height = bitmap.getHeight();
//创建一张空白的bitmap
Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//获取此张bitmap的画布
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
//画一圆
float cx = width * 1.0f / 2;
float cy = height * 1.0f / 2;
float radius = height > width ? width * 1.0f / 2 : height * 1.0f / 2;
canvas.drawCircle(cx, cy, radius, paint);
//这次画的图和以前画的图取交集显示
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
mIv.setImageBitmap(newBitmap);