{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

Offset

value public final class Offset


An immutable 2D floating-point offset.

Generally speaking, Offsets can be interpreted in two ways:

  1. As representing a point in Cartesian space a specified distance from a separately-maintained origin. For example, the top-left position of children in the RenderBox protocol is typically represented as an Offset from the top left of the parent box.

  2. As a vector that can be applied to coordinates. For example, when painting a RenderObject, the parent is passed an Offset from the screen's origin which it can add to the offsets of its children to find the Offset from the screen's origin to each of the children.

Because a particular Offset can be interpreted as one sense at one time then as the other sense at a later time, the same class is used for both senses.

See also:

Creates an offset. The first argument sets x, the horizontal component, and the second sets y, the vertical component.

Summary

Nested types

public static class Offset.Companion

Public methods

final float
final float
final @NonNull Offset
copy(float x, float y)

Returns a copy of this Offset instance optionally overriding the x or y parameter

final @NonNull Offset
div(float operand)

Division operator.

final float

The magnitude of the offset.

final float

The square of the magnitude of the offset.

final float
final float
final boolean
final @NonNull Offset

Binary subtraction operator.

final @NonNull Offset

Binary addition operator.

final @NonNull Offset
rem(float operand)

Modulo (remainder) operator.

final @NonNull Offset
times(float operand)

Multiplication operator.

@NonNull String
final @NonNull Offset

Unary negation operator.

Extension functions

final @NonNull Offset
final @NonNull Offset
final @NonNull IntOffset

Round a Offset down to the nearest Int coordinates.

final boolean

True if both x and y values of the Offset are finite

final boolean

false when this is Offset.Unspecified.

final boolean

true when this is Offset.Unspecified.

final @NonNull Offset
OffsetKt.takeOrElse(
    @NonNull Offset receiver,
    @NonNull Function0<@NonNull Offset> block
)

If this Offset isSpecified then this is returned, otherwise block is executed and its result is returned.

Public methods

component1

public final float component1()

component2

public final float component2()

copy

public final @NonNull Offset copy(float x, float y)

Returns a copy of this Offset instance optionally overriding the x or y parameter

div

public final @NonNull Offset div(float operand)

Division operator.

Returns an offset whose coordinates are the coordinates of the left-hand-side operand (an Offset) divided by the scalar right-hand-side operand (a Float).

getDistance

public final float getDistance()

The magnitude of the offset.

If you need this value to compare it to another Offset's distance, consider using getDistanceSquared instead, since it is cheaper to compute.

getDistanceSquared

public final float getDistanceSquared()

The square of the magnitude of the offset.

This is cheaper than computing the getDistance itself.

getX

public final float getX()

getY

public final float getY()

isValid

public final boolean isValid()

minus

public final @NonNull Offset minus(@NonNull Offset other)

Binary subtraction operator.

Returns an offset whose x value is the left-hand-side operand's x minus the right-hand-side operand's x and whose y value is the left-hand-side operand's y minus the right-hand-side operand's y.

plus

public final @NonNull Offset plus(@NonNull Offset other)

Binary addition operator.

Returns an offset whose x value is the sum of the x values of the two operands, and whose y value is the sum of the y values of the two operands.

rem

public final @NonNull Offset rem(float operand)

Modulo (remainder) operator.

Returns an offset whose coordinates are the remainder of dividing the coordinates of the left-hand-side operand (an Offset) by the scalar right-hand-side operand (a Float).

times

public final @NonNull Offset times(float operand)

Multiplication operator.

Returns an offset whose coordinates are the coordinates of the left-hand-side operand (an Offset) multiplied by the scalar right-hand-side operand (a Float).

toString

public @NonNull String toString()

unaryMinus

public final @NonNull Offset unaryMinus()

Unary negation operator.

Returns an offset with the coordinates negated.

If the Offset represents an arrow on a plane, this operator returns the same arrow but pointing in the reverse direction.

Extension functions

IntOffsetKt.minus

public final @NonNull Offset IntOffsetKt.minus(@NonNull Offset receiver, @NonNull IntOffset offset)

IntOffsetKt.plus

public final @NonNull Offset IntOffsetKt.plus(@NonNull Offset receiver, @NonNull IntOffset offset)

IntOffsetKt.round

public final @NonNull IntOffset IntOffsetKt.round(@NonNull Offset receiver)

Round a Offset down to the nearest Int coordinates.

OffsetKt.isFinite

public final boolean OffsetKt.isFinite(@NonNull Offset receiver)

True if both x and y values of the Offset are finite

OffsetKt.isSpecified

public final boolean OffsetKt.isSpecified(@NonNull Offset receiver)

false when this is Offset.Unspecified.

OffsetKt.isUnspecified

public final boolean OffsetKt.isUnspecified(@NonNull Offset receiver)

true when this is Offset.Unspecified.

OffsetKt.takeOrElse

public final @NonNull Offset OffsetKt.takeOrElse(
    @NonNull Offset receiver,
    @NonNull Function0<@NonNull Offset> block
)

If this Offset isSpecified then this is returned, otherwise block is executed and its result is returned.