android课程:canvas画图 切割画布(clipRect)
android canvas画图 切割画布(clipRect)
canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);最后一个参数有多个选择分别是://DIFFERENCE是第一次不同于第二次的部分显示出来
//REPLACE是显示第二次的
//REVERSE_DIFFERENCE 是第二次不同于第一Java代码1.2.3.importandroid.content.Context; 4.importandroid.graphics.Canvas; 5.importandroid.graphics.Color; 6.importandroid.graphics.Paint; 7.importandroid.graphics.Path; 8.importandroid.graphics.Region; 9.importandroid.util.AttributeSet; 10.importandroid.view.View; 11.12.publicclasssBookextendsView{ 13.ContextmContext; 14.PaintmPaint; 15.PathmPath;
16.publicsBook(Contextcontext){ 17.super(context); 18.init(); 19.}
20.21.publicsBook(Contextcontext,AttributeSetattrs){ 22.super(context,attrs); 23.init(); 24.}
25.26.publicsBook(Contextcontext,AttributeSetattrs,intdefStyle){ 27.super(context,attrs,defStyle); 28.init(); 29.}
30.31.privatevoidinit(){ 32.mPaint=newPaint();
33.mPaint.setAntiAlias(true); 34.mPaint.setStrokeWidth(6); 35.mPaint.setTextSize(16);
36.mPaint.setTextAlign(Paint.Align.RIGHT); 37.38.mPath=newPath(); 39.}
40.41.protectedvoidonDraw(Canvascanvas){ 42.canvas.drawColor(Color.GRAY); 43.44.canvas.save();
45.canvas.translate(10,10); 46.drawScene(canvas); 47.canvas.restore();
48.49.canvas.save();
50.canvas.translate(160,10); 51.canvas.clipRect(10,10,90,90);
52.canvas.clipRect(30,30,70,70,Region.Op.XOR); 53.drawScene(canvas); 54.canvas.restore();
55.56.canvas.save();
57.canvas.translate(10,160); 58.mPath.reset();
59.//canvas.clipPath(mPath);//makestheclipempty 60.//mPath.addCircle(50,50,50,Path.Direction.CCW); 61.mPath.cubicTo(0,0,100,0,100,100); 62.mPath.cubicTo(100,100,0,100,0,0);
63.canvas.clipPath(mPath,Region.Op.REPLACE); 64.drawScene(canvas); 65.canvas.restore();
66.67.canvas.save();
68.canvas.translate(160,160); 69.canvas.clipRect(0,0,60,60);
70.canvas.clipRect(40,40,100,100,Region.Op.UNION); 71.drawScene(canvas); 72.canvas.restore();
73.74.canvas.save();
75.canvas.translate(10,310); 76.canvas.clipRect(0,0,60,60);
77.canvas.clipRect(40,40,100,100,Region.Op.XOR); 78.drawScene(canvas); 79.canvas.restore(); 80.81.canvas.save();
82.83.canvas.translate(160,310); 84.canvas.clipRect(0,0,60,60);
85.canvas.clipRect(40,40,100,100,Region.Op.REVERSE_DIFFERENCE); 86.drawScene(canvas); 87.canvas.restore(); 88.}
89.90.privatevoiddrawScene(Canvascanvas){ 91.canvas.clipRect(0,0,100,100); 92.93.canvas.drawColor(Color.WHITE); 94.95.mPaint.setColor(Color.RED);
96.canvas.drawLine(0,0,100,100,mPaint); 97.98.mPaint.setColor(Color.GREEN);
99.canvas.drawCircle(30,70,30,mPaint); 100.101.mPaint.setColor(Color.BLUE);
102.canvas.drawText("Clipping",100,30,mPaint); 103.} 104.}
105.
次的部分显示
//INTERSECT交集显示//UNION全部显示
//XOR补集 就是全集的减去交集生育部分显示