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
Offset(packedValue)constructorOffset(x: Float, y: Float)
Ready-made values and functions that return it
DrawScope:centerLayoutCoordinates:screenToLocal(),localToScreen(),windowToLocal(),localToWindow(),localToRoot(),localPositionOf()LookaheadScope:localLookaheadPositionOf()NestedScrollConnection:onPreScroll(),onPostScroll()OverscrollEffect:applyToScroll()Rect:topLeft,topCenter,topRight,centerLeft,center,centerRight,bottomLeft,bottomCenter,bottomRightShadowScope:offsetcenterVisibilityThreshold
Used by
Parameters that take a Offset.
Brush.linearGradient(start, end)Brush.radialGradient(center)Brush.sweepGradient(center)DrawScope.drawArc(topLeft)DrawScope.drawCircle(center)DrawScope.drawImage(topLeft)DrawScope.drawLine(start, end)DrawScope.drawOval(topLeft)DrawScope.drawRect(topLeft)DrawScope.drawRoundRect(topLeft)LayoutCoordinates.localPositionOf(relativeToSource)LayoutCoordinates.localToRoot(relativeToLocal)LayoutCoordinates.localToScreen(relativeToLocal)LayoutCoordinates.localToWindow(relativeToLocal)LayoutCoordinates.screenToLocal(relativeToScreen)LayoutCoordinates.windowToLocal(relativeToWindow)LookaheadScope.localLookaheadPositionOf(relativeToSource)NestedScrollConnection.onPostScroll(consumed, available)NestedScrollConnection.onPreScroll(available)Offset.minus(other)Offset.plus(other)OverscrollEffect.applyToScroll(delta)Path.addPath(offset)Path.translate(offset)Rect(offset, topLeft, bottomRight, center)Rect.contains(offset)Rect.translate(offset)SliderDefaults.drawStopIndicator(offset)TextLayoutResult.getOffsetForPosition(position)
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:
- 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
RenderBoxprotocol is typically represented as anOffsetfrom the top left of the parent box. - As a vector that can be applied to coordinates. For example, when painting a widget, the
parent is passed an
Offsetfrom the screen's origin which it can add to the offsets of its children to find theOffsetfrom 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.