---
# System prepended metadata

title: '<font style="text-shadow:0px 0px 15px #FF37FD;">RectF</font>'
tags: [Kotlin, Android developer]

---

<font style="text-shadow:0px 0px 15px #FF37FD;">RectF</font>
===
###### tags: `Android developer` `Kotlin`
 <font color="Brown">android.graphics.RectF</font>
 
 <font color="Brown">RectF為一個矩形保存四個**浮點**坐標。矩形由其4條邊（左，上，右下）的坐標表示。可以直接訪問這些字段。使用width（）和height（）來檢索矩形的寬度和高度。注意：大多數方法都不檢查坐標是否正確排序（即左<=右和頂<=底）。</font>
 
## <font color="sandybrown">Constructors</font>

### <font color="#990DFF">RectF()</font>
### <font color="#990DFF">RectF(left: Float, top: Float, right: Float, bottom: Float)</font>
### <font color="#990DFF">RectF(r: RectF?)</font>
### <font color="#990DFF">RectF(r: Rect?)</font>

## <font color="sandybrown">常量</font>
### <font color="#990DFF">ANTI_ALIAS_FLAG</font>

:::info
繪製抗鋸齒
:::

:::warning

**Ex:**
```kotlin=
Paint(ANTI_ALIAS_FLAG)

Paint().flags=ANTI_ALIAS_FLAG
```
  
:::

## <font color="sandybrown">方法</font>
### <font color="#990DFF">contains(x: Float, y: Float): Boolean</font>

:::info
如果（x，y）在矩形內，則返回true。左側和頂部被認為是內側，而右側和下側則不是。這意味著對於ax，y包含：left <= x <right和top <= y <bottom。空矩形永遠不會包含任何點。
:::

:::success
**參數**
x : 要測試包含的點的X坐標
Y : 要測試包含的點的Y坐標
:::

:::warning

