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阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容