Rect、RectF方法解析

Rect是一个final类,不属于被继承,实现Parcelable接口执行序列化,声明public作用域的四个整型属性:left、top、right和bottom,用来记录View矩形局域的四个顶点坐标。

  1. public Rect() {}  

1、创建一个空的Rect对象,left、top、right和bottom的默认值为0

  1. public Rect(int left, int top, int right, int bottom) {  
  2.     this.left = left;  
  3.     this.top = top;  
  4.     this.right = right;  
  5.     this.bottom = bottom;  
  6. }  

2、创建一个指定坐标值的Rect对象,left、top、right和bottom为指定值

  1. public Rect(Rect r) {  
  2.     if (r == null) {  
  3.         left = top = right = bottom = 0;  
  4.     } else {  
  5.         left = r.left;  
  6.         top = r.top;  
  7.         right = r.right;  
  8.         bottom = r.bottom;  
  9.     }  
  10. }  

3、使用已知的Rect,创建一个新的Rect对象,left、top、right和bottom为已知的Rect包含的值

  1. @Override  
  2. public boolean equals(Object o) {  
  3.     if (this == o) return true;  
  4.     if (o == null || getClass() != o.getClass()) return false;  
  5.   
  6.     Rect r = (Rect) o;  
  7.     return left == r.left && top == r.top && right == r.right && bottom == r.bottom;  
  8. }  

4、判断当前Rect与指定的o是否同一个,相同的条件:属于同一个对象或者两者left、top、right或bottom属性值一样

  1. @Override  
  2. public int hashCode() {  
  3.     int result = left;  
  4.     result = 31 * result + top;  
  5.     result = 31 * result + right;  
  6.     result = 31 * result + bottom;  
  7.     return result;  
  8. }  

5、计算Rect属性值的散列码

  1. @Override  
  2. public String toString() {  
  3.     StringBuilder sb = new StringBuilder(32);  
  4.     sb.append("Rect("); sb.append(left); sb.append(", ");  
  5.     sb.append(top); sb.append(" - "); sb.append(right);  
  6.     sb.append(", "); sb.append(bottom); sb.append(")");  
  7.     return sb.toString();  
  8. }  

6、以Rect(left,top-right,bottom)的格式返回矩形四个坐标值

  1. public String toShortString(StringBuilder sb) {  
  2.     sb.setLength(0);  
  3.     sb.append('['); sb.append(left); sb.append(',');  
  4.     sb.append(top); sb.append("]["); sb.append(right);  
  5.     sb.append(','); sb.append(bottom); sb.append(']');  
  6.     return sb.toString();  
  7. }  

7、以[left,top] [right,bottom]的格式返回矩形四个坐标值,即矩形区域左上角和右下角坐标

  1. public String toShortString() {  
  2.     return toShortString(new StringBuilder(32));  
  3. }  

8、以[left,top] [right,bottom]的格式返回矩形四个坐标值,即矩形区域左上角和右下角坐标,和上述方法一样

  1. public String flattenToString() {  
  2.     StringBuilder sb = new StringBuilder(32);  
  3.     // WARNING: Do not change the format of this string, it must be  
  4.     // preserved because Rects are saved in this flattened format.  
  5.     sb.append(left);  
  6.     sb.append(' ');  
  7.     sb.append(top);  
  8.     sb.append(' ');  
  9.     sb.append(right);  
  10.     sb.append(' ');  
  11.     sb.append(bottom);  
  12.     return sb.toString();  
  13. }  

9、以left top right bottom的格式返回矩形四个坐标值,即平铺的格式,比如:0 0 400 400或 100 100 800 300

  1. public static Rect unflattenFromString(String str) {  
  2.     Matcher matcher = UnflattenHelper.getMatcher(str);  
  3.     if (!matcher.matches()) {  
  4.         return null;  
  5.     }  
  6.     return new Rect(Integer.parseInt(matcher.group(1)),  
  7.             Integer.parseInt(matcher.group(2)),  
  8.             Integer.parseInt(matcher.group(3)),  
  9.             Integer.parseInt(matcher.group(4)));  
  10. }  

