Android 12 Developer Preview is here! Try it out, and give us your feedback!

Offset

@Immutable inline class Offset
kotlin.Any
   ↳ androidx.compose.ui.geometry.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:

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

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

Summary

Public methods
operator Float

operator Float

Offset
copy(x: Float = this.x, y: Float = this.y)

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

operator Offset
div(operand: Float)

Division operator.

Float

The magnitude of the offset.

Float

The square of the magnitude of the offset.

Boolean

operator Offset
minus(other: Offset)

Binary subtraction operator.

operator Offset
plus(other: Offset)

Binary addition operator.

operator Offset
rem(operand: Float)

Modulo (remainder) operator.

operator Offset
times(operand: Float)

Multiplication operator.

String

operator Offset

Unary negation operator.

Extension functions
From androidx.compose.ui.unit
operator Offset

operator Offset

IntOffset

Round a Offset down to the nearest Int coordinates.

From androidx.compose.ui.geometry
Offset
Offset.takeOrElse(block: () -> Offset)

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

Properties
Float

Float

Companion properties
Offset

An offset with infinite x and y components.

Offset

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

Offset

An offset with zero magnitude.

Extension properties
From androidx.compose.ui.geometry
Boolean

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

Boolean

false when this is Offset.Unspecified.

Boolean

true when this is Offset.Unspecified.

Public methods

component1

@Stable operator fun component1(): Float

component2

@Stable operator fun component2(): Float

copy

fun copy(
    x: Float = this.x,
    y: Float = this.y
): Offset

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

div

@Stable operator fun div(operand: Float): Offset

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

@Stable fun getDistance(): Float

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

@Stable fun getDistanceSquared(): Float

The square of the magnitude of the offset.

This is cheaper than computing the getDistance itself.

isValid

@Stable fun isValid(): Boolean