Rect类内有四个int型成员变量,分别为left,tope,  right,bottom

可以理解为一个矩形的左上坐标的X,Y轴坐标值,右下坐标的X,Y轴坐标值

这四个值确定后可以明确或说代表一个矩形范围。

该类有以下常用方法:

1.用以得到该矩形的宽高,中点坐标

2.判断与其它点,矩形的位置关系(是否相等,是否相交,是否包括)

3.对矩形两坐标的设置操作(改变某坐标点,拉伸,内缩,位移,缩放)

方法 作用
boolean equals(Object o) 两个矩形的两点坐标是否相同
int hashCode()  
toString() 生成带有两坐标点信息的字符串
boolean isEmpty() 判断矩形信息是否有效,右边坐标点的x轴及Y轴坐标大于左边的
int width() 获得矩形的宽度
int height() 获得矩形的高度
int centerX() 获得矩形的X轴中点坐标
int centerY() 获得矩形的Y轴中点坐标
float exactCenterX() 获得矩形的X轴中点坐标(float)
float exactCenterY() 获得矩形的Y轴中点坐标(float)
void setEmpty() 设置两点坐标值都为0
void set(int left, int top, int right, int bottom) 具体设置两点坐标值
void set(Rect src) 设置两点坐标值和src的两点坐标值一样
void offset(int dx, int dy) 让矩形x轴偏移dx,y轴偏移dy
void offsetTo(int newLeft, int newTop) 让矩形的左边x坐标为newLeft,y坐标为newTop,宽高不变
void inset(int dx, int dy) 让两点坐标值x轴方向内缩dx,y轴方向内缩dy(left+dx,right-dx,top+dy,bottom-dy)
void inset(Rect insets) 让两点坐标值相对insets内缩
void inset(int left, int top, int right, int bottom) 让两点坐标值相对两点坐标值内缩
boolean contains(int x, int y) 判断x,y代表的点是否在矩形内
boolean contains(int left, int top, int right, int bottom) 判断两点坐标值代表的点是否在矩形内
boolean contains(Rect r) 判断r代表的矩形是否在矩形内
boolean intersect(int left, int top, int right, int bottom) 判断两点坐标值代表的矩形与当前矩形是否相交,若相交让当前矩形坐标值为相交的范围
boolean setIntersect(Rect a, Rect b) 判断a,b两矩形是否相交,若相交让当前矩形坐标值为相交的范围
boolean intersects(int left, int top, int right, int bottom) 判断两点坐标值代表的矩形与当前矩形是否相交
boolean intersects(Rect a, Rect b) 判断a,b两矩形是否相交
union(int left, int top, int right, int bottom) 扩充矩形使两点坐标值代表的矩形在矩形内
void union(Rect r) 扩充矩形使r代表的矩形在矩形内
union(int x, int y) 扩充矩形使x,y代表的点在矩形内
sort()  排序,使大的坐标值为右边坐标点坐标值
void scale(float scale) 让两点坐标值都乘以scale
有兴趣的查看以下源代码:
public final class Rect implements Parcelable {public int left;public int top;public int right;public int bottom;/*** A helper class for flattened rectange pattern recognition. A separate* class to avoid an initialization dependency on a regular expression* causing Rect to not be initializable with an ahead-of-time compilation* scheme.*/private static final class UnflattenHelper {private static final Pattern FLATTENED_PATTERN = Pattern.compile("(-?\\d+) (-?\\d+) (-?\\d+) (-?\\d+)");static Matcher getMatcher(String str) {return FLATTENED_PATTERN.matcher(str);}}/*** Create a new empty Rect. All coordinates are initialized to 0.*/public Rect() {}/*** Create a new rectangle with the specified coordinates. Note: no range* checking is performed, so the caller must ensure that left <= right and* top <= bottom.** @param left   The X coordinate of the left side of the rectangle* @param top    The Y coordinate of the top of the rectangle* @param right  The X coordinate of the right side of the rectangle* @param bottom The Y coordinate of the bottom of the rectangle*/public Rect(int left, int top, int right, int bottom) {this.left = left;this.top = top;this.right = right;this.bottom = bottom;}/*** Create a new rectangle, initialized with the values in the specified* rectangle (which is left unmodified).** @param r The rectangle whose coordinates are copied into the new*          rectangle.*/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;}}@Overridepublic 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;}@Overridepublic int hashCode() {int result = left;result = 31 * result + top;result = 31 * result + right;result = 31 * result + bottom;return result;}@Overridepublic 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();}/*** Return a string representation of the rectangle in a compact form.*/public String toShortString() {return toShortString(new StringBuilder(32));}/*** Return a string representation of the rectangle in a compact form.* @hide*/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();}/*** Return a string representation of the rectangle in a well-defined format.** <p>You can later recover the Rect from this string through* {@link #unflattenFromString(String)}.* * @return Returns a new String of the form "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();}/*** Returns a Rect from a string of the form returned by {@link #flattenToString},* or null if the string is not of that form.*/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)));}/*** Print short representation to given writer.* @hide*/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(']');}/*** Returns true if the rectangle is empty (left >= right or top >= bottom)*/public final boolean isEmpty() {return left >= right || top >= bottom;}/*** @return the rectangle's width. This does not check for a valid rectangle* (i.e. left <= right) so the result may be negative.*/public final int width() {return right - left;}/*** @return the rectangle's height. This does not check for a valid rectangle* (i.e. top <= bottom) so the result may be negative.*/public final int height() {return bottom - top;}/*** @return the horizontal center of the rectangle. If the computed value*         is fractional, this method returns the largest integer that is*         less than the computed value.*/public final int centerX() {return (left + right) >> 1;}/*** @return the vertical center of the rectangle. If the computed value*         is fractional, this method returns the largest integer that is*         less than the computed value.*/public final int centerY() {return (top + bottom) >> 1;}/*** @return the exact horizontal center of the rectangle as a float.*/public final float exactCenterX() {return (left + right) * 0.5f;}/*** @return the exact vertical center of the rectangle as a float.*/public final float exactCenterY() {return (top + bottom) * 0.5f;}/*** Set the rectangle to (0,0,0,0)*/public void setEmpty() {left = right = top = bottom = 0;}/*** Set the rectangle's coordinates to the specified values. Note: no range* checking is performed, so it is up to the caller to ensure that* left <= right and top <= bottom.** @param left   The X coordinate of the left side of the rectangle* @param top    The Y coordinate of the top of the rectangle* @param right  The X coordinate of the right side of the rectangle* @param bottom The Y coordinate of the bottom of the rectangle*/public void set(int left, int top, int right, int bottom) {this.left = left;this.top = top;this.right = right;this.bottom = bottom;}/*** Copy the coordinates from src into this rectangle.** @param src The rectangle whose coordinates are copied into this*           rectangle.*/public void set(Rect src) {this.left = src.left;this.top = src.top;this.right = src.right;this.bottom = src.bottom;}/*** Offset the rectangle by adding dx to its left and right coordinates, and* adding dy to its top and bottom coordinates.** @param dx The amount to add to the rectangle's left and right coordinates* @param dy The amount to add to the rectangle's top and bottom coordinates*/public void offset(int dx, int dy) {left += dx;top += dy;right += dx;bottom += dy;}/*** Offset the rectangle to a specific (left, top) position,* keeping its width and height the same.** @param newLeft   The new "left" coordinate for the rectangle* @param newTop    The new "top" coordinate for the rectangle*/public void offsetTo(int newLeft, int newTop) {right += newLeft - left;bottom += newTop - top;left = newLeft;top = newTop;}/*** Inset the rectangle by (dx,dy). If dx is positive, then the sides are* moved inwards, making the rectangle narrower. If dx is negative, then the* sides are moved outwards, making the rectangle wider. The same holds true* for dy and the top and bottom.** @param dx The amount to add(subtract) from the rectangle's left(right)* @param dy The amount to add(subtract) from the rectangle's top(bottom)*/public void inset(int dx, int dy) {left += dx;top += dy;right -= dx;bottom -= dy;}/*** Insets the rectangle on all sides specified by the dimensions of the {@code insets}* rectangle.* @hide* @param insets The rectangle specifying the insets on all side.*/public void inset(Rect insets) {left += insets.left;top += insets.top;right -= insets.right;bottom -= insets.bottom;}/*** Insets the rectangle on all sides specified by the insets.* @hide* @param left The amount to add from the rectangle's left* @param top The amount to add from the rectangle's top* @param right The amount to subtract from the rectangle's right* @param bottom The amount to subtract from the rectangle's bottom*/public void inset(int left, int top, int right, int bottom) {this.left += left;this.top += top;this.right -= right;this.bottom -= bottom;}/*** Returns true if (x,y) is inside the rectangle. The left and top are* considered to be inside, while the right and bottom are not. This means* that for a x,y to be contained: left <= x < right and top <= y < bottom.* An empty rectangle never contains any point.** @param x The X coordinate of the point being tested for containment* @param y The Y coordinate of the point being tested for containment* @return true iff (x,y) are contained by the rectangle, where containment*              means left <= x < right and top <= y < bottom*/public boolean contains(int x, int y) {return left < right && top < bottom  // check for empty first&& x >= left && x < right && y >= top && y < bottom;}/*** Returns true iff the 4 specified sides of a rectangle are inside or equal* to this rectangle. i.e. is this rectangle a superset of the specified* rectangle. An empty rectangle never contains another rectangle.** @param left The left side of the rectangle being tested for containment* @param top The top of the rectangle being tested for containment* @param right The right side of the rectangle being tested for containment* @param bottom The bottom of the rectangle being tested for containment* @return true iff the the 4 specified sides of a rectangle are inside or*              equal to this rectangle*/public boolean contains(int left, int top, int right, int bottom) {// check for empty firstreturn this.left < this.right && this.top < this.bottom// now check for containment&& this.left <= left && this.top <= top&& this.right >= right && this.bottom >= bottom;}/*** Returns true iff the specified rectangle r is inside or equal to this* rectangle. An empty rectangle never contains another rectangle.** @param r The rectangle being tested for containment.* @return true iff the specified rectangle r is inside or equal to this*              rectangle*/public boolean contains(Rect r) {// check for empty firstreturn this.left < this.right && this.top < this.bottom// now check for containment&& left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom;}/*** If the rectangle specified by left,top,right,bottom intersects this* rectangle, return true and set this rectangle to that intersection,* otherwise return false and do not change this rectangle. No check is* performed to see if either rectangle is empty. Note: To just test for* intersection, use {@link #intersects(Rect, Rect)}.** @param left The left side of the rectangle being intersected with this*             rectangle* @param top The top of the rectangle being intersected with this rectangle* @param right The right side of the rectangle being intersected with this*              rectangle.* @param bottom The bottom of the rectangle being intersected with this*             rectangle.* @return true if the specified rectangle and this rectangle intersect*              (and this rectangle is then set to that intersection) else*              return false and do not change this rectangle.*/@CheckResultpublic 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;}/*** If the specified rectangle intersects this rectangle, return true and set* this rectangle to that intersection, otherwise return false and do not* change this rectangle. No check is performed to see if either rectangle* is empty. To just test for intersection, use intersects()** @param r The rectangle being intersected with this rectangle.* @return true if the specified rectangle and this rectangle intersect*              (and this rectangle is then set to that intersection) else*              return false and do not change this rectangle.*/@CheckResultpublic boolean intersect(Rect r) {return intersect(r.left, r.top, r.right, r.bottom);}/*** If rectangles a and b intersect, return true and set this rectangle to* that intersection, otherwise return false and do not change this* rectangle. No check is performed to see if either rectangle is empty.* To just test for intersection, use intersects()** @param a The first rectangle being intersected with* @param b The second rectangle being intersected with* @return true iff the two specified rectangles intersect. If they do, set*              this rectangle to that intersection. If they do not, return*              false and do not change this rectangle.*/@CheckResultpublic 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;}/*** Returns true if this rectangle intersects the specified rectangle.* In no event is this rectangle modified. No check is performed to see* if either rectangle is empty. To record the intersection, use intersect()* or setIntersect().** @param left The left side of the rectangle being tested for intersection* @param top The top of the rectangle being tested for intersection* @param right The right side of the rectangle being tested for*              intersection* @param bottom The bottom of the rectangle being tested for intersection* @return true iff the specified rectangle intersects this rectangle. In*              no event is this rectangle modified.*/public boolean intersects(int left, int top, int right, int bottom) {return this.left < right && left < this.right && this.top < bottom && top < this.bottom;}/*** Returns true iff the two specified rectangles intersect. In no event are* either of the rectangles modified. To record the intersection,* use {@link #intersect(Rect)} or {@link #setIntersect(Rect, Rect)}.** @param a The first rectangle being tested for intersection* @param b The second rectangle being tested for intersection* @return true iff the two specified rectangles intersect. In no event are*              either of the rectangles modified.*/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;}/*** Update this Rect to enclose itself and the specified rectangle. If the* specified rectangle is empty, nothing is done. If this rectangle is empty* it is set to the specified rectangle.** @param left The left edge being unioned with this rectangle* @param top The top edge being unioned with this rectangle* @param right The right edge being unioned with this rectangle* @param bottom The bottom edge being unioned with this rectangle*/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;}}}/*** Update this Rect to enclose itself and the specified rectangle. If the* specified rectangle is empty, nothing is done. If this rectangle is empty* it is set to the specified rectangle.** @param r The rectangle being unioned with this rectangle*/public void union(Rect r) {union(r.left, r.top, r.right, r.bottom);}/*** Update this Rect to enclose itself and the [x,y] coordinate. There is no* check to see that this rectangle is non-empty.** @param x The x coordinate of the point to add to the rectangle* @param y The y coordinate of the point to add to the rectangle*/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;}}/*** Swap top/bottom or left/right if there are flipped (i.e. left > right* and/or top > bottom). This can be called if* the edges are computed separately, and may have crossed over each other.* If the edges are already correct (i.e. left <= right and top <= bottom)* then nothing is done.*/public void sort() {if (left > right) {int temp = left;left = right;right = temp;}if (top > bottom) {int temp = top;top = bottom;bottom = temp;}}/*** Parcelable interface methods*/public int describeContents() {return 0;}/*** Write this rectangle to the specified parcel. To restore a rectangle from* a parcel, use readFromParcel()* @param out The parcel to write the rectangle's coordinates into*/public void writeToParcel(Parcel out, int flags) {out.writeInt(left);out.writeInt(top);out.writeInt(right);out.writeInt(bottom);}public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() {/*** Return a new rectangle from the data in the specified parcel.*/public Rect createFromParcel(Parcel in) {Rect r = new Rect();r.readFromParcel(in);return r;}/*** Return an array of rectangles of the specified size.*/public Rect[] newArray(int size) {return new Rect[size];}};/*** Set the rectangle's coordinates from the data stored in the specified* parcel. To write a rectangle to a parcel, call writeToParcel().** @param in The parcel to read the rectangle's coordinates from*/public void readFromParcel(Parcel in) {left = in.readInt();top = in.readInt();right = in.readInt();bottom = in.readInt();}/*** Scales up the rect by the given scale.* @hide*/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);}}}

android Rect相关推荐

