Most visited

Recently visited

Added in API level 1

Rect

public final class Rect
extends Object implements Parcelable

java.lang.Object
   ↳ android.graphics.Rect


Rect包含四个矩形的整数坐标。 该矩形由其4条边的坐标(左,上,右下)表示。 这些字段可以直接访问。 使用width()和height()来检索矩形的宽度和高度。 注意:大多数方法不检查是否正确排列坐标(即,<=右和<=下)。

请注意,右侧和底部坐标是独占的。 这意味着未被转换到Canvas的Rect将绘制到由其左侧和顶部坐标描述的列和行中,而不是其底部和右侧描绘的列和行。

Summary

Inherited constants

From interface android.os.Parcelable

Fields

public static final Creator<Rect> CREATOR

public int bottom

public int left

public int right

public int top

Public constructors

Rect()

创建一个新的空Rect。

Rect(int left, int top, int right, int bottom)

用指定的坐标创建一个新的矩形。

Rect(Rect r)

创建一个新的矩形,使用指定矩形中的值进行初始化(未作任何修改)。

Public methods

final int centerX()
final int centerY()
boolean contains(int left, int top, int right, int bottom)

如果矩形的4个指定边在此矩形内或等于此矩形,则返回true。

boolean contains(Rect r)

如果指定的矩形r在此矩形内或等于此矩形,则返回true。

boolean contains(int x, int y)

如果(x,y)在矩形内,则返回true。

int describeContents()

Parcelable接口方法

boolean equals(Object o)

指示其他某个对象是否“等于”这一个。

final float exactCenterX()
final float exactCenterY()
String flattenToString()

以明确定义的格式返回矩形的字符串表示形式。

int hashCode()

返回对象的哈希码值。

final int height()
void inset(int dx, int dy)

(dx,dy)插入矩形。

boolean intersect(int left, int top, int right, int bottom)

如果由left,top,right,bottom指定的矩形与此矩形相交,则返回true并将此矩形设置为该交点,否则返回false并且不更改此矩形。

boolean intersect(Rect r)

如果指定的矩形与此矩形相交,则返回true并将此矩形设置为该交点,否则返回false并不更改此矩形。

boolean intersects(int left, int top, int right, int bottom)

如果此矩形与指定的矩形相交,则返回true。

static boolean intersects(Rect a, Rect b)

如果两个指定的矩形相交,则返回true。

final boolean isEmpty()

如果矩形是空的,则返回true(left> = right或top> = bottom)

void offset(int dx, int dy)

通过将dx添加到其左侧和右侧坐标并将dy添加到其顶部和底部坐标来抵消该矩形。

void offsetTo(int newLeft, int newTop)

将矩形偏移到特定的(左侧,顶部)位置,并保持其宽度和高度相同。

void readFromParcel(Parcel in)

根据存储在指定宗地中的数据设置矩形的坐标。

void set(int left, int top, int right, int bottom)

将矩形的坐标设置为指定的值。

void set(Rect src)

将来自src的坐标复制到此矩形中。

void setEmpty()

将矩形设置为(0,0,0,0)

boolean setIntersect(Rect a, Rect b)

如果矩形a和b相交,则返回true并将此矩形设置为该交点,否则返回false并且不更改此矩形。

void sort()

