自定义控件之Region区域

构造Region

直接构造

    public Region(Region region)  //复制一个同样的Region变量
    public Region(Rect r) 
    public Region(int left, int top, int right, int bottom)

示例:

   private void drawRegion(Canvas canvas, Region rgn, Paint paint) {
        RegionIterator iter = new RegionIterator(rgn);
        Rect r = new Rect();

        while (iter.next(r)) {
            canvas.drawRect(r, paint);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        Region region = new Region(new Rect(50, 50, 200, 100));
        drawRegion(canvas, region, paint);
    }

间接构造

public Region()

set函数:

    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)//根据路径的区域与某区域的交集构造出新的区域

置空

  public void setEmpty() //将原来的区域设置为空,再利用其它set函数重新构造区域

注意调用set系列函数的region是不是有区域值,当调用set系列函数后,原来的区域值就会替换成set系列函数里的区域值

示例:

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        //构造一个椭圆路径
        Path ovalPath = new Path();
        RectF rect = new RectF(50, 50, 200, 500);
        ovalPath.addOval(rect, Path.Direction.CCW);
        //SetPath时,传入一个比椭圆区域小的矩形区域,让其取交集
        Region rgn = new Region();
        rgn.setPath(ovalPath, new Region(50, 50, 200, 200));
        //画出路径
        drawRegion(canvas, rgn, paint);

枚举区域-RegionIterator 类

    public RegionIterator(Region region) 构造函数
    public final boolean next(Rect r)获取下一个矩形
    protected void finalize() throws Throwable 

在Canvas中没有直接绘制Region函数,要绘制一个区域,只能通过该类构造矩形来逼近显示区域

   private void drawRegion(Canvas canvas, Region rgn, Paint paint) {
        RegionIterator iter = new RegionIterator(rgn);
        Rect r = new Rect();

        while (iter.next(r)) {
            canvas.drawRect(r, paint);
        }
    }

区域相交

boolean nuion(Rect r)函数用于与指定的矩形取并集

区域操作

    public boolean op(Rect r, Region.Op op)
    public boolean op(int left, int top, int right, int bottom, Region.Op op) 
    public boolean op(Region region, Region.Op op)
    public boolean op(Rect rect, Region region, Region.Op op)
    public boolean op(Region region1, Region region2, Region.Op op) 

Op参数值如下

        DIFFERENCE  最终区域为region1和region2不同的区域
        INTERSECT  最终区域为region1和region2相交区域
        REPLACE  最终区域为region2的区域
        REVERSE_DIFFERENCE  最终区域为region2和region1不同的区域
        UNION  最终区域为region1和region2组合一起的区域
        XOR  最终区域为region1和region2相交之外的区域

示例代码:

 private void init() {

        //构造两个矩形
        rect1 = new Rect(100,100,400,200);
        rect2 = new Rect(200,0,300,300);

        //构造一个画笔,画出矩形轮廓
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);


        //构造两个Region
        region = new Region(rect1);
        region2= new Region(rect2);

        //取两个区域的交集
        region.op(region2, Region.Op. REVERSE_DIFFERENCE);
    }


    private void drawRegion(Canvas canvas, Region rgn, Paint paint) {
        RegionIterator iter = new RegionIterator(rgn);
        Rect r = new Rect();

        while (iter.next(r)) {
            canvas.drawRect(r, paint);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画出相交的矩形
        canvas.drawRect(rect1, paint);
        canvas.drawRect(rect2, paint);

        //画面相交的结果
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.FILL);
        drawRegion(canvas, region, paint);
    }

改变Op参数对比

DIFFERENCE

INTERSECT

REPLACE

REVERSE_DIFFERENCE

UNION

XOR

其他函数

判断方法:

 

 boolean isEmpty();  判断该区域是否为空
 boolean isRect();判断该区域是否是一个矩阵
 boolean isComplex();判断该区域是否是多个矩阵的组合

 

getBound系列函数

用于返回一个Region边界,用于返回能够包裹当前路径的最小矩形

 public Rect getBounds()
 public boolean getBounds(Rect r)

用于返回当前矩形所对应的Path对象

 public Path getBoundaryPath()
 public boolean getBoundaryPath(Path path) 

是否包含

public native boolean contains(int var1, int var2);
public boolean quickContains(Rect r) 
public native boolean quickContains(int var1, int var2, int var3, int var4);

是否相交

 public boolean quickReject(Rect r)
     public native boolean quickReject(int var1, int var2, int var3, int var4);
    public native boolean quickReject(Region var1);

平移变换

public void translate(int dx, int dy) 
public native void translate(int var1, int var2, Region var3);

 

posted on 2018-12-28 16:08  LoaderMan  阅读(714)  评论(0编辑  收藏  举报

导航