10、给定一个平铺格式的字符串,比如:0 0 400 400,判断是否合法,然后转换为一个Rect对象

  1. public void printShortString(PrintWriter pw) {  
  2.     pw.print('['); pw.print(left); pw.print(',');  
  3.     pw.print(top); pw.print("]["); pw.print(right);  
  4.     pw.print(','); pw.print(bottom); pw.print(']');  
  5. }  

11、将Rect包含的属性值以[left,top] [right,bottom]的格式写入给定的PrintWriter流中

  1. public final boolean isEmpty() {  
  2.     return left >= right || top >= bottom;  
  3. }  

12、判断Rect是否一个空对象,即包含的属性值是否不为0

  1. public final int width() {  
  2.     return right - left;  
  3. }  

13、计算矩形区域的宽度

  1. public final int height() {  
  2.     return bottom - top;  
  3. }  

14、计算矩形区域的高度

  1. public final int centerX() {  
  2.     return (left + right) >> 1;  
  3. }  

15、计算矩形区域的水平中心点,计算结果为分数则返回最接近的整型数,例如:水平中心点400

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

16、计算矩形区域的垂直中心点,计算结果为分数则返回最接近的整型数,例如:垂直中心点850

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

17、计算矩形区域的水平中心点,返回结果float类型,例如:水平中心点400.0

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

18、计算矩形区域的垂直中心点,返回结果float类型,例如:垂直中心点850.0

  1. public void setEmpty() {  
  2.     left = right = top = bottom = 0;  
  3. }  

19、将Rect对象包含的属性值设置为0

  1. public void set(int left, int top, int right, int bottom) {  
  2.     this.left = left;  
  3.     this.top = top;  
  4.     this.right = right;  
  5.     this.bottom = bottom;  
  6. }  

20、将Rect的属性值设置为指定的值

  1. public void set(Rect src) {  
  2.     this.left = src.left;  
  3.     this.top = src.top;  
  4.     this.right = src.right;  
  5.     this.bottom = src.bottom;  
  6. }  

21、复制指定的Rect对象包含的属性值

  1. public void offset(int dx, int dy) {  
  2.     left += dx;  
  3.     top += dy;  
  4.     right += dx;  
  5.     bottom += dy;  
  6. }  

22、在当前矩形区域的水平方向、垂直方向分别增加dx、dy距离,即扩展

  1. public void offsetTo(int newLeft, int newTop) {  
  2.     right += newLeft - left;  
  3.     bottom += newTop - top;  
  4.     left = newLeft;  
  5.     top = newTop;  
  6. }  

23、在当前矩形区域的水平方向、垂直方向分别偏移dx、dy距离,即水平平移dx、垂直平移dy

  1. public void inset(int dx, int dy) {  
  2.     left += dx;  
  3.     top += dy;  
  4.     right -= dx;  
  5.     bottom -= dy;  
  6. }  

24、在当前矩形区域的水平方向、垂直方向分别减少dx、dy距离,即缩小

  1. public boolean contains(int x, int y) {  
  2.     return left < right && top < bottom  // check for empty first  
  3.            && x >= left && x < right && y >= top && y < bottom;  
  4. }  

25、计算指定的坐标(x,y)是否包含在矩形区域范围内,包含返回true,否则返回false

  1. public boolean contains(int left, int top, int right, int bottom) {  
  2.            // check for empty first  
  3.     return this.left < this.right && this.top < this.bottom  
  4.            // now check for containment  
  5.             && this.left <= left && this.top <= top  
  6.             && this.right >= right && this.bottom >= bottom;  
  7. }  

26、计算指定的left、top、right、bottom顶点是否包含在矩形区域范围内,包含返回true,否则返回false

  1. public boolean contains(Rect r) {  
  2.            // check for empty first  
  3.     return this.left < this.right && this.top < this.bottom  
  4.            // now check for containment  
  5.            && left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom;  
  6. }  

27、计算指定的Rect是否包含在矩形区域范围内,包含返回true,否则返回false

  1. public boolean intersect(int left, int top, int right, int bottom) {  
  2.     if (this.left < right && left < this.right && this.top < bottom && top < this.bottom) {  
  3.         if (this.left < left) this.left = left;  
  4.         if (this.top < top) this.top = top;  
  5.         if (this.right > right) this.right = right;  
  6.         if (this.bottom > bottom) this.bottom = bottom;  
  7.         return true;  
  8.     }  
  9.     return false;  
  10. }  