如果发生翻转,则交换顶部/底部或左/右(即

String toShortString()

以紧凑形式返回矩形的字符串表示形式。

String toString()

返回对象的字符串表示形式。

static Rect unflattenFromString(String str)

flattenToString()返回的形式的字符串中返回Rect;如果字符串不是该形式,则返回null。

void union(int left, int top, int right, int bottom)

更新此Rect以包围自身和指定的矩形。

void union(Rect r)

更新此Rect以包围自身和指定的矩形。

void union(int x, int y)

更新此Rect以封闭自身和[x,y]坐标。

final int width()
void writeToParcel(Parcel out, int flags)

将此矩形写入指定的宗地。

Inherited methods

From class java.lang.Object
From interface android.os.Parcelable

Fields

CREATOR

Added in API level 1
Creator<Rect> CREATOR

bottom

Added in API level 1
int bottom

left

Added in API level 1
int left

right

Added in API level 1
int right

top

Added in API level 1
int top

Public constructors

Rect

Added in API level 1
Rect ()

创建一个新的空Rect。 所有的坐标都被初始化为0。

Rect

Added in API level 1
Rect (int left, 
                int top, 
                int right, 
                int bottom)

用指定的坐标创建一个新的矩形。 注意:不执行范围检查,所以调用者必须确保左<=右和顶部<=底部。

Parameters
left int: The X coordinate of the left side of the rectangle
top int: The Y coordinate of the top of the rectangle
right int: The X coordinate of the right side of the rectangle
bottom int: The Y coordinate of the bottom of the rectangle

Rect

Added in API level 1
Rect (Rect r)

创建一个新的矩形,使用指定矩形中的值进行初始化(未作任何修改)。

Parameters
r Rect: The rectangle whose coordinates are copied into the new rectangle.

Public methods

centerX

Added in API level 1
int centerX ()

Returns
int 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.

centerY

Added in API level 1
int centerY ()

Returns
int 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.

contains

Added in API level 1
boolean contains (int left, 
                int top, 
                int right, 
                int bottom)

如果矩形的4个指定边在此矩形内或等于此矩形,则返回true。 即这个矩形是指定矩形的超集。 一个空矩形永远不会包含另一个矩形。

Parameters
left int: The left side of the rectangle being tested for containment
top int: The top of the rectangle being tested for containment
right int: The right side of the rectangle being tested for containment
bottom int: The bottom of the rectangle being tested for containment
Returns
boolean true iff the the 4 specified sides of a rectangle are inside or equal to this rectangle

contains

Added in API level 1
boolean contains (Rect r)

如果指定的矩形r在此矩形内或等于此矩形,则返回true。 一个空矩形永远不会包含另一个矩形。

Parameters
r Rect: The rectangle being tested for containment.
Returns
boolean true iff the specified rectangle r is inside or equal to this rectangle

contains

Added in API level 1
boolean contains (int x, 
                int y)

如果(x,y)在矩形内,则返回true。 左边和顶部被认为是在里面,而右边和底部则不在。 这意味着对于ax,将包含y:left <= x <right并且top <= y <bottom。 一个空矩形永远不会包含任何点。

Parameters
x int: The X coordinate of the point being tested for containment
y int: The Y coordinate of the point being tested for containment
Returns
boolean true iff (x,y) are contained by the rectangle, where containment means left <= x < right and top <= y < bottom

describeContents

Added in API level 1
int describeContents ()

Parcelable接口方法

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.

equals

Added in API level 1
boolean equals (Object o)

指示其他某个对象是否“等于”这一个。

equals方法在非空对象引用上实现等价关系:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

equals方法Object实现了对象上最可能的等价关系; 也就是说,对于任何非空参考值xy ,当且仅当xy引用同一对象( x == y的值为true )时,此方法返回true

请注意,无论何时重写此方法,通常都必须覆盖 hashCode方法,以便维护 hashCode方法的一般合同,该方法声明等同对象必须具有相同的哈希代码。

Parameters
o Object: the reference object with which to compare.
Returns
boolean true if this object is the same as the obj argument; false otherwise.

exactCenterX

Added in API level 1
float exactCenterX ()

Returns
float the exact horizontal center of the rectangle as a float.

exactCenterY

Added in API level 1
float exactCenterY ()

Returns
float the exact vertical center of the rectangle as a float.

flattenToString

Added in API level 7
String flattenToString ()

以明确定义的格式返回矩形的字符串表示形式。

您可以稍后通过 unflattenFromString(String)从此字符串中恢复Rect。

Returns
String Returns a new String of the form "left top right bottom"

hashCode

Added in API level 1
int hashCode ()

返回对象的哈希码值。 为了散列表的好处而支持此方法,例如由HashMap提供的HashMap

hashCode的总合同是:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

尽可能合理实用,由类Object定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术。)

Returns
int a hash code value for this object.

height

Added in API level 1
int height ()

Returns
int the rectangle's height. This does not check for a valid rectangle (i.e. top <= bottom) so the result may be negative.

inset

Added in API level 1
void inset (int dx, 
                int dy)

(dx,dy)插入矩形。 如果dx为正值,则边向内移动,使矩形变窄。 如果dx为负值,则边向外移动,使矩形变宽。 dy和顶部和底部也是如此。

Parameters
dx int: The amount to add(subtract) from the rectangle's left(right)
dy int: The amount to add(subtract) from the rectangle's top(bottom)

intersect

Added in API level 1
boolean intersect (int left, 
                int top, 
                int right, 
                int bottom)

如果由left,top,right,bottom指定的矩形与此矩形相交,则返回true并将此矩形设置为该交点,否则返回false并且不更改此矩形。 不执行检查以查看任一矩形是否为空。 注意:要仅测试相交,请使用intersects(Rect, Rect)

Parameters
left int: The left side of the rectangle being intersected with this rectangle
top int: The top of the rectangle being intersected with this rectangle
right int: The right side of the rectangle being intersected with this rectangle.
bottom int: The bottom of the rectangle being intersected with this rectangle.
Returns
boolean 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.

intersect

Added in API level 1
boolean intersect (Rect r)

如果指定的矩形与此矩形相交,则返回true并将此矩形设置为该交点,否则返回false并不更改此矩形。 不执行检查以查看任一矩形是否为空。 为了测试交集,使用intersects()

Parameters
r Rect: The rectangle being intersected with this rectangle.
Returns
boolean 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.

intersects

Added in API level 1
boolean intersects (int left, 
                int top, 
                int right, 
                int bottom)

如果此矩形与指定的矩形相交,则返回true。 这个矩形在任何情况下都不会被修改。 不执行检查以查看任一矩形是否为空。 要记录交集,请使用intersect()或setIntersect()。

