这是一个我们常用的一个“绘画相关的工具类”,常用于描述长方形/正方形,实现一个 “长方形”的概念
3个构造函数
//第一个构造函数
public Rect() { }
//第二个构造函数
public Rect(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = 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;
}
4个属性
1.public int left;
2.public int top;
3.public int right;
4.public int bottom;
这4个属性描述着这一个“方块”
这四个属性不单单描述了这个 长方形4个点的坐标,间接的描述出这个长方形的尺寸
长 = bottom - top
宽 = right – left
拥有的方法如下:
方法 | 说明 |
---|---|
public final boolean isEmpty () | 如果矩形为空,则返回true(left >= right 或 top >= bottom) |
public void setEmpty () | 将矩形设置为(0,0,0,0)(left, top, right, bottom) |
public final int height () | 返回矩形的高度(这没有检查一个有效的矩形;即top <= bottom),所以结果可能是负的 |
public final int width () | 返回矩形的宽度(这没有检查一个有效的矩形即left<=right),所以结果可能是负的 |
public String flattenToString () | 返回矩形的字符串表示形式(左上右下) |
public final int centerX () | 获取矩阵中心点X轴上的坐标;如果值是小数,则返回小于值的最大整数(返回int类型) |
public final int centerY () | 获取矩阵中心点Y轴上的坐标;如果值是小数,则返回小于值的最大整数(返回int类型) |
public final float exactCenterX () | 该方法和CenterX()类似,获取矩阵中心点X轴上的坐标,只是该方法精确度比它高(返回float类型) |
public final float exactCenterY () | 该方法和CenterY()类似, 获取矩阵中心点Y轴上的坐标,只是该方法精确度比它高(返回float类型) |
public boolean contains (int x, int y) | 如果(x,y)在矩形内,则返回true 注意:左侧和顶部被认为是内部,而右侧和底部则不是; x,y被包含:left<= x <right和top<= y <bottom 空矩形不包含任何点 |
public boolean contains (int left, int top, int right, int bottom) | 如果矩形的4条指定边在该矩形或等于该矩形,则返回true 注意:一个空矩形永远不会包含另一个矩形 |
public boolean contains (Rect r) | 如果指定的矩形r在该矩形内或等于该矩形,则返回true 注意:一个空矩形永远不会包含另一个矩形 |
public int describeContents () | Parcelable接口方法 |
public boolean equals (Object o) | 比较此实例与指定对象,并指示它们是否相等;如果指定的对象等于此对象,则为true;否则返回false |
public void inset (int dx, int dy) | 在矩形中插入(dx,dy);dx/dy是正的,两边向内移动(使矩形变窄)。dx/dy是负的,两边向外移动(使矩形变宽) |
public boolean intersects (int left, int top, int right, int bottom) | 如果此矩形与指定矩形相交,则返回true 注意:这个矩形在任何情况下都不会被修改 不执行检查矩形是否为空的操作 要记录交集,请使用intersect()/setIntersect() |
public static boolean intersects (Rect a, Rect b) | 如果两个指定矩形相交,则返回true 注意:在任何情况下,这两个矩形都不会被修改。 要记录交集,请使用intersectt)/setIntersect() |
public boolean intersect (Rect r) | 如果指定的矩形与此矩形相交,则返回true并将此矩形设置为该交集;否则返回false且不更改此矩形 注意:要测试交集,请使用intersects() |
public boolean intersect (int left, int top, int right, int bottom) | 如果由左,上,右,下,指定的矩形与此矩形相交,返回true并将该矩形设置为该交集;否则返回false且不改变该矩形 注意:要测试交集,请使用intersects() |
boolean setIntersect( Rect a, Rect b) | 如果矩形a和b相交,返回true并将该矩形设置为该相交,否则返回false且不改变该矩形 注意:不执行检查矩形是否为空的操作 要测试交集,请使用intersects() |
public void offset (int dx, int dy) | 通过在X轴和Y轴上设置偏移量去移动矩形 也就是矩形的left和right坐标上加上dx,在top和bottom坐标上加上dy来偏移矩形 |
public void offsetTo (int newLeft, int newTop) | 将矩形偏移到一个指定的位置(左上角),保持它的宽度和高度相同 |
public void readFromParcel (Parcel in) | 根据存储在指定包裹中的数据设置矩形的坐标 |
public void writeToParcel (Parcel out, int flags) | 将此矩形写入指定的包裹 |
public void set (int left, int top, int right, int bottom) | 将矩形的坐标设置为指定值 注意:没有执行范围检查,确保left <= right和top <= bottom。 |
public void set (Rect src) | 从src复制坐标到这个矩形中 |
public void sort () | 如果可以翻转(即左>右和/或顶部>底)交换顶部/底部或左/右 注意: 如果边缘已经正确(即左<=右和顶<=底),则不执行任何操作 |
public String toShortString () | 以紧凑形式返回矩形的字符串表示形式 |
public String toString () | 返回一个字符串,该字符串包含对该对象的简明的人类可读的描述 注意:鼓励子类重写此方法,并提供对象类型和数据的实现 @Override public String toString() { return getClass().getName() + “[” + “primitiveField=” + primitiveField + ", " + “referenceField=” +referenceField + ", " + “arrayField=” + Arrays.toString(arrayField) + “]”; } |
public static Rect unflattenFromString (String str) | 从扁平化字符串()返回的表单字符串中返回一个矩形,如果字符串不是该表单的,则返回null |
public void union (int left, int top, int right, int bottom) | 更新此矩形为指定的矩形 注意: 如果指定的矩形为空,则不执行任何操作; 如果自身矩形为空,则将其设置为指定的矩形 |
void union( Rect r) | 更新此矩形为指定的矩形 注意: 如果指定的矩形为空,则不执行任何操作; 如果自身矩形为空,则将其设置为指定的矩形 |
void union (int x, int y) | 更新这个矩形,以包含它自己和[x,y]坐标 注意: 没有检查该矩形是否为非空 |