Рисуйте прямые линии на холстеAndroid

Форум для тех, кто программирует под Android
Anonymous
Рисуйте прямые линии на холсте

Сообщение Anonymous »

Я пытаюсь рисовать прямые линии с помощью холста и методаDetectDragGestures, но пока добился лишь хаотического поведения без каких-либо фиксаций. Я ищу решение, которое позволит мне всегда иметь прямые линии.

Код: Выделить всё

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun CanvasAction() {
var centerOffset by remember {
mutableStateOf(Offset.Zero)
}

var points by remember {
mutableStateOf(listOf())
}

Canvas(
modifier = Modifier
.fillMaxSize()
.pointerInput(key1 = Unit) {
detectDragGestures(
onDragStart = { offset ->
points = points + Point(offset = offset, isStartedPosition = true)
},
onDrag = { change, offset ->
change.historical.forEach {
centerOffset = it.position
}

points = points + change.historical.map {
Point(offset = it.position, isStartedPosition = false)
}
},
)
},
) {
drawCircle(
color = Color.Blue,
radius = 7.dp.toPx(),
center = centerOffset,
)

val path = Path()

points.forEach { point ->
if (point.isStartedPosition) {
path.moveTo(point.offset.x, point.offset.y)
} else {
path.lineTo(point.offset.x, point.offset.y)
}
}

drawPath(
path = path,
color = Color.Blue,
style = Stroke(width = 2.dp.toPx()),
)
}
}
Изображение



Подробнее здесь: https://stackoverflow.com/questions/783 ... -on-canvas

Вернуться в «Android»