自定义View-Rect和RectF

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32113133/article/details/66969990
Rect 类定义了一个矩形结构,同样实现了 Parcelable 序列化接口。Rect 类定义了 left、top、right、bottom 四个成员变量,我们需要正确理解这 4 个成员变量的作用:
left:矩形左边线条离 y 轴的距离
top:矩形上面线条离 x 轴的距离
right:矩形右边线条离 y 轴的距离
bottom:矩形底部线条离 x 轴的距离

矩形是一种非常常见的图形结构,并且能衍生出更多的图形,如椭圆、扇形、弧线等等;矩形还能进行各种图形运算,如交集、并集等等,所以,与之对应的 Rect 类功能也更加复杂。有人会疑惑为什么不直接指定左上角的坐标、宽度和高度来确定一个矩形,因为指定 top、left、right 和 bottom 更符合坐标系的数学逻辑,也能更好的支持矩形的计算。

Rect 的主要功能有:
1)初始化:主要有两种初始化的方法:一是直接指定 left、top、right、bottom 等 4 个成员变量的值,二是从另一个 Rect 对象中复制。下面是 Rect 的三个构造方法:
  Rect()
  Rect(int left,int top,int right,int bottom)
  Rect(Rect r)

2)增值计算:根据 left、top、right、bottom 等 4 个成员变量计算矩形的宽度、高度或中心点的坐标,主要的方法定义如下:
 public final boolean isEmpty(){
       return left>=right ||top>= bottom;
 }

判断 Rect 是否为空,也就是矩形区域面积是否为 0 或者为无效矩形。


 public final int width(){
      return right - left;
 }

返回矩形的宽度。


 public final int height(){
     return bottom - top;
 }

返回矩形的高度。

public final int centerX(){
     return (left + right) >> 1;
}
计算矩形中心点的 x 坐标,右移一位相当于除以 2,移位运算比普通的除法运算效率

更高。


public final int centerY(){
    return (top + bottom) >> 1;
}

计算矩形中心点的 y 坐标。


public final float exactCenterX(){
   return (left + right) * 0.5f;
}

计算矩形中心点的 x 坐标,返回 float 类型,结果更精确。


 public final float exactCenterY(){
   return (top + bottom) * 0.5f;
 }

计算矩形中心点的 y 坐标,返回 float 类型,结果更精确。


3)改变矩形的位置或大小,通过修改 left、top、right 和 bottom 等 4 个成员变量的值,获取矩形位置平移、放大、缩小等结果。
public void setEmpty(){
  left = right = top = bottom = 0;
}

将矩形的 left、top、right 和 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;
}

给 left、top、right 和 bottom 重新赋值。


 public void set(Rect src){
  this.left = src.left;
  this.top = src.top;
  this.right = src.right;
  this.bottom = src.bottom;
}


矩形的 left、top、right 和 bottom 来自于另一个矩形 src。
public void offset(int dx,int dy){
  left += dx;
  top += dy;
  right += dx;
  bottom += dy;
}
矩形的 left 和 right 同时移动相同的距离 dx,矩形的 top 和 bottom 同时移动相同的距

离 dy,实际上就是将矩形移动(dx、dy)距离,正负决定移动的方向。


public void offsetTo(int newLeft,int newTop){
  right += newLeft - left;
  bottom += newTop - top;
  left = newLeft;
  top = newTop;
}

offsetTo()方法也是移位,和 offset()不同的是前者是绝对定位,后者是相对定位。


public void inset(int dx,int dy){
  left += dx;
  top += dy;
  right -= dx;
  bottom -= dy;
}
实现了矩形的缩放功能,缩放中心点就是矩形的中心点,要注意的是 dx、dy 为正数时
表示缩小,负数表示放大。
4)包含测试:支持一个点是否位于矩形内和一个矩形是否位于另一个矩形内。
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
             && this.right >= right && this.bottom >= bottom;
}

判断传递过来的矩形是否位于矩形内。


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;
}
判断传递过来的矩形是否位于矩形内。


矩形的交集与并集运算:交集是指两个矩形相交的公共部分,并集是指两个矩形所占
有最大面积区域。
主要的方法如下:
 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 的 left、top、right、bottom,并将构建的 Rect 对象与当前 Rect 对象做交集运算,结果保存在当前 Rect 对象中。

 public boolean intersect(Rect r){
            return intersect(r.left, r.top, r.right, r.bottom);
 }

传入新的 Rect 对象,并将该对象与当前 Rect 对象做交集运算,结果保存在当前 Rect对象中。比如有下面的代码段:

 Rect rect1 = new Rect(0, 0, 400, 400);
 Rect rect2 = new Rect(200, 200, 600, 600);
 rect1.intersect(rect2);
此时,rect1 的 left、top、right、bottom 属性被改变了,分别为 200、200、400、400,
public void union(int left,int top,int right,int bottom){
            if ((left < right) && (top < bottom)) {
                if ((this.left < this.right) && (this.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;
                } else {
                    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);
}
union()方法是计算两个矩形的并集,传入一个新的 Rect,与当前 Rect 进行并集运算,并将结果保存在当前 Rect 对象中。比如有下面的代码段:
 Rect rect1 = new Rect(0, 0, 400, 400);
 Rect rect2 = new Rect(200, 200, 600, 600);
 rect1.union(rect2);
运行后与交集一样,最终的结果保存在 rect1 对象中, rect1 的 left、top、right、bottom属性值分别为:0,0,600,600,也就是说,并集取的是四个方向的最大值。与 Rect 类类似的还有 RectF 类,RectF 类的代码实现与 Rect 如出一辙,主要的不同是 Rect的 left、top、right、bottom 四个成员变量为 int 类型,而 RectF 为 float 类型。在开发中,常常会出现 Rect 与 RectF 相互转换的情况,Rect 类中没有定义与 RectF 相关的任何信息,但在 RectF 类中,则定义了二者相互转换的方法。RectF 转换成 Rect。RectF 定义了两个名为 round 和 roundOut 的方法,round()方法将 RectF类的类型为 float 的 left、top、right、bottom 属性以四舍五入的方式转换成 int 再通过 Rect 类型的参数传回,roundOut()方法虽然和 round()差不多,但在某些情况下返回的矩形区域要大些。


如果还有疑问,扒开源代码探个究竟,我们发现,roundOut()方法中获取 left 和 top 时调用了 FloatMath.floor()方法,该方法返回小于参数的最大值,如 FloatMath.floor(3.5)返回 3;而获取 right 和 bottom 调用了 FloatMath.ceil()方法,该方法返回大于参数的最小值,如
FloatMath.ceil(5.2)返回 6。
 public void round(Rect dst){
            dst.set(FastMath.round(left), FastMath.round(top),
                    FastMath.round(right), FastMath.round(bottom));
 }

public void roundOut(Rect dst){
        dst.set((int) FloatMath.floor(left), (int) FloatMath.floor(top),
                (int) FloatMath.ceil(right), (int) FloatMath.ceil(bottom));
}

Rect 转换成 RectF 就相对简单了,实例化 RectF 时,构造方法支持传递 Rect 对象作为参数:
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;
       }
}


猜你喜欢

转载自blog.csdn.net/qq_32113133/article/details/66969990