0
点赞
收藏
分享

微信扫一扫

Android Rect相关方法

陈情雅雅 2022-03-11 阅读 65
android

1.检测矩形是否包含某个点

public boolean contains(int x, int y)

2.检测矩形是否相交

矩形的Rect.intersect(Rect a)方法是用来取两个矩形的相交部分,并设置给Rect。

@CheckResult

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;

}

 

静态方法检测矩形是否相交:

public static boolean intersects(Rect a, Rect b)

实例变量检测是否相交:

public boolean intersect(Rect r)

实例变量和静态变量检测是否相交的区别在于,实例变量检测是否相交并不会把相交矩形返回,而实例变量则会矩形返回。

举个例子:

原图,相交的两个矩形(rect_1、rect_2):

14bf87760895410293efb7c5deee5791.png

 当调用rect_1.intersect(rect_2)后,则rect_1变成下图中绿色矩形:

2580a6d23e7145d7aa4992e1a92a4b86.png

 

3.合并矩形

public void union(Rect r)

取矩形最小左上角,最大右下角点为基准,绘制矩形。效果图:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5a2f6Iqz6Iqz,size_7,color_FFFFFF,t_70,g_se,x_16

 

 

 

举报

相关推荐

0 条评论