  1. android Rect类的使用

    今天,讲讲Android的Rect类的使用. public final class Rect extends Object implements Parcelable java.lang.Object ...

  2. android Rect的使用

    转:http://byandby.javaeye.com/blog/826230 Java代码  1.   //绘制矩形 2.   canvas.drawRect(new Rect(150, 75,  ...

  3. Android Rect 的使用以及与RectF的区别

    Rect 保存矩形的四个整数坐标.矩形是由四条边(左.上.右下)的坐标表示, 绘制矩形的时候用得到 // 设置抗锯齿效果 true是去边缘会将锯齿模糊化paint.setAntiAlias(true) ...

  4. Android Rect相关方法

    1.Rect 常用的一个"绘画相关的工具类",常用来描述长方形/正方形,他只有4个属性: public int left; public int top; public int r ...

  5. jni android rect.h,解决 fatal error: jni_md.h: No such file or directory #include “jni_md.h”

    在Linux系统下使用jdk1.8编译项目时,遇到如下问题: 原因: 好像是#include "jni_md.h"会将文件包含在与jni.h相同的目录中,但是现在找不到了. 解决办 ...

  6. Android tombstone 分析案例

    Android tombstone 分析案例 tombstone文件内容 1. 体系结构 2. 发生Crash线程 3. 原因 4. 寄存器状态 4.1 处理器工作模式下的寄存器 4.2 未分组寄存器 ...

