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):
当调用rect_1.intersect(rect_2)后,则rect_1变成下图中绿色矩形:
3.合并矩形
public void union(Rect r)
取矩形最小左上角,最大右下角点为基准,绘制矩形。效果图: