Skip to content

Most visited

Recently visited

navigation
Added in API level 1

RectF

public class RectF
extends Object implements Parcelable

java.lang.Object
   ↳ android.graphics.RectF


RectF holds four float coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom). These fields can be accessed directly. Use width() and height() to retrieve the rectangle's width and height. Note: most methods do not check to see that the coordinates are sorted correctly (i.e. left <= right and top <= bottom).

Summary

Inherited constants

From interface android.os.Parcelable

Fields

public static final Creator<RectF> CREATOR

public float bottom

public float left

public float right

public float top

Public constructors

RectF()

Create a new empty RectF.

RectF(float left, float top, float right, float bottom)

Create a new rectangle with the specified coordinates.

RectF(RectF r)

Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).

RectF(Rect r)

Public methods

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

Returns true iff the 4 specified sides of a rectangle are inside or equal to this rectangle.

boolean contains(float x, float y)

Returns true if (x,y) is inside the rectangle.

boolean contains(RectF r)

Returns true iff the specified rectangle r is inside or equal to this rectangle.

int describeContents()

Parcelable interface methods

boolean equals(Object o)

Compares this instance with the specified object and indicates if they are equal.

int hashCode()

Returns an integer hash code for this object.

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

Inset the rectangle by (dx,dy).

boolean intersect(float left, float top, float right, float 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(RectF 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.

static boolean intersects(RectF a, RectF b)

Returns true iff the two specified rectangles intersect.

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

Returns true if this rectangle intersects the specified rectangle.

final boolean isEmpty()

Returns true if the rectangle is empty (left >= right or top >= bottom)

void offset(float dx, float dy)

Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.

void offsetTo(float newLeft, float newTop)

Offset the rectangle to a specific (left, top) position, keeping its width and height the same.

void readFromParcel(Parcel in)

Set the rectangle's coordinates from the data stored in the specified parcel.

void round(Rect dst)

Set the dst integer Rect by rounding this rectangle's coordinates to their nearest integer values.

void roundOut(Rect dst)

Set the dst integer Rect by rounding "out" this rectangle, choosing the floor of top and left, and the ceiling of right and bottom.

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

Set the rectangle's coordinates to the specified values.

void set(RectF src)

Copy the coordinates from src into this rectangle.

void set(Rect src)

Copy the coordinates from src into this rectangle.

void setEmpty()

Set the rectangle to (0,0,0,0)

boolean setIntersect(RectF a, RectF 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.

String toShortString()

Return a string representation of the rectangle in a compact form.

String toString()

Returns a string containing a concise, human-readable description of this object.

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

Update this Rect to enclose itself and the specified rectangle.

void union(RectF r)

Update this Rect to enclose itself and the specified rectangle.

void union(float x, float y)

Update this Rect to enclose itself and the [x,y] coordinate.

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

Write this rectangle to the specified parcel.

Inherited methods

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

Fields

CREATOR

Added in API level 3
Creator<RectF> CREATOR

bottom

Added in API level 1
float bottom

left

Added in API level 1
float left

right

Added in API level 1
float right

top

Added in API level 1
float top

Public constructors

RectF

Added in API level 1
RectF ()

Create a new empty RectF. All coordinates are initialized to 0.

RectF

Added in API level 1
RectF (float left, 
                float top, 
                float right, 
                float bottom)

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.

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

RectF

Added in API level 1
RectF (RectF r)

Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).

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

RectF

Added in API level 1
RectF (Rect r)

Parameters
r Rect

Public methods

centerX

Added in API level 1
float centerX ()

Returns
float the horizontal center of the rectangle. This does not check for a valid rectangle (i.e. left <= right)

centerY

Added in API level 1
float centerY ()

Returns
float the vertical center of the rectangle. This does not check for a valid rectangle (i.e. top <= bottom)

contains

Added in API level 1
boolean contains (float left, 
                float top, 
                float right, 
                float 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.

Parameters
left float: The left side of the rectangle being tested for containment
top float: The top of the rectangle being tested for containment
right float: The right side of the rectangle being tested for containment
bottom float: 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 (float x, 
                float y)

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.

Parameters
x float: The X coordinate of the point being tested for containment
y float: 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

contains

Added in API level 1
boolean contains (RectF r)

Returns true iff the specified rectangle r is inside or equal to this rectangle. An empty rectangle never contains another rectangle.

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

describeContents

Added in API level 3
int describeContents ()

Parcelable interface methods

Returns
int a bitmask indicating the set of special object types marshalled by the Parcelable.

equals

Added in API level 1
boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

Parameters
o Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

height

Added in API level 1
float height ()

Returns
float 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 (float dx, 
                float dy)

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.

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

intersect

Added in API level 1
boolean intersect (float left, 
                float top, 
                float right, 
                float 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 intersects()

Parameters
left float: The left side of the rectangle being intersected with this rectangle
top float: The top of the rectangle being intersected with this rectangle
right float: The right side of the rectangle being intersected with this rectangle.
bottom float: 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 (RectF 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. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()

Parameters
r RectF: 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 (RectF a, 
                RectF b)

Returns true iff the two specified rectangles intersect. In no event are either of the rectangles modified. To record the intersection, use intersect() or setIntersect().

Parameters
a RectF: The first rectangle being tested for intersection
b RectF: 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.

intersects

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

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().

Parameters
left float: The left side of the rectangle being tested for intersection
top float: The top of the rectangle being tested for intersection
right float: The right side of the rectangle being tested for intersection
bottom float: 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.

isEmpty

Added in API level 1
boolean isEmpty ()

Returns true if the rectangle is empty (left >= right or top >= bottom)

Returns
boolean

offset

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

Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.

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

offsetTo

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

Offset the rectangle to a specific (left, top) position, keeping its width and height the same.

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

readFromParcel

Added in API level 3
void readFromParcel (Parcel in)

Set the rectangle's coordinates from the data stored in the specified parcel. To write a rectangle to a parcel, call writeToParcel().

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

round

Added in API level 1
void round (Rect dst)

Set the dst integer Rect by rounding this rectangle's coordinates to their nearest integer values.

Parameters
dst Rect

roundOut

Added in API level 1
void roundOut (Rect dst)

Set the dst integer Rect by rounding "out" this rectangle, choosing the floor of top and left, and the ceiling of right and bottom.

Parameters
dst Rect

set

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

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.

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

set

Added in API level 1
void set (RectF src)

Copy the coordinates from src into this rectangle.

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

set

Added in API level 1
void set (Rect src)

Copy the coordinates from src into this rectangle.

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

setEmpty

Added in API level 1
void setEmpty ()

Set the rectangle to (0,0,0,0)

setIntersect

Added in API level 1
boolean setIntersect (RectF a, 
                RectF 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. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()

Parameters
a RectF: The first rectangle being intersected with
b RectF: 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 ()

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.

toShortString

Added in API level 14
String toShortString ()

Return a string representation of the rectangle in a compact form.

Returns
String

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

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

See Writing a useful toString method if you intend implementing your own toString method.

Returns
String a printable representation of this object.

union

Added in API level 1
void union (float left, 
                float top, 
                float right, 
                float 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.

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

union

Added in API level 1
void union (RectF r)

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.

Parameters
r RectF: The rectangle being unioned with this rectangle

union

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

Update this Rect to enclose itself and the [x,y] coordinate. There is no check to see that this rectangle is non-empty.

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

width

Added in API level 1
float width ()

Returns
float 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 3
void writeToParcel (Parcel out, 
                int flags)

Write this rectangle to the specified parcel. To restore a rectangle from a parcel, use 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.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.