28、计算当前Rect与指定的left、top、right、bottom顶点是否存在交集区域,存在返回true并且返回指定坐标,否则返回false

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

29、计算当前Rect与指定的Rect是否存在交集区域,存在返回true并且返回指定坐标,否则返回false

  1. public boolean setIntersect(Rect a, Rect b) {  
  2.     if (a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom) {  
  3.         left = Math.max(a.left, b.left);  
  4.         top = Math.max(a.top, b.top);  
  5.         right = Math.min(a.right, b.right);  
  6.         bottom = Math.min(a.bottom, b.bottom);  
  7.         return true;  
  8.     }  
  9.     return false;  
  10. }  

30、计算指定的a、b是否存在交集区域,存在返回true并且返回最大坐标,否则返回false

  1. public boolean intersects(int left, int top, int right, int bottom) {  
  2.     return this.left < right && left < this.right && this.top < bottom && top < this.bottom;  
  3. }  

31、计算当前Rect与指定的left、top、right、bottom顶点是否存在交集区域,存在返回true并且不返回指定坐标,否则返回false

  1. public static boolean intersects(Rect a, Rect b) {  
  2.     return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;  
  3. }  

32、计算指定的a、b是否存在交集区域,存在返回true并且不返回最大坐标,否则返回false

  1. public void union(int left, int top, int right, int bottom) {  
  2.     if ((left < right) && (top < bottom)) {  
  3.         if ((this.left < this.right) && (this.top < this.bottom)) {  
  4.             if (this.left > left) this.left = left;  
  5.             if (this.top > top) this.top = top;  
  6.             if (this.right < right) this.right = right;  
  7.             if (this.bottom < bottom) this.bottom = bottom;  
  8.         } else {  
  9.             this.left = left;  
  10.             this.top = top;  
  11.             this.right = right;  
  12.             this.bottom = bottom;  
  13.         }  
  14.     }  
  15. }  

33、计算当前Rect与指定的left、top、right、bottom顶点是否存在集区域,存在更新当前矩形区域,否则不更新

  1. public void union(Rect r) {  
  2.     union(r.left, r.top, r.right, r.bottom);  
  3. }  

34、计算当前Rect与指定的Rect是否存在集区域,存在更新当前矩形区域,否则不更新

  1. public void union(int x, int y) {  
  2.     if (x < left) {  
  3.         left = x;  
  4.     } else if (x > right) {  
  5.         right = x;  
  6.     }  
  7.     if (y < top) {  
  8.         top = y;  
  9.     } else if (y > bottom) {  
  10.         bottom = y;  
  11.     }  
  12. }  

35、计算当前Rect与指定的坐标(x,y)是否存在集区域,存在更新当前矩形区域,否则不更新

  1. public void sort() {  
  2.     if (left > right) {  
  3.         int temp = left;  
  4.         left = right;  
  5.         right = temp;  
  6.     }  
  7.     if (top > bottom) {  
  8.         int temp = top;  
  9.         top = bottom;  
  10.         bottom = temp;  
  11.     }  
  12. }  

36、排序当前矩形区域,符合:left

  1. public void scale(float scale) {  
  2.     if (scale != 1.0f) {  
  3.         left = (int) (left * scale + 0.5f);  
  4.         top = (int) (top * scale + 0.5f);  
  5.         right = (int) (right * scale + 0.5f);  
  6.         bottom = (int) (bottom * scale + 0.5f);  
  7.     }  
  8. }  

37、按照指定的值缩放当前矩形区域

  1. public void scaleRoundIn(float scale) {  
  2.     if (scale != 1.0f) {  
  3.         left = (int) Math.ceil(left * scale);  
  4.         top = (int) Math.ceil(top * scale);  
  5.         right = (int) Math.floor(right * scale);  
  6.         bottom = (int) Math.floor(bottom * scale);  
  7.     }  
  8. }  

38、按照指定的值缩放当前矩形区域

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值