  7. Unity之手机键盘自定义输入栏位置适配不同手机分辨率适配

    Unity之手机键盘自定义输入栏位置适配&不同手机分辨率适配 效果图 PC端展示 手机端展示(手机是顶部带摄像头的IQOO Neo 5 ) 设计思路 也没啥思路不思路的,就是获取键盘高度,在安 ...

  8. [转]通过创建一个位图的XY Chart来学习Android绘图类Rect,Paint,Bitmap,Canvas(附源码)...

    本文转自:http://www.cnblogs.com/salam/archive/2010/11/10/1873437.html 绘制一个XY集是一种很常见的任务,基于Android平台的绘制很简单 ...

  9. Android Canvas类介绍和Android Draw Rect 坐标图示

    当我们调整好画笔之后,现在需要绘制到画布上,这就得用Canvas类了.在Android中既然把Canvas当做画布,那么就可以在画布上绘制我们想要的任何东西.除了在画布上绘制之外,还需要设置一些关于画 ...

  10. android 自定义paint,Android自定义View中Paint、Rect、Canvas介绍(一)

    自定义View对于新手而言貌似是一个很复杂的东西.格式,各函数的意义.对于大神经常忘记各函数及一些参数的具体写法及意义,刚好在做一个风车效果,把过程及遇到的问题都写下来 1.如何自定义一个View p ...

最新文章

