1. 实现圆角
使用ClipPath先绘制圆角的path,然后再裁剪
1
2
3
4
5
6
7
8
9
10
11
protected void onDraw(Canvas canvas) {
clipPath.reset();
rectF.set(0, 0, getWidth(), getHeight());
clipPath.addRoundRect(rectF, radius, radius, Path.Direction.CW);
// int save = canvas.save();
canvas.clipPath(clipPath);
canvas.drawRect(rectF, paint);
super.onDraw(canvas);
// canvas.restoreToCount(save);
}paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
paint设置Xfermode取交集。1
2
3
4
5
6
7
8
9
10
11
12
13Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, width, height);
// 设置圆形
// canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, paint);
// 设置圆角
canvas.drawRoundRect(rect, mRadius, mRadius, paint);
// 核心代码取两个图片的交集部分
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;