calculateCentroid
Function
Common
fun PointerEvent.calculateCentroid(useCurrent: Boolean = true): Offset
Returns the centroid of all pointers that are down and were previously down. If no pointers are
down, Offset.Unspecified
is returned. If useCurrent
is true
, the centroid of the
PointerInputChange.position
is returned and if false
, the centroid of the
PointerInputChange.previousPosition
is returned. Only pointers that are down in both the
previous and current state are used to calculate the centroid.
Example Usage:
Code Examples
CalculateCentroidSize
@Composable
fun CalculateCentroidSize() {
var centroidSize by remember { mutableStateOf(0f) }
var position by remember { mutableStateOf(Offset.Zero) }
Box(
Modifier.drawBehind {
// Draw a circle where the gesture is
drawCircle(Color.Blue, centroidSize, center = position)
}
.pointerInput(Unit) {
awaitEachGesture {
awaitFirstDown().also { position = it.position }
do {
val event = awaitPointerEvent()
val size = event.calculateCentroidSize()
if (size != 0f) {
centroidSize = event.calculateCentroidSize()
}
val centroid = event.calculateCentroid()
if (centroid != Offset.Unspecified) {
position = centroid
}
} while (event.changes.any { it.pressed })
}
}
.fillMaxSize()
)
}