androidx.compose.ui.geometry

Offset

Value class · from org.jetbrains.compose.ui:ui-geometry:1.12.2

No hand-written docs yet.

An immutable 2D floating-point offset.

value class Offset(
    packedValue: Long,
)

How to get one

Create it

Ready-made values and functions that return it

Used by

Parameters that take a Offset.

Properties

x

val x: Float

y

val y: Float

Zero

val Zero: Offset

An offset with zero magnitude.

Infinite

val Infinite: Offset

An offset with infinite x and y components.

Unspecified

val Unspecified: Offset

Represents an unspecified Offset value, usually a replacement for null when a primitive value is desired.

packedValue

val packedValue: Long

Functions

component1

fun component1(): Float

component2

fun component2(): Float

copy

fun copy(
    x: Float = unpackFloat1(packedValue),
    y: Float = unpackFloat2(packedValue),
): Offset

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

isValid

fun isValid(): Boolean

Returns: - False if x or y is a NaN - True if x or y is infinite - True otherwise

getDistance

fun getDistance(): Float

The magnitude of the offset.

getDistanceSquared

fun getDistanceSquared(): Float

The square of the magnitude of the offset.

unaryMinus

fun unaryMinus(): Offset

Unary negation operator.

minus

fun minus(other: Offset): Offset

Binary subtraction operator.

plus

fun plus(other: Offset): Offset

Binary addition operator.

times

fun times(operand: Float): Offset

Multiplication operator.

div

fun div(operand: Float): Offset

Division operator.

rem

fun rem(operand: Float): Offset

Modulo (remainder) operator.

Library docs

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 widget, 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:

  • Size, which represents a vector describing the size of a rectangle.

To create an Offset, call the top-level function that accepts an x/y pair of coordinates:

val offset = Offset(x, y)

The primary constructor of Offset is intended to be used with the packedValue property to allow storing offsets in arrays or collections of primitives without boxing.