Added in API level 1

Rect


class Rect : Parcelable
kotlin.Any
   ↳ android.graphics.Rect

Rect holds four integer 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).

Note that the right and bottom coordinates are exclusive. This means a Rect being drawn untransformed onto a android.graphics.Canvas will draw into the column and row described by its left and top coordinates, but not those of its bottom and right.

Summary

Inherited constants
Public constructors

Create a new empty Rect.

Rect(left: Int, top: Int, right: Int, bottom: Int)

Create a new rectangle with the specified coordinates.

Rect(r: Rect?)

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

Public methods
Int

Int

Boolean
contains(x: Int, y: Int)

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

Boolean
contains(left: Int, top: Int, right: Int, bottom: Int)

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

Boolean

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

Int

Parcelable interface methods

Boolean
equals(other: Any?)

Float

Float

String

Return a string representation of the rectangle in a well-defined format.

Int

Int

Unit
inset(dx: Int, dy: Int)

Inset the rectangle by (dx,dy).

Unit
inset(insets: Insets)

Insets the rectangle on all sides specified by the dimensions of insets.

Unit
inset(left: Int, top: Int, right: Int, bottom: Int)

Insets the rectangle on all sides specified by the insets.

Boolean
intersect(left: Int, top: Int, right: Int, bottom: Int)

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

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(left: Int, top: Int, right: Int, bottom: Int)

Returns true if this rectangle intersects the specified rectangle.

static Boolean
intersects(a: Rect, b: Rect)

Returns true iff the two specified rectangles intersect.

Boolean

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

Unit
offset(dx: Int, dy: Int)

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

Unit
offsetTo(newLeft: Int, newTop: Int)

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

Unit

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

Unit
set(left: Int, top: Int, right: Int, bottom: Int)

Set the rectangle's coordinates to the specified values.

Unit
set(src: Rect)

Copy the coordinates from src into this rectangle.

Unit

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

Boolean

If rectangles a and b intersect, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.

Unit

Swap top/bottom or left/right if there are flipped (i.e. left > right and/or top > bottom).

String

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

String

static Rect?

Returns a Rect from a string of the form returned by flattenToString, or null if the string is not of that form.

Unit
union(left: Int, top: Int, right: Int, bottom: Int)

Update this Rect to enclose itself and the specified rectangle.

Unit
union(r: Rect)

Update this Rect to enclose itself and the specified rectangle.

Unit
union(x: Int, y: Int)

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

Int

Unit
writeToParcel(out: Parcel, flags: Int)

Write this rectangle to the specified parcel.

Properties
static Parcelable.Creator<Rect!>

Int

Int

Int

Int

Public constructors

Rect

Added in API level 1
Rect()

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

Rect

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

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 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(r: Rect?)

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

Parameters
r Rect?: The rectangle whose coordinates are copied into the new rectangle. This value may be null.

Public methods

centerX

