android画图--切割区域

android切割画布的历程不算很难,可是理解起来也比较麻烦,这里写一下我的理解 但是不一定正确: 
canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);最后一个参数有多个选择分别是://DIFFERENCE是第一次不同于第二次的部分显示出来 
            //REPLACE是显示第二次的 
            //REVERSE_DIFFERENCE 是第二次不同于第一次的部分显示 
            //INTERSECT交集显示 
            //UNION全部显示 
            //XOR补集 就是全集的减去交集剩余部分显示 
首先还是继承一个view,在 构造函数里面初始化一个画笔 笔宽为6 文本大小16 文本对齐方式 居右。 
mPaint = new Paint(); 
            mPaint.setAntiAlias(true); 
            mPaint.setStrokeWidth(6); 
            mPaint.setTextSize(16); 
            mPaint.setTextAlign(Paint.Align.RIGHT); 
            
            mPath = new Path(); 

而在接口onDraw中 画了好几个图,每个图都进行了裁剪 
@Override protected void onDraw(Canvas canvas) { 
            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); // makes the clip empty 
             mPath.addCircle(50, 50, 50, Path.Direction.CCW); 
            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(); 
        } 
// 建立了一个方法,用于显示所画出的效果 
private void drawScene(Canvas canvas) { 
            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); 
        }

你可能感兴趣的:(android画图--切割区域)