提起自定义view,很让人心烦心累,但是我们又不得不去面对,欠下的总要还的,慢慢学吧.
开发中我们会遇到这种情况,上下滑动不带标题,左右滑动不带左右栏.这个时候如何处理,那么你就要好好学习一下这个canvas.drawRect()的用法,它可以帮助你解决这个问题!言归正传,先看几个示例你就懂了.
Tables | Are |
---|---|
Region.Op.DIFFERENCE | 是第一次不同于第二次的部分显示出来 |
Region.Op.REPLACE | 只显示第二次的 |
Region.Op.REVERSE_DIFFERENCE | 是第二次不同于第一次的部分显示 |
Region.Op.INTERSECT | 交集显示 |
Region.Op.UNION | 全部显示 |
Region.Op.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);
canvas.drawRect(100, 200, 400, 700, paint1);
/**
* 初始化两个画笔
*/
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);
/**
* 初始化两个画笔
*/
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);
/**
* 初始化两个画笔
*/
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);
/**
* 初始化两个画笔
*/
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);
/**
* 初始化两个画笔
*/
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);
/**
* 初始化两个画笔
*/
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);
好了,遇上不懂的,其实大家刚开始都不懂,那么就多尝试,看效果,就大概了解其真实用途了,还要结合文档注释,这样就可以更加准确确定其用法,没有什么学不会的,只要多尝试.