Added in API level 1
fun centerX(): Int
Return
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
fun centerY(): Int
Return
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
fun contains(
    x: Int,
    y: Int
): Boolean

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 Int: The X coordinate of the point being tested for containment
y Int: The Y coordinate of the point being tested for containment
Return
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
fun contains(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Boolean

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 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
Return
Boolean true iff the 4 specified sides of a rectangle are inside or equal to this rectangle

contains

Added in API level 1
fun contains(r: Rect): Boolean

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

Parameters
r Rect: The rectangle being tested for containment. This value cannot be null.
Return
Boolean true iff the specified rectangle r is inside or equal to this rectangle

describeContents

Added in API level 1
fun describeContents(): Int

Parcelable interface methods

Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

equals

Added in API level 1
fun equals(other: Any?): Boolean
Parameters
obj the reference object with which to compare.
Return
Boolean true if this object is the same as the obj argument; false otherwise.

exactCenterX

Added in API level 1
fun exactCenterX(): Float
Return
Float the exact horizontal center of the rectangle as a float.

exactCenterY

Added in API level 1
fun exactCenterY(): Float
Return
Float the exact vertical center of the rectangle as a float.

flattenToString

Added in API level 7
fun flattenToString(): String

Return a string representation of the rectangle in a well-defined format.

You can later recover the Rect from this string through unflattenFromString(java.lang.String).

Return
String Returns a new String of the form "left top right bottom" This value cannot be null.

hashCode

Added in API level 1
fun hashCode(): Int
Return
Int a hash code value for this object.

height

Added in API level 1
fun height(): Int
Return
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
fun inset(
    dx: Int,
    dy: Int
): Unit

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

inset

Added in API level 31
fun inset(insets: Insets): Unit

Insets the rectangle on all sides specified by the dimensions of insets.

Parameters
insets Insets: The insets to inset the rect by. This value cannot be null.

inset

Added in API level 31
fun inset(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Unit

Insets the rectangle on all sides specified by the insets.

Parameters
left Int: The amount to add from the rectangle's left
top Int: The amount to add from the rectangle's top
right Int: The amount to subtract from the rectangle's right
bottom Int: The amount to subtract from the rectangle's bottom

intersect

Added in API level 1
fun intersect(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Boolean

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(android.graphics.Rect,android.graphics.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.
Return
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
fun intersect(r: Rect): Boolean

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 Rect: The rectangle being intersected with this rectangle. This value cannot be null.
Return
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
fun intersects(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Boolean

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 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
Return
Boolean true iff the specified rectangle intersects this rectangle. In no event is this rectangle modified.

intersects

Added in API level 1
static fun intersects(
    a: Rect,
    b: Rect
): Boolean

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

Parameters
a Rect: The first rectangle being tested for intersection This value cannot be null.
b Rect: The second rectangle being tested for intersection This value cannot be null.
Return
Boolean true iff the two specified rectangles intersect. In no event are either of the rectangles modified.

isEmpty

Added in API level 1
fun isEmpty(): Boolean

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

offset

Added in API level 1
fun offset(
    dx: Int,
    dy: Int
): Unit

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

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
fun offsetTo(
    newLeft: Int,
    newTop: Int
): Unit

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

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
fun readFromParcel(in: Parcel): Unit

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 This value cannot be null.

set

Added in API level 1
fun set(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Unit

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 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
fun set(src: Rect): Unit

Copy the coordinates from src into this rectangle.

Parameters
src Rect: The rectangle whose coordinates are copied into this rectangle. This value cannot be null.

setEmpty

Added in API level 1
fun setEmpty(): Unit

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

setIntersect

Added in API level 1
fun setIntersect(
    a: Rect,
    b: Rect
): Boolean

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 Rect: The first rectangle being intersected with This value cannot be null.
b Rect: The second rectangle being intersected with This value cannot be null.
Return
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
fun sort(): Unit

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 3
fun toShortString(): String

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

Return
String This value cannot be null.

toString

Added in API level 1
fun toString(): String
Return
String a string representation of the object.

unflattenFromString

Added in API level 7
static fun unflattenFromString(str: String?): Rect?

Returns a Rect from a string of the form returned by flattenToString, or null if the string is not of that form.

Parameters
str String?: This value may be null.

union

Added in API level 1
fun union(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Unit

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 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
fun union(r: Rect): Unit

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 Rect: The rectangle being unioned with this rectangle This value cannot be null.

union

Added in API level 1
fun union(
    x: Int,
    y: Int
): Unit

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 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
fun width(): Int
Return
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
fun writeToParcel(
    out: Parcel,
    flags: Int
): Unit

Write this rectangle to the specified parcel. To restore a rectangle from a parcel, use readFromParcel()

Parameters
dest The Parcel in which the object should be written. This value cannot be null.
flags Int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES
out Parcel: The parcel to write the rectangle's coordinates into

Properties

CREATOR

Added in API level 1
static val CREATOR: Parcelable.Creator<Rect!>

bottom

Added in API level 1
var bottom: Int

left

Added in API level 1
var left: Int
Added in API level 1
var right: Int

top

Added in API level 1
var top: Int