Android自定义View-canvas.drawRect()用法详解

提起自定义view,很让人心烦心累,但是我们又不得不去面对,欠下的总要还的,慢慢学吧.

开发中我们会遇到这种情况,上下滑动不带标题,左右滑动不带左右栏.这个时候如何处理,那么你就要好好学习一下这个canvas.drawRect()的用法,它可以帮助你解决这个问题!言归正传,先看几个示例你就懂了.

一. 首先我们要看canvas.drawRect()方法的最后一个参数(至关重要一个参数)

TablesAre
Region.Op.DIFFERENCE是第一次不同于第二次的部分显示出来
Region.Op.REPLACE只显示第二次的
Region.Op.REVERSE_DIFFERENCE是第二次不同于第一次的部分显示
Region.Op.INTERSECT交集显示
Region.Op.UNION全部显示
Region.Op.XOR(补集)就是全集的减去交集生育部分显示

1.0(默认不剪切的效果)

代码
  /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
  /**
         * 绘制两个矩形
         */
        canvas.drawRect(200, 500, 900, 1500, paint);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果图

这里写图片描述


2.0(DIFFERENCE)

代码
  /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
          /**
         * 绘制两个矩形
         */
        canvas.drawRect(200, 500, 900, 1500, paint);
        RectF rectF = new RectF(200, 500, 900, 1500);
        canvas.clipRect(rectF, Region.Op.DIFFERENCE);
        RectF rects = new RectF(100, 200, 400, 700);
        canvas.clipRect(rects, Region.Op.DIFFERENCE);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果图

这里写图片描述


3.0(REPLACE)

代码
  /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
          /**
         * 绘制两个矩形
         */
        canvas.drawRect(200, 500, 900, 1500, paint);
        RectF rectF = new RectF(200, 500, 900, 1500);
        canvas.clipRect(rectF, Region.Op.REPLACE);
        RectF rects = new RectF(100, 200, 400, 700);
        canvas.clipRect(rects, Region.Op.REPLACE);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果

这里写图片描述


4.0(REVERSE_DIFFERENCE)

代码
   /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
         /**
         * 绘制两个矩形
         */
         canvas.drawRect(200, 500, 900, 1500, paint);
        RectF rectF = new RectF(200, 500, 900, 1500);
        canvas.clipRect(rectF, Region.Op.REVERSE_DIFFERENCE);
        RectF rects = new RectF(100, 200, 400, 700);
        canvas.clipRect(rects, Region.Op.REVERSE_DIFFERENCE);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果

这里写图片描述


5.0(INTERSECT)

  /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
         /**
         * 绘制两个矩形
         */
       canvas.drawRect(200, 500, 900, 1500, paint);
        RectF rectF = new RectF(200, 500, 900, 1500);
        canvas.clipRect(rectF, Region.Op.INTERSECT);
        RectF rects = new RectF(100, 200, 400, 700);
        canvas.clipRect(rects, Region.Op.INTERSECT);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果

这里写图片描述


6.0(UNION)

代码
  /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
         /**
         * 绘制两个矩形
         */
        canvas.drawRect(200, 500, 900, 1500, paint);
        RectF rectF = new RectF(200, 500, 900, 1500);
        canvas.clipRect(rectF, Region.Op.UNION);
        RectF rects = new RectF(100, 200, 400, 700);
        canvas.clipRect(rects, Region.Op.UNION);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果

这里写图片描述


7.0(XOR)

代码
  /**
         * 初始化两个画笔
         */
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint1 = new Paint();
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.FILL);
          /**
         * 绘制两个矩形
         */
        canvas.drawRect(200, 500, 900, 1500, paint);
        RectF rectF = new RectF(200, 500, 900, 1500);
        canvas.clipRect(rectF, Region.Op.XOR);
        RectF rects = new RectF(100, 200, 400, 700);
        canvas.clipRect(rects, Region.Op.XOR);
        canvas.drawRect(100, 200, 400, 700, paint1);
效果

这里写图片描述


好了,遇上不懂的,其实大家刚开始都不懂,那么就多尝试,看效果,就大概了解其真实用途了,还要结合文档注释,这样就可以更加准确确定其用法,没有什么学不会的,只要多尝试.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值