Graphics2D API:Rect類、RectF類

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));
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,619評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,155評論 3 425
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,635評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,539評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,255評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,646評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,655評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,838評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,399評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,146評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,338評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,893評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,565評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,983評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,257評論 1 292
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,059評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,296評論 2 376

推薦閱讀更多精彩內容