<font style="text-shadow:0px 0px 15px #FF37FD;">Region.Op</font> === ###### tags: `Android developer` `Kotlin` <font color="Brown">android.graphics.Region.Op</font> <font color="Brown">關於面域聯集交集</font> ## <font color="sandybrown">Enum</font> ### <font color="#990DFF">DIFFERENCE</font> :::info 最終區域為 region1 與 region2 不相同的區域 ::: :::warning **Ex:** ```kotlin= val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 2f } val rect1 = Rect(100, 200, 400, 300) val rect2 = Rect(200, 100, 300, 400) canvas?.drawRect(rect1, paint) canvas?.drawRect(rect2, paint) val region1 = Region(rect1) val region2 = Region(rect2) // 最終區域為 region1 與 region2 不相同的區域 region1.op(region2, Region.Op.DIFFERENCE) paint.apply { color = Color.BLUE style = Paint.Style.FILL } val regionIterator = RegionIterator(region1) val rect = Rect() while (regionIterator.next(rect)) { canvas?.drawRect(rect, paint) } ``` :::danger **輸出結果 :** ![](https://i.imgur.com/bsP8YwX.png) ::: ### <font color="#990DFF">INTERSECT</font> :::info 最終區域為 region1 與 region2 相交的區域 ::: :::warning **Ex:** ```kotlin= val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 2f } val rect1 = Rect(100, 200, 400, 300) val rect2 = Rect(200, 100, 300, 400) canvas?.drawRect(rect1, paint) canvas?.drawRect(rect2, paint) val region1 = Region(rect1) val region2 = Region(rect2) // 最終區域為 region1 與 region2 相交的區域 region1.op(region2, Region.Op.INTERSECT) paint.apply { color = Color.BLUE style = Paint.Style.FILL } val regionIterator = RegionIterator(region1) val rect = Rect() while (regionIterator.next(rect)) { canvas?.drawRect(rect, paint) } ``` :::danger **輸出結果 :** ![](https://i.imgur.com/Sq15leR.png) ::: ### <font color="#990DFF">REPLACE</font> :::info 最終區域為 region2 區域 ::: :::warning **Ex:** ```kotlin= val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 2f } val rect1 = Rect(100, 200, 400, 300) val rect2 = Rect(200, 100, 300, 400) canvas?.drawRect(rect1, paint) canvas?.drawRect(rect2, paint) val region1 = Region(rect1) val region2 = Region(rect2) // 最終區域為 region2 區域 region1.op(region2, Region.Op.REPLACE) paint.apply { color = Color.BLUE style = Paint.Style.FILL } val regionIterator = RegionIterator(region1) val rect = Rect() while (regionIterator.next(rect)) { canvas?.drawRect(rect, paint) } ``` :::danger **輸出結果 :** ![](https://i.imgur.com/y0aR60g.png) ::: ### <font color="#990DFF">REVERSE_DIFFERENCE</font> :::info 最終區域為 region2 與 region1 不相同的區域 ::: :::warning **Ex:** ```kotlin= val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 2f } val rect1 = Rect(100, 200, 400, 300) val rect2 = Rect(200, 100, 300, 400) canvas?.drawRect(rect1, paint) canvas?.drawRect(rect2, paint) val region1 = Region(rect1) val region2 = Region(rect2) // 最終區域為 region2 與 region1 不相同的區域 region1.op(region2, Region.Op.REVERSE_DIFFERENCE) paint.apply { color = Color.BLUE style = Paint.Style.FILL } val regionIterator = RegionIterator(region1) val rect = Rect() while (regionIterator.next(rect)) { canvas?.drawRect(rect, paint) } ``` :::danger **輸出結果 :** ![](https://i.imgur.com/aavjq3E.png) ::: ### <font color="#990DFF">UNION</font> :::info 最終區域為 region1 與 region2 組合在一起的區域 ::: :::warning **Ex:** ```kotlin= val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 2f } val rect1 = Rect(100, 200, 400, 300) val rect2 = Rect(200, 100, 300, 400) canvas?.drawRect(rect1, paint) canvas?.drawRect(rect2, paint) val region1 = Region(rect1) val region2 = Region(rect2) // 最終區域為 region1 與 region2 組合在一起的區域 region1.op(region2, Region.Op.UNION) paint.apply { color = Color.BLUE style = Paint.Style.FILL } val regionIterator = RegionIterator(region1) val rect = Rect() while (regionIterator.next(rect)) { canvas?.drawRect(rect, paint) } ``` :::danger **輸出結果 :** ![](https://i.imgur.com/OmlV71L.png) ::: ### <font color="#990DFF">XOR</font> :::info 最終區域為 region1 與 region2 相交以外的區域 ::: :::warning **Ex:** ```kotlin= val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 2f } val rect1 = Rect(100, 200, 400, 300) val rect2 = Rect(200, 100, 300, 400) canvas?.drawRect(rect1, paint) canvas?.drawRect(rect2, paint) val region1 = Region(rect1) val region2 = Region(rect2) // 最終區域為 region1 與 region2 相交以外的區域 region1.op(region2, Region.Op.XOR) paint.apply { color = Color.BLUE style = Paint.Style.FILL } val regionIterator = RegionIterator(region1) val rect = Rect() while (regionIterator.next(rect)) { canvas?.drawRect(rect, paint) } ``` :::danger **輸出結果 :** ![](https://i.imgur.com/7RxXe7y.png) :::