前言
Region,即為區域,它表示的是canvas圖層上的某一塊封閉的區域。很多時候,我們會利用Region來構造一個圖形。
今天要講的內容有:
- Region的直接構造方法
- Region的間接構造方法
- Region的setPath(Path path, Region clip)方法介紹
- Region取區域並集
- Region.Op常量操作Region
- 項目結構圖和效果圖
一. Region的直接構造方法
Region有以下構造方法:
public Region(Region region);//復制一個Region
public Region(Rect r);//通過Rect構建一個Region
public Region(int left, int top, int right, int bottom);//通過坐標點構建一個region
下面我們試圖繪制一個Region,代碼如下:
//設置畫筆
Paint paint=new Paint();
paint.setColor(getRidColor(R.color.color_0a900a));
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5f);//無描邊,設置setStrokeWidth無效
//構建矩形
Rect rect=new Rect();
rect.set(340,50,740,250);
Region region=new Region(rect);
//Android還提供了一個RegionIterator來對Region中的所有矩陣進行迭代,
// 可以使用該類,獲得某個Region的所有矩陣
//通過遍歷region中的矩陣,並繪制出來,來繪制region
RegionIterator iterator=new RegionIterator(region);
Rect r=new Rect();
while(iterator.next(r)){
canvas.drawRect(r,paint);
}
以上代碼值得注意的是,Paint設置的style是FILL,不存在描邊的問題,所有設置 stokenWidth無效。
RegionIterator 是Region中所有矩陣的迭代器,我們可以通過遍歷region中的矩陣,並繪制出來,來繪制region。
繪制出的Region效果圖如下:
由上面的代碼可以看出來,Canvas並未提供直接繪制Region的方法,而Region的本意也不是用來繪圖的。它的主要作用是來操作圖形的,用處理區域間的合並,差集等邏輯關系
二. Region的間接構造方法
Region的間接構造方法主要是通過new一個空的Region,然后結合set相關函數來設置Region。
Region空構造函數:
public Region();
涉及的set函數有:
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的話,你可以這樣:
Region region=new Region();
region.set(340,50,740,250);
值得注意的是,setXXX系列的方法,都會替換掉之前Region中的區域值。
setEmpty()方法是清空Region中的區域。
其他幾個方法都好理解,下面着重講 setPath 方法。
三. Region的setPath(Path path, Region clip)方法介紹
public boolean setPath(Path path, Region clip);
參數介紹:
Path path:用來構造區域的路徑
Region clip:與第一個參數path所構成的路徑取交集,並將該交集設為最終區域。
簡單的說,就是setPath(Path path, Region clip)方法是將path形成的路徑和clip形成的區域取交集,獲得一個交集區域。
下面給出setPath方法使用的代碼:
//設置paint
Paint paint=new Paint();
paint.setColor(getRidColor(R.color.color_f5cc1d));
paint.setStyle(Paint.Style.FILL);
//構造橢圓路徑
Path path=new Path();
//構建橢圓path
RectF rectF=new RectF(100,300,980,500);
path.addOval(rectF,Path.Direction.CCW);//Path.Direction.CCW:逆時針;Path.Direction.CW:順時針
//構建Region
Region region=new Region();
region.set(540,300,980,500);
//取path和region的交集
Region rgn=new Region();
rgn.setPath(path,region);
//繪制區域
drawRegion(canvas,rgn,paint);
效果圖如下:
四. Region取區域並集
Region取區域並集的方法如下:
boolean union(Rect r);
返回的是一個boolean,若為true,則表示並集成功,否則表示失敗
示例代碼如下:
//設置畫筆
Paint paint=new Paint();
paint.setColor(getRidColor(R.color.color_12aef7));
paint.setStyle(Paint.Style.FILL);
//設置區域
Region region=new Region(540,550,980,650);
region.union(new Rect(490,600,590,700));
//繪制區域
drawRegion(canvas,region,paint);
效果圖如下:
五.Region.Op常量操作Region
當然,region的操作還有多種,簡便的操作,由Region.Op常量控制
Region.Op操作常量有:
Region.Op.INTERSECT //交集
Region.Op.DIFFERENCE //補集
Region.Op.REPLACE //替換
Region.Op.REVERSE_DIFFERENCE //反轉補集
Region.Op.UNION //並集
Region.Op.XOR //異並集
下面給出Region操作交集的示例代碼:
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
//繪制矩形軌跡
Rect rOne=new Rect(120,750,220,1050);
Rect rTwo=new Rect(20,850,320,950);
canvas.drawRect(rOne,paint);
canvas.drawRect(rTwo,paint);
Region regionOne=new Region(rOne);
Region regionTwo=new Region(rTwo);
regionTwo.op(regionOne,Region.Op.INTERSECT);//交集
paint.setStyle(Paint.Style.FILL);
drawRegion(canvas,regionTwo,paint);
效果圖如下:
其他幾種Region操作情況與此類似,demo中均有詳細代碼,現在給出自定義控件RegionView的主要邏輯代碼:
//初始化
init(canvas);
//直接構建Region
directbuildRegion(canvas);
//間接構建Region
inDirectBuildRegion(canvas);
//Region的setPath方法求path和region的交集
pathAndRegionIntersection(canvas);
//區域取並集
rectAndRectIntersection(canvas);
//區域操作
controOne(canvas);//交集
controTwo(canvas);//補集
controThree(canvas);//替換
controFour(canvas);//反轉補集
controFive(canvas);//並集
controSix(canvas);//異並集
RegionView在MainActivity對應的activity_main.xml中引用如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.testdemo.main.MainActivity">
<com.android.testdemo.function.RegionView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
六.項目結構圖和效果圖
項目結構圖
效果圖
Region使用全解
注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權