Rect是Rectangle(矩形、長方形)的簡寫,在Graphics2D中,Rect、RectF類定義了一個矩形結構,都實現了Parcelable序列化接口.
在這兩個類中,都用left、top、right、bottom四個成員變量來表示矩形四條邊到坐標軸的距離,不同的是,Rect類中這四個成員變量是int類型,RectF類中是float類型.
一、Rect類
1、成員變量
public int left;
public int top;
public int right;
public int bottom;
left:矩形左邊距y軸距離
top:矩形上邊距x軸距離
right:矩形右邊距y軸距離
bottom:矩形下邊距x軸距離
2、構造方法
public Rect() {}
public Rect(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public Rect(Rect r) {
if (r == null) {
left = top = right = bottom = 0;
} else {
left = r.left;
top = r.top;
right = r.right;
bottom = r.bottom;
}
}
可以通過給出矩形的四個參數(左、上、右、下)初始化,也可以通過另外一個Rect對象初始化.
3、主要方法
1)判定是否為有效矩形
public final boolean isEmpty() {
return left >= right || top >= bottom;
}
Rect類、RectF類大多數方法都沒有檢測是否為有效矩形,這一點需要注意.
2)獲取寬、高、矩形中心點x、y坐標
public final int width() {//寬
return right - left;
}
public final int height() {//高
return bottom - top;
}
/**
* 矩形中心點x坐標,這里采用效率更高的位運算,右移1位相當于除以2
*/
public final int centerX() {
return (left + right) >> 1;
}
/**
* 矩形中心點y坐標
*/
public final int centerY() {
return (top + bottom) >> 1;
}
/**
* 精確的矩形中心點x坐標(float類型),因為直接除以2小數點后會舍去,所以乘以0.5f得到的結果更加精確
*/
public final float exactCenterX() {
return (left + right) * 0.5f;
}
/**
* 精確的矩形中心點y坐標
*/
public final float exactCenterY() {
return (top + bottom) * 0.5f;
}
3)改變矩形位置
將矩形的 left 、 right 、 top 、bottom置為0
public void setEmpty() {
left = right = top = bottom = 0;
}
給矩形四個成員變量賦值
public void set(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public void set(Rect src) {
this.left = src.left;
this.top = src.top;
this.right = src.right;
this.bottom = src.bottom;
}
/**
* 矩形平移
* 矩形x軸方向移動dx距離,y軸方向移動dy距離
* dx、dy的正負代表移動的方向
*/
public void offset(int dx, int dy) {
left += dx;
top += dy;
right += dx;
bottom += dy;
}
/**
* 矩形平移
* newLeft 平移后的left
* newTop 平移后的top
*/
public void offsetTo(int newLeft, int newTop) {
right += newLeft - left;
bottom += newTop - top;
left = newLeft;
top = newTop;
}
注意:平移不會改變矩形的width、height
4)改變矩形大小
/**
* dx>0,左右兩邊向內移動,矩形變窄
* dx<0,左右兩邊向外移動,矩形變寬
* 見下圖
* dy同理,不過移動的是上、下兩邊
*/
public void inset(int dx, int dy) {
left += dx;
top += dy;
right -= dx;
bottom -= dy;
}
/**
* left 、top、right 、bottom 矩形四條邊移動的距離
* 正負代表移動的方向,為正時,朝內移動,為負時,朝外移動
*/
public void inset(int left, int top, int right, int bottom) {
this.left += left;
this.top += top;
this.right -= right;
this.bottom -= bottom;
}
/**
* 根據另外一個Rect 對象的四個成員變量移動
*/
public void inset(Rect insets) {
left += insets.left;
top += insets.top;
right -= insets.right;
bottom -= insets.bottom;
}
5)包含判斷
/**
* 判斷點(x,y)是否在當前矩形內
*/
public boolean contains(int x, int y) {
return left < right && top < bottom // 檢測是否為有效矩形
&& x >= left && x < right && y >= top && y < bottom;//檢測矩形中是否包含點(x,y)
}
/**
* 判斷另外一個矩形是否在當前矩形內
*/
public boolean contains(int left, int top, int right, int bottom) {
return this.left < this.right && this.top < this.bottom// 檢測是否為有效矩形
&& this.left <= left && this.top <= top//另外的矩形left、top<=本矩形left、top
&& this.right >= right && this.bottom >= bottom;//另外的矩形right 、bottom >=本矩形left、top
}
/**
* 判斷另外一個矩形是否在當前矩形內
*/
public boolean contains(Rect r) {
return this.left < this.right && this.top < this.bottom
&& left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom;
}
6)矩形的交集運算
/**
* 傳入left 、top、right、bottom和當前Rect做交集運算,結果保存在當前Rect對象中
*
* 返回值代表是否有交集,有交集為true,反之,false
* 當有交集時,Rect的left 、top、right、bottom為相交矩形的left 、top、right、bottom
*/
public boolean intersect(int left, int top, int right, int bottom) {
if (this.left < right && left < this.right && this.top < bottom && top < this.bottom) {
if (this.left < left) this.left = left;
if (this.top < top) this.top = top;
if (this.right > right) this.right = right;
if (this.bottom > bottom) this.bottom = bottom;
return true;
}
return false;
}
/**
* 傳入一個Rect對象和當前Rect做交集運算,結果保存在當前Rect對象中
*/
public boolean intersect(Rect r) {
return intersect(r.left, r.top, r.right, r.bottom);
}
/**
* 傳入兩個Rect對象做交集運算,結果保存在當前Rect對象中
*/
public boolean setIntersect(Rect a, Rect b) {
if (a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom) {
left = Math.max(a.left, b.left);
top = Math.max(a.top, b.top);
right = Math.min(a.right, b.right);
bottom = Math.min(a.bottom, b.bottom);
return true;
}
return false;
}
有交集的情況
/**
*判斷是否有交集,有返回true,否則,返回false
*/
public boolean intersects(int left, int top, int right, int bottom) {
return this.left < right && left < this.right && this.top < bottom && top < this.bottom;
}
/**
*判斷是否有交集,有返回true,否則,返回false
*/
public static boolean intersects(Rect a, Rect b) {
return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;
}
7)矩形的并集運算
/**
* 傳入left 、top、right、bottom和當前Rect做并集運算,結果保存在當前Rect對象中
*/
public void union(int left, int top, int right, int bottom) {
if ((left < right) && (top < bottom)) {//1、先判斷傳過來的矩形是否有效
if ((this.left < this.right) && (this.top < this.bottom)) {//2.1、判斷當前矩形是否有效
if (this.left > left) this.left = left;
if (this.top > top) this.top = top;
if (this.right < right) this.right = right;
if (this.bottom < bottom) this.bottom = bottom;
} else {//2.2、如果當前矩形無效,直接把傳過來的矩形保存在當前Rect對象中
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
}
public void union(Rect r) {
union(r.left, r.top, r.right, r.bottom);
}
這里需要注意的是,和數學中的并集運算略有不同,如圖:矩形A、B做并集運算,結果矩形是取四個方向的最大值,新的大矩形作為結果保存在當前Rect對象中
二、RectF類
RectF類和Rect方法邏輯基本一樣,主要是Rect成員變量為int類型,RectF為float類型
1、獲取矩形中心點x、y坐標
這里不同的是:RectF類獲取中心點x,y坐標本身就是float類型的,所以
沒有exactCenterX()、exactCenterY方法.
public final float centerX() {
return (left + right) * 0.5f;
}
public final float centerY() {
return (top + bottom) * 0.5f;
}
2、RectF和Rect的轉換
Rect類中沒有定義兩者之間轉化的方法.
1)、將一個Rect對象轉換為RectF對象
RectF類有一個構造方法,可以將一個Rect對象轉換為RectF對象
public RectF(Rect r) {
if (r == null) {
left = top = right = bottom = 0.0f;
} else {
left = r.left;
top = r.top;
right = r.right;
bottom = r.bottom;
}
}
2)、將一個RectF對象轉換為Rect對象
RectF中定義了兩個方法,可以傳入一個Rect對象,然后將當前RectF對象的4個成員變量處理后設置給Rect對象的成員變量
/**
* RectF的4個成員變量四舍五入后設置給傳入的Rect對象
*/
public void round(Rect dst) {
dst.set(FastMath.round(left), FastMath.round(top),
FastMath.round(right), FastMath.round(bottom));
}
/**
* RectF的left、top向下取整,right、bottom向上取整,然后設置給傳入的Rect對象
*/
public void roundOut(Rect dst) {
dst.set((int) Math.floor(left), (int) Math.floor(top),
(int) Math.ceil(right), (int) Math.ceil(bottom));
}