  1. 【C / C++】关于数组默认初值问题
  2. 爬虫进行request请求时User-Agent怎样写
  3. 编译安装_Unbound编译安装
  4. 定义一个圆的类,输入半径,计算周长和面积并输出
  5. 赣州服务器系统,赣州排名P级别服务器厂
  6. Andrew Ng(coursera)单变量线性回归(LINEAR REGRESSION WITH ONE VARIABLE)
  7. 手把手教你VMware14虚拟机安装教程「图文附软件」
  8. 【张量分析】倒三角 微分算子 对 张量场 的作用
  9. atomic 原子操作
  10. 同星T1014在线回放设置
  11. 163,搜狐,新浪哪个邮箱安全?
  12. 常用网络结构:Alex,VGG,Resnet对比
  13. 起点篇:跨入半导体行业,数字IC设计
  14. 英语知识系列:按发音规律重排的英语音标
  15. Datastage性能优化
  16. 计算机实验室教师岗位职责,实验室实验教师岗位职责
  17. Hello CTP(五)——CTP仓位计算
  18. Python 练习实例100例—9
  19. (三)测试工具-3 adb安装卸载apk+清除数据
  20. 文字游戏之恶搞拆字造句

热门文章

  1. Flink sql 写ddl连接kafka
  2. oracle spatial 更新,oracle Spatial(空间数据库)概述
  3. 制作网站及论坛的过程
  4. 软件设计师----计算机网络
  5. 用Python修改Minecraft的mod
  6. 服务器无法定位到现有系统分区,真正解决win7 “安装程序无法定位现有系统分区,也无法创建新的系统分区”的方法...
  7. 写一个京东最顶部的导航条
  8. cl——long.py
  9. poj3294Life Forms
  10. 跨站点设置 Cookie