Rect是Rectangle(矩形、長(zhǎng)方形)的簡(jiǎn)寫窑邦,在Graphics2D中擅威,Rect、RectF類定義了一個(gè)矩形結(jié)構(gòu)冈钦,都實(shí)現(xiàn)了Parcelable序列化接口.
在這兩個(gè)類中郊丛,都用left、top瞧筛、right厉熟、bottom四個(gè)成員變量來表示矩形四條邊到坐標(biāo)軸的距離,不同的是较幌,Rect類中這四個(gè)成員變量是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绢片、構(gòu)造方法
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;
}
}
可以通過給出矩形的四個(gè)參數(shù)(左、上岛琼、右底循、下)初始化,也可以通過另外一個(gè)Rect對(duì)象初始化.
3衷恭、主要方法
1)判定是否為有效矩形
public final boolean isEmpty() {
return left >= right || top >= bottom;
}
Rect類此叠、RectF類大多數(shù)方法都沒有檢測(cè)是否為有效矩形,這一點(diǎn)需要注意.
2)獲取寬、高灭袁、矩形中心點(diǎn)x猬错、y坐標(biāo)
public final int width() {//寬
return right - left;
}
public final int height() {//高
return bottom - top;
}
/**
* 矩形中心點(diǎn)x坐標(biāo),這里采用效率更高的位運(yùn)算茸歧,右移1位相當(dāng)于除以2
*/
public final int centerX() {
return (left + right) >> 1;
}
/**
* 矩形中心點(diǎn)y坐標(biāo)
*/
public final int centerY() {
return (top + bottom) >> 1;
}
/**
* 精確的矩形中心點(diǎn)x坐標(biāo)(float類型)倦炒,因?yàn)橹苯映?小數(shù)點(diǎn)后會(huì)舍去,所以乘以0.5f得到的結(jié)果更加精確
*/
public final float exactCenterX() {
return (left + right) * 0.5f;
}
/**
* 精確的矩形中心點(diǎn)y坐標(biāo)
*/
public final float exactCenterY() {
return (top + bottom) * 0.5f;
}
3)改變矩形位置
將矩形的 left 软瞎、 right 逢唤、 top 、bottom置為0
public void setEmpty() {
left = right = top = bottom = 0;
}
給矩形四個(gè)成員變量賦值
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軸方向移動(dòng)dx距離涤浇,y軸方向移動(dòng)dy距離
* dx鳖藕、dy的正負(fù)代表移動(dòng)的方向
*/
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;
}
注意:平移不會(huì)改變矩形的width、height
4)改變矩形大小
/**
* dx>0只锭,左右兩邊向內(nèi)移動(dòng)著恩,矩形變窄
* dx<0,左右兩邊向外移動(dòng)蜻展,矩形變寬
* 見下圖
* dy同理喉誊,不過移動(dòng)的是上、下兩邊
*/
public void inset(int dx, int dy) {
left += dx;
top += dy;
right -= dx;
bottom -= dy;
}
/**
* left 纵顾、top伍茄、right 、bottom 矩形四條邊移動(dòng)的距離
* 正負(fù)代表移動(dòng)的方向施逾,為正時(shí)敷矫,朝內(nèi)移動(dòng),為負(fù)時(shí)音念,朝外移動(dòng)
*/
public void inset(int left, int top, int right, int bottom) {
this.left += left;
this.top += top;
this.right -= right;
this.bottom -= bottom;
}
/**
* 根據(jù)另外一個(gè)Rect 對(duì)象的四個(gè)成員變量移動(dòng)
*/
public void inset(Rect insets) {
left += insets.left;
top += insets.top;
right -= insets.right;
bottom -= insets.bottom;
}
5)包含判斷
/**
* 判斷點(diǎn)(x,y)是否在當(dāng)前矩形內(nèi)
*/
public boolean contains(int x, int y) {
return left < right && top < bottom // 檢測(cè)是否為有效矩形
&& x >= left && x < right && y >= top && y < bottom;//檢測(cè)矩形中是否包含點(diǎn)(x,y)
}
/**
* 判斷另外一個(gè)矩形是否在當(dāng)前矩形內(nèi)
*/
public boolean contains(int left, int top, int right, int bottom) {
return this.left < this.right && this.top < this.bottom// 檢測(cè)是否為有效矩形
&& this.left <= left && this.top <= top//另外的矩形left沪饺、top<=本矩形left、top
&& this.right >= right && this.bottom >= bottom;//另外的矩形right 闷愤、bottom >=本矩形left整葡、top
}
/**
* 判斷另外一個(gè)矩形是否在當(dāng)前矩形內(nèi)
*/
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)矩形的交集運(yùn)算
/**
* 傳入left 、top讥脐、right遭居、bottom和當(dāng)前Rect做交集運(yùn)算,結(jié)果保存在當(dāng)前Rect對(duì)象中
*
* 返回值代表是否有交集旬渠,有交集為true俱萍,反之,false
* 當(dāng)有交集時(shí)告丢,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;
}
/**
* 傳入一個(gè)Rect對(duì)象和當(dāng)前Rect做交集運(yùn)算话侧,結(jié)果保存在當(dāng)前Rect對(duì)象中
*/
public boolean intersect(Rect r) {
return intersect(r.left, r.top, r.right, r.bottom);
}
/**
* 傳入兩個(gè)Rect對(duì)象做交集運(yùn)算栗精,結(jié)果保存在當(dāng)前Rect對(duì)象中
*/
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)矩形的并集運(yùn)算
/**
* 傳入left 、top赫悄、right寥殖、bottom和當(dāng)前Rect做并集運(yùn)算,結(jié)果保存在當(dāng)前Rect對(duì)象中
*/
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、判斷當(dāng)前矩形是否有效
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熏纯、如果當(dāng)前矩形無效同诫,直接把傳過來的矩形保存在當(dāng)前Rect對(duì)象中
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);
}
這里需要注意的是,和數(shù)學(xué)中的并集運(yùn)算略有不同樟澜,如圖:矩形A误窖、B做并集運(yùn)算,結(jié)果矩形是取四個(gè)方向的最大值秩贰,新的大矩形作為結(jié)果保存在當(dāng)前Rect對(duì)象中
二霹俺、RectF類
RectF類和Rect方法邏輯基本一樣,主要是Rect成員變量為int類型毒费,RectF為float類型
1丙唧、獲取矩形中心點(diǎn)x、y坐標(biāo)
這里不同的是:RectF類獲取中心點(diǎn)x觅玻,y坐標(biāo)本身就是float類型的想际,所以
沒有exactCenterX()、exactCenterY方法.
public final float centerX() {
return (left + right) * 0.5f;
}
public final float centerY() {
return (top + bottom) * 0.5f;
}
2溪厘、RectF和Rect的轉(zhuǎn)換
Rect類中沒有定義兩者之間轉(zhuǎn)化的方法.
1)胡本、將一個(gè)Rect對(duì)象轉(zhuǎn)換為RectF對(duì)象
RectF類有一個(gè)構(gòu)造方法,可以將一個(gè)Rect對(duì)象轉(zhuǎn)換為RectF對(duì)象
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)畸悬、將一個(gè)RectF對(duì)象轉(zhuǎn)換為Rect對(duì)象
RectF中定義了兩個(gè)方法侧甫,可以傳入一個(gè)Rect對(duì)象,然后將當(dāng)前RectF對(duì)象的4個(gè)成員變量處理后設(shè)置給Rect對(duì)象的成員變量
/**
* RectF的4個(gè)成員變量四舍五入后設(shè)置給傳入的Rect對(duì)象
*/
public void round(Rect dst) {
dst.set(FastMath.round(left), FastMath.round(top),
FastMath.round(right), FastMath.round(bottom));
}
/**
* RectF的left、top向下取整披粟,right纵苛、bottom向上取整,然后設(shè)置給傳入的Rect對(duì)象
*/
public void roundOut(Rect dst) {
dst.set((int) Math.floor(left), (int) Math.floor(top),
(int) Math.ceil(right), (int) Math.ceil(bottom));
}