Rect是一个final类,不属于被继承,实现Parcelable接口执行序列化,声明public作用域的四个整型属性:left、top、right和bottom,用来记录View矩形局域的四个顶点坐标。
- public Rect() {}
1、创建一个空的Rect对象,left、top、right和bottom的默认值为0
- public Rect(int left, int top, int right, int bottom) {
- this.left = left;
- this.top = top;
- this.right = right;
- this.bottom = bottom;
- }
2、创建一个指定坐标值的Rect对象,left、top、right和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;
- }
- }
3、使用已知的Rect,创建一个新的Rect对象,left、top、right和bottom为已知的Rect包含的值
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- Rect r = (Rect) o;
- return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
- }
4、判断当前Rect与指定的o是否同一个,相同的条件:属于同一个对象或者两者left、top、right或bottom属性值一样
- @Override
- public int hashCode() {
- int result = left;
- result = 31 * result + top;
- result = 31 * result + right;
- result = 31 * result + bottom;
- return result;
- }
5、计算Rect属性值的散列码
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder(32);
- sb.append("Rect("); sb.append(left); sb.append(", ");
- sb.append(top); sb.append(" - "); sb.append(right);
- sb.append(", "); sb.append(bottom); sb.append(")");
- return sb.toString();
- }
6、以Rect(left,top-right,bottom)的格式返回矩形四个坐标值
- public String toShortString(StringBuilder sb) {
- sb.setLength(0);
- sb.append('['); sb.append(left); sb.append(',');
- sb.append(top); sb.append("]["); sb.append(right);
- sb.append(','); sb.append(bottom); sb.append(']');
- return sb.toString();
- }
7、以[left,top] [right,bottom]的格式返回矩形四个坐标值,即矩形区域左上角和右下角坐标
- public String toShortString() {
- return toShortString(new StringBuilder(32));
- }
8、以[left,top] [right,bottom]的格式返回矩形四个坐标值,即矩形区域左上角和右下角坐标,和上述方法一样
- public String flattenToString() {
- StringBuilder sb = new StringBuilder(32);
- // WARNING: Do not change the format of this string, it must be
- // preserved because Rects are saved in this flattened format.
- sb.append(left);
- sb.append(' ');
- sb.append(top);
- sb.append(' ');
- sb.append(right);
- sb.append(' ');
- sb.append(bottom);
- return sb.toString();
- }
9、以left top right bottom的格式返回矩形四个坐标值,即平铺的格式,比如:0 0 400 400或 100 100 800 300
- public static Rect unflattenFromString(String str) {
- Matcher matcher = UnflattenHelper.getMatcher(str);
- if (!matcher.matches()) {
- return null;
- }
- return new Rect(Integer.parseInt(matcher.group(1)),
- Integer.parseInt(matcher.group(2)),
- Integer.parseInt(matcher.group(3)),
- Integer.parseInt(matcher.group(4)));
- }
10、给定一个平铺格式的字符串,比如:0 0 400 400,判断是否合法,然后转换为一个Rect对象
- public void printShortString(PrintWriter pw) {
- pw.print('['); pw.print(left); pw.print(',');
- pw.print(top); pw.print("]["); pw.print(right);
- pw.print(','); pw.print(bottom); pw.print(']');
- }
11、将Rect包含的属性值以[left,top] [right,bottom]的格式写入给定的PrintWriter流中
- public final boolean isEmpty() {
- return left >= right || top >= bottom;
- }
12、判断Rect是否一个空对象,即包含的属性值是否不为0
- public final int width() {
- return right - left;
- }
13、计算矩形区域的宽度
- public final int height() {
- return bottom - top;
- }
14、计算矩形区域的高度
- public final int centerX() {
- return (left + right) >> 1;
- }
15、计算矩形区域的水平中心点,计算结果为分数则返回最接近的整型数,例如:水平中心点400
- public final int centerY() {
- return (top + bottom) >> 1;
- }
16、计算矩形区域的垂直中心点,计算结果为分数则返回最接近的整型数,例如:垂直中心点850
- public final float exactCenterX() {
- return (left + right) * 0.5f;
- }
17、计算矩形区域的水平中心点,返回结果float类型,例如:水平中心点400.0
- public final float exactCenterY() {
- return (top + bottom) * 0.5f;
- }
18、计算矩形区域的垂直中心点,返回结果float类型,例如:垂直中心点850.0
- public void setEmpty() {
- left = right = top = bottom = 0;
- }
19、将Rect对象包含的属性值设置为0
- public void set(int left, int top, int right, int bottom) {
- this.left = left;
- this.top = top;
- this.right = right;
- this.bottom = bottom;
- }
20、将Rect的属性值设置为指定的值
- public void set(Rect src) {
- this.left = src.left;
- this.top = src.top;
- this.right = src.right;
- this.bottom = src.bottom;
- }
21、复制指定的Rect对象包含的属性值
- public void offset(int dx, int dy) {
- left += dx;
- top += dy;
- right += dx;
- bottom += dy;
- }
22、在当前矩形区域的水平方向、垂直方向分别增加dx、dy距离,即扩展
- public void offsetTo(int newLeft, int newTop) {
- right += newLeft - left;
- bottom += newTop - top;
- left = newLeft;
- top = newTop;
- }
23、在当前矩形区域的水平方向、垂直方向分别偏移dx、dy距离,即水平平移dx、垂直平移dy
- public void inset(int dx, int dy) {
- left += dx;
- top += dy;
- right -= dx;
- bottom -= dy;
- }
24、在当前矩形区域的水平方向、垂直方向分别减少dx、dy距离,即缩小
- public boolean contains(int x, int y) {
- return left < right && top < bottom // check for empty first
- && x >= left && x < right && y >= top && y < bottom;
- }
25、计算指定的坐标(x,y)是否包含在矩形区域范围内,包含返回true,否则返回false
- public boolean contains(int left, int top, int right, int bottom) {
- // check for empty first
- return this.left < this.right && this.top < this.bottom
- // now check for containment
- && this.left <= left && this.top <= top
- && this.right >= right && this.bottom >= bottom;
- }
26、计算指定的left、top、right、bottom顶点是否包含在矩形区域范围内,包含返回true,否则返回false
- public boolean contains(Rect r) {
- // check for empty first
- return this.left < this.right && this.top < this.bottom
- // now check for containment
- && left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom;
- }
27、计算指定的Rect是否包含在矩形区域范围内,包含返回true,否则返回false
- 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;
- }
28、计算当前Rect与指定的left、top、right、bottom顶点是否存在交集区域,存在返回true并且返回指定坐标,否则返回false
- public boolean intersect(Rect r) {
- return intersect(r.left, r.top, r.right, r.bottom);
- }
29、计算当前Rect与指定的Rect是否存在交集区域,存在返回true并且返回指定坐标,否则返回false
- 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;
- }
30、计算指定的a、b是否存在交集区域,存在返回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;
- }
31、计算当前Rect与指定的left、top、right、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;
- }
32、计算指定的a、b是否存在交集区域,存在返回true并且不返回最大坐标,否则返回false
- 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;
- }
- }
- }
33、计算当前Rect与指定的left、top、right、bottom顶点是否存在并集区域,存在更新当前矩形区域,否则不更新
- public void union(Rect r) {
- union(r.left, r.top, r.right, r.bottom);
- }
34、计算当前Rect与指定的Rect是否存在并集区域,存在更新当前矩形区域,否则不更新
- public void union(int x, int y) {
- if (x < left) {
- left = x;
- } else if (x > right) {
- right = x;
- }
- if (y < top) {
- top = y;
- } else if (y > bottom) {
- bottom = y;
- }
- }
35、计算当前Rect与指定的坐标(x,y)是否存在并集区域,存在更新当前矩形区域,否则不更新
- public void sort() {
- if (left > right) {
- int temp = left;
- left = right;
- right = temp;
- }
- if (top > bottom) {
- int temp = top;
- top = bottom;
- bottom = temp;
- }
- }
36、排序当前矩形区域,符合:left
- public void scale(float scale) {
- if (scale != 1.0f) {
- left = (int) (left * scale + 0.5f);
- top = (int) (top * scale + 0.5f);
- right = (int) (right * scale + 0.5f);
- bottom = (int) (bottom * scale + 0.5f);
- }
- }
37、按照指定的值缩放当前矩形区域
- public void scaleRoundIn(float scale) {
- if (scale != 1.0f) {
- left = (int) Math.ceil(left * scale);
- top = (int) Math.ceil(top * scale);
- right = (int) Math.floor(right * scale);
- bottom = (int) Math.floor(bottom * scale);
- }
- }
38、按照指定的值缩放当前矩形区域