public final class Rect extends Object implements Parcelable
Note that the right and bottom coordinates are exclusive. This means a Rect
being drawn untransformed onto a Canvas
will draw
into the column and row described by its left and top coordinates, but not
those of its bottom and right.
Parcelable.ClassLoaderCreator<T>, Parcelable.Creator<T>
Modifier and Type | Field and Description |
---|---|
int |
bottom |
static Parcelable.Creator<Rect> |
CREATOR |
int |
left |
int |
right |
int |
top |
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_ELIDE_DUPLICATES, PARCELABLE_WRITE_RETURN_VALUE
Constructor and Description |
---|
Rect()
Create a new empty Rect.
|
Rect(int left,
int top,
int right,
int bottom)
Create a new rectangle with the specified coordinates.
|
Rect(Rect r)
Create a new rectangle, initialized with the values in the specified
rectangle (which is left unmodified).
|
Modifier and Type | Method and Description |
---|---|
int |
centerX() |
int |
centerY() |
boolean |
contains(int x,
int y)
Returns true if (x,y) is inside the rectangle.
|
boolean |
contains(int left,
int top,
int right,
int 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.
|
boolean |
contains(Rect r)
Returns true iff the specified rectangle r is inside or equal to this
rectangle.
|
int |
describeContents()
Parcelable interface methods
|
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
float |
exactCenterX() |
float |
exactCenterY() |
String |
flattenToString()
Return a string representation of the rectangle in a well-defined format.
|
int |
hashCode()
Returns a hash code value for the object.
|
int |
height() |
void |
inset(int dx,
int dy)
Inset the rectangle by (dx,dy).
|
void |
inset(int left,
int top,
int right,
int bottom)
Insets the rectangle on all sides specified by the insets.
|
void |
inset(Rect insets)
Insets the rectangle on all sides specified by the dimensions of the
insets
rectangle. |
boolean |
intersect(int left,
int top,
int right,
int 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.
|
boolean |
intersect(Rect r)
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.
|
boolean |
intersects(int left,
int top,
int right,
int bottom)
Returns true if this rectangle intersects the specified rectangle.
|
static boolean |
intersects(Rect a,
Rect b)
Returns true iff the two specified rectangles intersect.
|
boolean |
isEmpty()
Returns true if the rectangle is empty (left >= right or top >= bottom)
|
void |
offset(int dx,
int dy)
Offset the rectangle by adding dx to its left and right coordinates, and
adding dy to its top and bottom coordinates.
|
void |
offsetTo(int newLeft,
int newTop)
Offset the rectangle to a specific (left, top) position,
keeping its width and height the same.
|
void |
printShortString(PrintWriter pw)
Print short representation to given writer.
|
void |
readFromParcel(Parcel in)
Set the rectangle's coordinates from the data stored in the specified
parcel.
|
void |
scale(float scale)
Scales up the rect by the given scale.
|
void |
set(int left,
int top,
int right,
int bottom)
Set the rectangle's coordinates to the specified values.
|
void |
set(Rect src)
Copy the coordinates from src into this rectangle.
|
void |
setEmpty()
Set the rectangle to (0,0,0,0)
|
boolean |
setIntersect(Rect a,
Rect b)
If rectangles a and b intersect, return true and set this rectangle to
that intersection, otherwise return false and do not change this
rectangle.
|
void |
sort()
Swap top/bottom or left/right if there are flipped (i.e. left > right
and/or top > bottom).
|
String |
toShortString()
Return a string representation of the rectangle in a compact form.
|
String |
toShortString(StringBuilder sb)
Return a string representation of the rectangle in a compact form.
|
String |
toString()
Returns a string representation of the object.
|
static Rect |
unflattenFromString(String str)
Returns a Rect from a string of the form returned by
flattenToString() ,
or null if the string is not of that form. |
void |
union(int x,
int y)
Update this Rect to enclose itself and the [x,y] coordinate.
|
void |
union(int left,
int top,
int right,
int bottom)
Update this Rect to enclose itself and the specified rectangle.
|
void |
union(Rect r)
Update this Rect to enclose itself and the specified rectangle.
|
int |
width() |
void |
writeToParcel(Parcel out,
int flags)
Write this rectangle to the specified parcel.
|
public int left
public int top
public int right
public int bottom
public static final Parcelable.Creator<Rect> CREATOR
public Rect()
public Rect(int left, int top, int right, int bottom)
left
- The X coordinate of the left side of the rectangletop
- The Y coordinate of the top of the rectangleright
- The X coordinate of the right side of the rectanglebottom
- The Y coordinate of the bottom of the rectanglepublic Rect(Rect r)
r
- The rectangle whose coordinates are copied into the new
rectangle.public boolean equals(Object o)
Object
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
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.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
equals
in class Object
o
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
Object
HashMap
.
The general contract of hashCode
is:
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.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.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.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public String toShortString()
public String toShortString(StringBuilder sb)
public String flattenToString()
You can later recover the Rect from this string through
unflattenFromString(String)
.
public static Rect unflattenFromString(String str)
flattenToString()
,
or null if the string is not of that form.public void printShortString(PrintWriter pw)
public final boolean isEmpty()
public final int width()
public final int height()
public final int centerX()
public final int centerY()
public final float exactCenterX()
public final float exactCenterY()
public void setEmpty()
public void set(int left, int top, int right, int bottom)
left
- The X coordinate of the left side of the rectangletop
- The Y coordinate of the top of the rectangleright
- The X coordinate of the right side of the rectanglebottom
- The Y coordinate of the bottom of the rectanglepublic void set(Rect src)
src
- The rectangle whose coordinates are copied into this
rectangle.public void offset(int dx, int dy)
dx
- The amount to add to the rectangle's left and right coordinatesdy
- The amount to add to the rectangle's top and bottom coordinatespublic void offsetTo(int newLeft, int newTop)
newLeft
- The new "left" coordinate for the rectanglenewTop
- The new "top" coordinate for the rectanglepublic void inset(int dx, int dy)
dx
- The amount to add(subtract) from the rectangle's left(right)dy
- The amount to add(subtract) from the rectangle's top(bottom)public void inset(Rect insets)
insets
rectangle.insets
- The rectangle specifying the insets on all side.public void inset(int left, int top, int right, int bottom)
left
- The amount to add from the rectangle's lefttop
- The amount to add from the rectangle's topright
- The amount to subtract from the rectangle's rightbottom
- The amount to subtract from the rectangle's bottompublic boolean contains(int x, int y)
x
- The X coordinate of the point being tested for containmenty
- The Y coordinate of the point being tested for containmentpublic boolean contains(int left, int top, int right, int bottom)
left
- The left side of the rectangle being tested for containmenttop
- The top of the rectangle being tested for containmentright
- The right side of the rectangle being tested for containmentbottom
- The bottom of the rectangle being tested for containmentpublic boolean contains(Rect r)
r
- The rectangle being tested for containment.public boolean intersect(int left, int top, int right, int bottom)
intersects(Rect, Rect)
.left
- The left side of the rectangle being intersected with this
rectangletop
- The top of the rectangle being intersected with this rectangleright
- The right side of the rectangle being intersected with this
rectangle.bottom
- The bottom of the rectangle being intersected with this
rectangle.public boolean intersect(Rect r)
r
- The rectangle being intersected with this rectangle.public boolean setIntersect(Rect a, Rect b)
a
- The first rectangle being intersected withb
- The second rectangle being intersected withpublic boolean intersects(int left, int top, int right, int bottom)
left
- The left side of the rectangle being tested for intersectiontop
- The top of the rectangle being tested for intersectionright
- The right side of the rectangle being tested for
intersectionbottom
- The bottom of the rectangle being tested for intersectionpublic static boolean intersects(Rect a, Rect b)
intersect(Rect)
or setIntersect(Rect, Rect)
.a
- The first rectangle being tested for intersectionb
- The second rectangle being tested for intersectionpublic void union(int left, int top, int right, int bottom)
left
- The left edge being unioned with this rectangletop
- The top edge being unioned with this rectangleright
- The right edge being unioned with this rectanglebottom
- The bottom edge being unioned with this rectanglepublic void union(Rect r)
r
- The rectangle being unioned with this rectanglepublic void union(int x, int y)
x
- The x coordinate of the point to add to the rectangley
- The y coordinate of the point to add to the rectanglepublic void sort()
public int describeContents()
describeContents
in interface Parcelable
Parcelable.CONTENTS_FILE_DESCRIPTOR
public void writeToParcel(Parcel out, int flags)
writeToParcel
in interface Parcelable
out
- The parcel to write the rectangle's coordinates intoflags
- Additional flags about how the object should be written.
May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE
.public void readFromParcel(Parcel in)
in
- The parcel to read the rectangle's coordinates frompublic void scale(float scale)