Canvas---区域(Range)
来源:互联网 发布:手机编程游戏 编辑:程序博客网 时间:2024/06/05 07:34
构造Region
构造Region有4个构造函数
public Region() //创建一个空的区域 public Region(Region region) //拷贝一个region的范围 public Region(Rect r) //创建一个矩形的区域 public Region(int left, int top, int right, int bottom) //创建一个矩形的区域
第一个需要和其他函数配合使用;第二个表示根据其他的Region复制一个Region;第三个和第四个是最常用的,根据矩形或者左上和右下两个点创造的矩形区域。
2.简介构造
public void setEmpty() //置空 public boolean set(Region region) public boolean set(Rect r) public boolean set(int left, int top, int right, int bottom) public boolean setPath(Path path, Region clip)//后面单独讲
Region提供了一系列的set方法,含义如下:
SetEmpty():从某种意义上讲置空也是一个构造函数,即将原来的一个区域变量变成了一个空变量,要再利用其它的Set方法重新构造区域。
set(Region region):利用新的区域值来替换原来的区域
set(Rect r):利用矩形所代表的区域替换原来的区域
set(int left, int top, int right, int bottom):同样,根据矩形的两个点构造出矩形区域来替换原来的区域值
setPath(Path path, Region clip):根据路径的区域与某区域的交集,构造出新区域
3.使用setPath构造不规则的区域
boolean setPath(Path path,Region clip)
path为构造区域的路径;clip与path与构造的路径的交集构成区域。
下面是一个简单的demo,先构造一个椭圆路径,然后在SetPath时,传进去一个比Path小的矩形区域,让它们两个取交集
Path path=new Path(); RectF rectF=new RectF(50,400,250,800); path.addOval(rectF, Path.Direction.CW); Region region=new Region(); region.setPath(path,new Region(50,400,250,600)); RegionIterator iter = new RegionIterator(region); Rect r = new Rect(); while (iter.next(r)) { canvas.drawRect(r, paint); }
区域矩形集枚举—RegionIterator类
RegionIterator类,实现了获取组成区域的矩形集的功能,其实RegionIterator类非常简单,总共就两个函数,一个构造函数和一个获取下一个矩形的函数;
RegionIterator(Region region) //根据区域构建对应的矩形集
boolean next(Rect r) //获取下一个矩形,结果保存在参数Rect r 中
如果将上述例子中的paint的style改为stroke的话,可能会看的更加清楚。
paint.setStyle(Paint.Style.STROKE); Path path2=new Path(); RectF rectF2=new RectF(300,400,500,800); path2.addOval(rectF2, Path.Direction.CW); Region region2=new Region(); region2.setPath(path2,new Region(300,400,500,600)); RegionIterator iter2= new RegionIterator(region2); Rect r2= new Rect(); while (iter2.next(r2)) { canvas.drawRect(r2, paint); }
区域的合并与交叉操作
无论是区域还是矩形,都会涉及到对另一个区域的一些操作,比如交集、并集等。
public final boolean union(Rect r) public boolean op(Rect r, Op op) { public boolean op(int left, int top, int right, int bottom, Op op) public boolean op(Region region, Op op) public boolean op(Rect rect, Region region, Op op)
第一个函数是指定区交集操作;其余的op函数,都是需要指定与另外的区域的操作,这里最重要的就是知道Op的参数
public enum Op { DIFFERENCE(0), //最终区域为region1 与 region2不同的区域 INTERSECT(1), // 最终区域为region1 与 region2相交的区域 UNION(2), //最终区域为region1 与 region2组合一起的区域 XOR(3), //最终区域为region1 与 region2相交之外的区域 REVERSE_DIFFERENCE(4), //最终区域为region2 与 region1不同的区域 REPLACE(5); //最终区域为为region2的区域 }
先通过取交集的例子来感受一下:
Rect rect=new Rect(400,400,800,600); Rect rect2=new Rect(500,300,700,700); canvas.drawRect(rect,paint); canvas.drawRect(rect2,paint); Region region1=new Region(rect); Region region2=new Region(rect2); region1.op(region2, Region.Op.INTERSECT); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.GREEN); RegionIterator iterator=new RegionIterator(region1); Rect r=new Rect(); while(iterator.next(r)) { canvas.drawRect(r,paint); }
下面是Op的六种操作的对比:
其它的方法
/**几个判断方法*/ public native boolean isEmpty();//判断该区域是否为空 public native boolean isRect(); //是否是一个矩阵 public native boolean isComplex();//是否是多个矩阵组合 /**一系列的getBound方法,返回一个Region的边界*/ public Rect getBounds() public boolean getBounds(Rect r) public Path getBoundaryPath() public boolean getBoundaryPath(Path path) /**一系列的判断是否包含某点 和是否相交*/ public native boolean contains(int x, int y);//是否包含某点 public boolean quickContains(Rect r) //是否包含某矩形 public native boolean quickContains(int left, int top, int right, int bottom) //是否没有包含某矩阵形 public boolean quickReject(Rect r) //是否没和该矩形相交 public native boolean quickReject(int left, int top, int right, int bottom); //是否没和该矩形相交 public native boolean quickReject(Region rgn); //是否没和该矩形相交 /**几个平移变换的方法*/ public void translate(int dx, int dy) public native void translate(int dx, int dy, Region dst); public void scale(float scale) //hide public native void scale(float scale, Region dst);//hide
- Canvas---区域(Range)
- android Graphics:区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- android Graphics(三):区域(Range)
- (4.1.36.5)android Graphics(三):区域(Range)
- 自定义控件之绘图篇(三):区域(Range)
- 自定义控件之绘图篇(三):区域(Range)
- 自定义控件(30)---绘图区域(Range)
- Canvas裁剪区域
- 自定义控件之绘图篇(三) —— 区域(Range)
- Canvas学习积累——Canvas的区域复制
- 六、matplotlib系列---pyplot基础图表函数概述
- UVALive 4683 Find The Number(容斥+二分)
- Java并发编程(三)Volatile
- 宽度一定且使背景图片不变形的情况下,背景图片不随内容滚动(固定)
- Eclipse(Java EE) 报错:Server Tomcat v7.0 Server at localhost failed to start.
- Canvas---区域(Range)
- php 抽象类和接口的区别
- 当在数据库为数字,需要显示的为文字的时候怎么处理(如性别,星期)
- Power Designer 添加MySQL数据源
- Python基础篇之Socket编程 SocketServer
- HDU 3625 Examining the Rooms(第一类stirling数)
- 第八章 –– 常见类大数据BigInteger和BigDecimal
- 超分辨率重建之FSRCNN
- centos7下部署rocketchat软件