Graphics2D API:Rect類、RectF類

Rect是Rectangle(矩形、長方形)的簡寫,在Graphics2D中,Rect、RectF類定義了一個矩形結(jié)構(gòu),都實現(xiàn)了Parcelable序列化接口.

在這兩個類中,都用left、top、right、bottom四個成員變量來表示矩形四條邊到坐標(biāo)軸的距離,不同的是,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、構(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;
        }
    }

可以通過給出矩形的四個參數(shù)(左、上、右、下)初始化,也可以通過另外一個Rect對象初始化.

3、主要方法
1)判定是否為有效矩形
    public final boolean isEmpty() {
        return left >= right || top >= bottom;
    }

Rect類、RectF類大多數(shù)方法都沒有檢測是否為有效矩形,這一點(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類型),因為直接除以2小數(shù)點(diǎn)后會舍去,所以乘以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;
    }
給矩形四個成員變量賦值

    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的正負(fù)代表移動的方向
     */
    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,左右兩邊向內(nèi)移動,矩形變窄
     *  dx<0,左右兩邊向外移動,矩形變寬
     *   見下圖
     *  dy同理,不過移動的是上、下兩邊
     */
    public void inset(int dx, int dy) {
        left += dx;
        top += dy;
        right -= dx;
        bottom -= dy;
    }

    /**
     *  left 、top、right 、bottom   矩形四條邊移動的距離
     *   正負(fù)代表移動的方向,為正時,朝內(nèi)移動,為負(fù)時,朝外移動
     */
    public void inset(int left, int top, int right, int bottom) {
        this.left += left;
        this.top += top;
        this.right -= right;
        this.bottom -= bottom;
    }

    /**
     * 根據(jù)另外一個Rect 對象的四個成員變量移動
     */
    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  // 檢測是否為有效矩形
               && x >= left && x < right && y >= top && y < bottom;//檢測矩形中是否包含點(diǎn)(x,y)
    }

    /**
     * 判斷另外一個矩形是否在當(dāng)前矩形內(nèi)
     */
    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
    }

    /**
     * 判斷另外一個矩形是否在當(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對象中
     * 
     * 返回值代表是否有交集,有交集為true,反之,false
     * 當(dāng)有交集時,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對象和當(dāng)前Rect做交集運(yùn)算,結(jié)果保存在當(dāng)前Rect對象中
     */
    public boolean intersect(Rect r) {
        return intersect(r.left, r.top, r.right, r.bottom);
    }

    /**
     * 傳入兩個Rect對象做交集運(yùn)算,結(jié)果保存在當(dāng)前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)矩形的并集運(yùn)算
    /**
     * 傳入left 、top、right、bottom和當(dāng)前Rect做并集運(yùn)算,結(jié)果保存在當(dāng)前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、判斷當(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對象中
                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é)果矩形是取四個方向的最大值,新的大矩形作為結(jié)果保存在當(dāng)前Rect對象中


二、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)、將一個Rect對象轉(zhuǎn)換為RectF對象

RectF類有一個構(gòu)造方法,可以將一個Rect對象轉(zhuǎn)換為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對象轉(zhuǎn)換為Rect對象

RectF中定義了兩個方法,可以傳入一個Rect對象,然后將當(dāng)前RectF對象的4個成員變量處理后設(shè)置給Rect對象的成員變量

    
    /**
     * RectF的4個成員變量四舍五入后設(shè)置給傳入的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向上取整,然后設(shè)置給傳入的Rect對象
     */
    public void roundOut(Rect dst) {
        dst.set((int) Math.floor(left), (int) Math.floor(top),
                (int) Math.ceil(right), (int) Math.ceil(bottom));
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容