**Ex:**
```kotlin=
var rect= RectF(100f, 10f, 300f, 100f)
Log.d("Contains", "onDraw: ${rect.contains(50f,50f)}")
```
:::danger
**輸出結果 :**
![](https://i.imgur.com/QuhzjwY.png)

:::

### <font color="#990DFF">contains(left: Float, top: Float, right: Float, bottom: Float): Boolean</font>

:::info
如果矩形的4個指定邊在此矩形內或等於此矩形，則返回true。即，此矩形是指定矩形的超集。空矩形永遠不會包含另一個矩形。
:::

:::success
**參數**
left : 正在測試包含矩形的左側
top : 正在測試包含的矩形的頂部
right : 正在測試包圍的矩形的右側
bottom : 正在測試包含的矩形底部
:::

:::warning

**Ex:**
```kotlin=
var rect= RectF(100f, 10f, 300f, 100f)
Log.d("Contains", "onDraw: ${rect.contains(0f,0f,50f,50f)}")
```
:::danger
**輸出結果 :**
![](https://i.imgur.com/ELkkiz3.png)

:::

### <font color="#990DFF">contains(r: Rect): Boolean</font>

:::info
如果指定的矩形r在此矩形內部或等於此矩形，則返回true。空矩形永遠不會包含另一個矩形。。
:::

:::success
**參數**
r : 正在測試包含的矩形。絕不能是這個值null。
:::

:::warning

**Ex:**
```kotlin=
var rect= RectF(100f, 10f, 300f, 100f)
Log.d("Contains", "onDraw: ${rect.contains(Rect(0f, 0f, 50f, 50f))}")
```
:::

### <font color="#990DFF">static fun intersects(a: Rect, b: Rect): Boolean</font>

:::info
如果兩個指定的矩形相交，則返回true。
:::

:::success
**參數**
a : 要為交集測試的第一個矩形，該值絕不能是null。
b : 要測試交集的第二個矩形此值絕不能是null。
:::

:::warning

**Ex:**
```kotlin=
var paint: Paint = Paint().apply {
            color = Color.RED
            style=Paint.Style.STROKE
            strokeWidth = 5f
        }
val rect_1=RectF(10f,10f,200f,200f)
val rect_2=RectF(190f,10f,250f,200f)
val rect_3=RectF(10f,210f,200f,300f)
canvas?.drawRect(rect_1,paint)
paint.color=Color.GREEN
canvas?.drawRect( rect_2,paint)
paint.color=Color.BLUE
canvas?.drawRect(rect_3 ,paint)
Log.d("intersects", "onDraw: ${Rect.intersects(rect_1,rect_2)}")
Log.d("intersects", "onDraw: ${Rect.intersects(rect_1,rect_3)}")
```
:::danger
**輸出結果 :**
![](https://i.imgur.com/OagiZSz.png)

D/intersects: onDraw: true
D/intersects: onDraw: false
:::

### <font color="#990DFF">intersects(left: Float, top: Float, right: Float, bottom: Float): Boolean</font>

:::info
如果此矩形與指定的矩形相交，則返回true。
:::

:::success
**參數**
left : 正在測試交叉點的矩形的左側
top : 正在測試交叉點的矩形頂部
right : 正在測試交叉點的矩形的右側
bottom : 正在測試交叉點的矩形的底部
:::

:::warning

**Ex:**
```kotlin=
val rect_1=RectF(10f,10f,200f,200f)
Log.d("intersects", "onDraw: ${rect_1.intersects(190f,10f,250f,200f)}")
```
:::

### <font color="#990DFF">intersect(left: Float, top: Float, right: Float, bottom: Float): Boolean</font>

:::info
如果由left，top，right，bottom指定的矩形與此矩形相交，則返回true並將此矩形設置為該交集，否則返回false並且不更改此矩形。不檢查是否有任何矩形為空。
:::

:::success
**參數**
left : 矩形的左側與此矩形相交
top : 矩形的頂部與此矩形相交
right : 矩形的右側與此矩形相交
bottom : 矩形的底部與此矩形相交
:::

:::warning

**Ex:**
```kotlin=
val rect_1=RectF(10f,10f,200f,200f)
// 相交部分不會賦予值給rect_1 結果->true
val result_1=rect_1.intersects(190f,10f,250f,200f)
Log.d("intersects", "onDraw: $result_1 / $rect_1")
// 未相交部分 結果->false
val result_2=rect_1.intersect(210f, 10f, 250f, 200f)
Log.d("intersects", "onDraw: $result_2 / $rect_1")
// 相交部分會賦予值給rect_1 結果->true
val result_3=rect_1.intersect(190f, 10f, 250f, 200f)
Log.d("intersects", "onDraw: $result_3 / $rect_1")
```
:::danger
**輸出結果 :**
D/intersects: onDraw: true / Rect(10, 10 - 200, 200)
D/intersects: onDraw: false / Rect(10, 10 - 200, 200)
D/intersects: onDraw: true / Rect(190, 10 - 200, 200)
:::

### <font color="#990DFF">union(left: Float, top: Float, right: Float, bottom: Float): Unit</font>

:::info
合併兩個矩形，不論是否有相交，取得兩個矩形最小左上角的點作為結果矩形的左上角點，取兩個矩形最大右下角點作為結果矩形的右下角點
:::

:::success
**參數**
left : 左邊緣與此矩形聯合
top : 頂部邊緣與此矩形結合在一起
right : 右邊緣與此矩形結合
bottom : 底邊與此矩形結合
:::

:::warning

**Ex:**
```kotlin=
var paint: Paint = Paint().apply {
            color = Color.RED
            style=Paint.Style.STROKE
            strokeWidth = 5f
        }
val rect_1=RectF(100f,100f,120f,120f)
val rect_2=RectF(200f,200f,220f,220f)
canvas?.drawRect(rect_1,paint)
paint.color=Color.GREEN
canvas?.drawRect(rect_2,paint)
paint.color=Color.BLUE
rect_1.union(rect_2)
canvas?.drawRect(rect_1,paint)
```
:::danger
**輸出結果 :**
![](https://i.imgur.com/cPAvxb8.png)
:::

### <font color="#990DFF">union(x: Float, y: Float): Unit</font>

:::info
判斷矩形與點的相對位置，如果點沒有與矩形相交，則會取點與矩形的聯集，如果點在矩形裡面則取得矩形的左上右下座標
:::

:::success
**參數**
x : 要添加到矩形的點的x坐標
y : 要添加到矩形的點的y坐標
:::

:::warning

**Ex:**
```kotlin=
 var paint: Paint = Paint().apply {
            color = Color.RED
            style=Paint.Style.STROKE
            strokeWidth = 5f
        }
val rect_1=RectF(100f,100f,120f,120f)
canvas?.drawRect(rect_1,paint)
paint.color=Color.BLUE
rect_1.union(50f,50f)
paint.color=Color.GREEN
canvas?.drawPoint(50f,50f,paint)

paint.color=Color.RED
val rect_2=RectF(200f,50f,220f,70f)
canvas?.drawRect(rect_2,paint)
paint.color=Color.BLUE
rect_2.union(270f,120f)
canvas?.drawRect(rect_2,paint)
paint.color=Color.GREEN
canvas?.drawPoint(270f,120f,paint)
```
:::danger
**輸出結果 :**
![](https://i.imgur.com/o5etqZx.png)
:::