android canvas画图 切割画布(clipRect)
canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);最后一个参数有多个选择分别是://DIFFERENCE是第一次不同于第二次的部分显示出来
//REPLACE是显示第二次的
//REVERSE_DIFFERENCE 是第二次不同于第一
Java代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105. importandroid.content.Context; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.Path; importandroid.graphics.Region; importandroid.util.AttributeSet; importandroid.view.View; publicclasssBookextendsView{ ContextmContext; PaintmPaint; PathmPath; publicsBook(Contextcontext){ super(context); init(); } publicsBook(Contextcontext,AttributeSetattrs){ super(context,attrs); init(); } publicsBook(Contextcontext,AttributeSetattrs,intdefStyle){ super(context,attrs,defStyle); init(); } privatevoidinit(){ mPaint=newPaint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(6); mPaint.setTextSize(16); mPaint.setTextAlign(Paint.Align.RIGHT); mPath=newPath(); } protectedvoidonDraw(Canvascanvas){ canvas.drawColor(Color.GRAY); canvas.save(); canvas.translate(10,10); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(160,10); canvas.clipRect(10,10,90,90); canvas.clipRect(30,30,70,70,Region.Op.XOR); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10,160); mPath.reset(); //canvas.clipPath(mPath);//makestheclipempty //mPath.addCircle(50,50,50, W); mPath.cubicTo(0,0,100,0,100,100); mPath.cubicTo(100,100,0,100,0,0); canvas.clipPath(mPath,Region.Op.REPLACE); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(160,160); canvas.clipRect(0,0,60,60); canvas.clipRect(40,40,100,100,Region.Op.UNION); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10,310); canvas.clipRect(0,0,60,60); canvas.clipRect(40,40,100,100,Region.Op.XOR); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(160,310); canvas.clipRect(0,0,60,60); canvas.clipRect(40,40,100,100,Region.Op.REVERSE_DIFFERENCE); drawScene(canvas); canvas.restore(); } privatevoiddrawScene(Canvascanvas){ canvas.clipRect(0,0,100,100); canvas.drawColor(Color.WHITE); mPaint.setColor(Color.RED); canvas.drawLine(0,0,100,100,mPaint); mPaint.setColor(Color.GREEN); canvas.drawCircle(30,70,30,mPaint); mPaint.setColor(Color.BLUE); canvas.drawText("Clipping",100,30,mPaint); } }
次的部分显示
//INTERSECT交集显示
//UNION全部显示
//XOR补集 就是全集的减去交集生育部分显示