android clipPath切割画布

(转自:http://wallage.blog.163.com/blog/static/1738962420101012439991/)

 

 

 

[java] view plain copy
  1. public class PictureTestActivity extends Activity {  
  2.    
  3.        @Override  
  4.        public void onCreate(Bundle savedInstanceState){  
  5.            super.onCreate(savedInstanceState);  
  6.            setContentView(new SampleView(this));  
  7.        }  
  8.         private static class SampleView extends View {  
  9.             private Paint mPaint;  
  10.             private Path mPath;  
  11.             public SampleView(Context context) {  
  12.                 super(context);  
  13.                 setFocusable(true);  
  14.                   
  15.                 mPaint = new Paint();  
  16.                 mPaint.setAntiAlias(true);  
  17.                 mPaint.setStrokeWidth(6);  
  18.                 mPaint.setTextSize(16);  
  19.                 mPaint.setTextAlign(Paint.Align.RIGHT);  
  20.                   
  21.                 mPath = new Path();  
  22.             }  
  23.               
  24.             private void drawScene(Canvas canvas) {  
  25.                 canvas.clipRect(00100100);  
  26.                   
  27.                 canvas.drawColor(Color.WHITE);  
  28.                   
  29.                 mPaint.setColor(Color.RED);  
  30.                 canvas.drawLine(00100100, mPaint);  
  31.                   
  32.                 mPaint.setColor(Color.GREEN);  
  33.                 canvas.drawCircle(307030, mPaint);  
  34.                   
  35.                 mPaint.setColor(Color.BLUE);  
  36.                 canvas.drawText("Clipping"10030, mPaint);  
  37.             }  
  38.               
  39.             @Override   
  40.             protected void onDraw(Canvas canvas) {  
  41.                 canvas.drawColor(Color.GRAY);              
  42.                 canvas.save();  
  43.                 canvas.translate(1010);  
  44.                 drawScene(canvas);  
  45.                 canvas.restore();  
  46.                   
  47.                 canvas.save();  
  48.                 canvas.translate(16010);  
  49.                 canvas.clipRect(10109090);  
  50.                 canvas.clipRect(30307070, Region.Op.DIFFERENCE);  
  51.                 drawScene(canvas);  
  52.                 canvas.restore();  
  53.                   
  54.                 canvas.save();  
  55.                 canvas.translate(10160);  
  56.                 mPath.reset();  
  57.                 canvas.clipPath(mPath); // makes the clip empty  
  58.                 mPath.addCircle(505050, Path.Direction.CCW);  
  59.                 canvas.clipPath(mPath, Region.Op.REPLACE);  
  60.                 drawScene(canvas);  
  61.                 canvas.restore();  
  62.                   
  63.                 canvas.save();  
  64.                 canvas.translate(160160);  
  65.                 canvas.clipRect(006060);  
  66.                 canvas.clipRect(4040100100, Region.Op.UNION);  
  67.                 drawScene(canvas);  
  68.                 canvas.restore();  
  69.                   
  70.                 canvas.save();  
  71.                 canvas.translate(10310);  
  72.                 canvas.clipRect(006060);  
  73.                 canvas.clipRect(4040100100, Region.Op.XOR);  
  74.                 drawScene(canvas);  
  75.                 canvas.restore();  
  76.                   
  77.                 canvas.save();  
  78.                 canvas.translate(160310);  
  79.                 canvas.clipRect(006060);  
  80.                 canvas.clipRect(4040100100, Region.Op.REVERSE_DIFFERENCE);  
  81.                 drawScene(canvas);  
  82.                 canvas.restore();  
  83.             }  
  84.         }  
  85. }  

 

效果如图:

 

 

 

 

canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);最后一个参数有多个选择分别是:

            //DIFFERENCE是第一次不同于第二次的部分显示出来
            //REPLACE是显示第二次的
            //REVERSE_DIFFERENCE 是第二次不同于第一次的部分显示
            //INTERSECT交集显示
            //UNION全部显示
            //XOR补集 就是全集的减去交集生育部分显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值