1xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
2
3
4//通过Xfermode的DST_OUT来产生中间的透明裁剪区域,一定要另起一个Layer(层)
5canvas.saveLayer(0, 0, this.getWidth(), this.getHeight(), null, Canvas.ALL_SAVE_FLAG);
6
7//设置背景
8canvas.drawColor(Color.parseColor(“#a8000000”));
9paint.setXfermode(xfermode);
圆形裁剪框的实现
绘制圆形裁剪框很容易实现,主要确定圆心和半径即可
1//中间的透明的圆
2canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, clipRadiusWidth, paint);
3//白色的圆边框
4canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, clipRadiusWidth, borderPaint);
正方形裁剪框的实现
绘制长方形的话主要要确定四个点的坐标 left ,top, right, botom。很简单
1left = mHorizontalPadding;
2top = this.getHeight() / 2 - clipWidth / 2;
3right = this.getWidth() - mHorizontalPadding;
4botom = this.getHeight() / 2 + clipWidth / 2;
1//绘制中间白色的矩形蒙层
2canvas.drawRect(mHorizontalPadding, this.getHeight() / 2 - clipWidth / 2,
3 this.getWidth() - mHorizontalPadding, this.getHeight() / 2 + clipWidth / 2, paint);
4//绘制白色的矩形边框
5canvas.drawRect(mHorizontalPadding, this.getHeight() / 2 - clipWidth / 2,
6 this.getWidth() - mHorizontalPadding, this.getHeight() / 2 + clipWidth / 2, borderPaint);
九宫格的
九宫格的绘制稍微繁琐一点,分三个步骤
- 绘制长方形边框
- 绘制九宫格引导线
- 绘制裁剪边框的是个直角
绘制长方形边框的这里就不说了,比较简单。
我们来看一下绘制九宫格引导线的
- 绘制竖直方向两条线
- 绘制水平方向两条线
1private void drawGuidelines(@NonNull Canvas canvas, Rect clipRect) {
2
3 final float left = clipRect.left;
4 final float top = clipRect.top;
5 final float right = clipRect.right;
6 final float bottom = clipRect.bottom;
7
8 fi