Parameters
left int: The left side of the rectangle being tested for intersection
top int: The top of the rectangle being tested for intersection
right int: The right side of the rectangle being tested for intersection
bottom int: The bottom of the rectangle being tested for intersection
Returns
boolean true iff the specified rectangle intersects this rectangle. In no event is this rectangle modified.

intersects

Added in API level 1
boolean intersects (Rect a, 
                Rect b)

如果两个指定的矩形相交,则返回true。 在任何情况下都不修改任何一个矩形。 要记录交叉点,请使用intersect(Rect)setIntersect(Rect, Rect)

Parameters
a Rect: The first rectangle being tested for intersection
b Rect: The second rectangle being tested for intersection
Returns
boolean true iff the two specified rectangles intersect. In no event are either of the rectangles modified.

isEmpty

Added in API level 1
boolean isEmpty ()

如果矩形是空的,则返回true(left> = right或top> = bottom)

Returns
boolean

offset

Added in API level 1
void offset (int dx, 
                int dy)

通过将dx添加到其左侧和右侧坐标并将dy添加到其顶部和底部坐标来抵消该矩形。

Parameters
dx int: The amount to add to the rectangle's left and right coordinates
dy int: The amount to add to the rectangle's top and bottom coordinates

offsetTo

Added in API level 1
void offsetTo (int newLeft, 
                int newTop)

将矩形偏移到特定的(左侧,顶部)位置,并保持其宽度和高度相同。

Parameters
newLeft int: The new "left" coordinate for the rectangle
newTop int: The new "top" coordinate for the rectangle

readFromParcel

Added in API level 1
void readFromParcel (Parcel in)

根据存储在指定宗地中的数据设置矩形的坐标。 要将矩形写入宗地,请调用writeToParcel()。

Parameters
in Parcel: The parcel to read the rectangle's coordinates from

set

Added in API level 1
void set (int left, 
                int top, 
                int right, 
                int bottom)

将矩形的坐标设置为指定的值。 注意:不执行范围检查,因此由调用者确保左<=右和顶部<=底部。

Parameters
left int: The X coordinate of the left side of the rectangle
top int: The Y coordinate of the top of the rectangle
right int: The X coordinate of the right side of the rectangle
bottom int: The Y coordinate of the bottom of the rectangle

set

Added in API level 1
void set (Rect src)

将来自src的坐标复制到此矩形中。

Parameters
src Rect: The rectangle whose coordinates are copied into this rectangle.

setEmpty

Added in API level 1
void setEmpty ()

将矩形设置为(0,0,0,0)

setIntersect

Added in API level 1
boolean setIntersect (Rect a, 
                Rect b)

如果矩形a和b相交,则返回true并将此矩形设置为该交点,否则返回false并且不更改此矩形。 不执行检查以查看任一矩形是否为空。 为了测试交集,使用intersects()

Parameters
a Rect: The first rectangle being intersected with
b Rect: The second rectangle being intersected with
Returns
boolean 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.

sort

Added in API level 1
void sort ()

如果翻转(即,左>右和/或顶部>底部),交换顶部/底部或左/右。 如果边是分开计算的,并且可能相互交叉,则可以调用它。 如果边缘已经是正确的(即左边<=右边和顶部<=底部),则不做任何事情。

toShortString

Added in API level 3
String toShortString ()

以紧凑形式返回矩形的字符串表示形式。

Returns
String

toString

Added in API level 1
String toString ()

返回对象的字符串表示形式。 一般来说, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

ObjecttoString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

unflattenFromString

Added in API level 7
Rect unflattenFromString (String str)

flattenToString()返回的形式的字符串中返回Rect;如果字符串不是该形式,则返回null。

Parameters
str String
Returns
Rect

union

Added in API level 1
void union (int left, 
                int top, 
                int right, 
                int bottom)

更新此Rect以包围自身和指定的矩形。 如果指定的矩形为空,则不做任何操作。 如果该矩形为空,则将其设置为指定的矩形。

Parameters
left int: The left edge being unioned with this rectangle
top int: The top edge being unioned with this rectangle
right int: The right edge being unioned with this rectangle
bottom int: The bottom edge being unioned with this rectangle

union

Added in API level 1
void union (Rect r)

更新此Rect以包围自身和指定的矩形。 如果指定的矩形为空,则不做任何操作。 如果该矩形为空,则将其设置为指定的矩形。

Parameters
r Rect: The rectangle being unioned with this rectangle

union

Added in API level 1
void union (int x, 
                int y)

更新此Rect以封闭自身和[x,y]坐标。 没有检查,看看这个矩形是非空的。

Parameters
x int: The x coordinate of the point to add to the rectangle
y int: The y coordinate of the point to add to the rectangle

width

Added in API level 1
int width ()

Returns
int the rectangle's width. This does not check for a valid rectangle (i.e. left <= right) so the result may be negative.

writeToParcel

Added in API level 1
void writeToParcel (Parcel out, 
                int flags)

将此矩形写入指定的宗地。 要从包中恢复矩形,请使用readFromParcel()

Parameters
out Parcel: The parcel to write the rectangle's coordinates into
flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

Hooray!