Interfejsy API geometrii
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
借助 Geometry API,您可以创建橡皮擦和选择机制等交互式工具。
为了说明 Geometry API 的实际应用,请探索以下内容:
橡皮擦实现示例。
全笔画橡皮擦
fun eraseWholeStrokes(
eraserBox: ImmutableBox,
finishedStrokesState: MutableState<Set<Stroke>>,
) {
val threshold = 0.1f
val strokesToErase = finishedStrokesState.value.filter { stroke ->
stroke.shape.computeCoverageIsGreaterThan(
box = eraserBox,
coverageThreshold = threshold,
)
}
if (strokesToErase.isNotEmpty()) {
Snapshot.withMutableSnapshot {
finishedStrokesState.value -= strokesToErase
}
}
}
对于 Compose 实现,请务必触发重组,以便
从而有效消除了笔触例如,您可以使用
rememberCoroutineScope
,并将协程作用域传递给
触摸监听器,让您可以在作用域内修改 finishedStrokesState
功能。
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-27 UTC.
[null,null,["Ostatnia aktualizacja: 2025-07-27 UTC."],[],[],null,["# Geometry APIs\n\nThe Geometry APIs allow you to create interactive tools such as erasers and\nselection mechanisms.\n\nTo illustrate practical application of the Geometry APIs, explore the following\neraser implementation example.\n\n### Whole stroke eraser\n\n fun eraseWholeStrokes(\n eraserBox: ImmutableBox,\n finishedStrokesState: MutableState\u003cSet\u003cStroke\u003e\u003e,\n ) {\n val threshold = 0.1f\n\n val strokesToErase = finishedStrokesState.value.filter { stroke -\u003e\n stroke.shape.computeCoverageIsGreaterThan(\n box = eraserBox,\n coverageThreshold = threshold,\n )\n }\n if (strokesToErase.isNotEmpty()) {\n Snapshot.withMutableSnapshot {\n finishedStrokesState.value -= strokesToErase\n }\n }\n }\n\nFor a Compose implementation, make sure to trigger a recomposition, so the\nstrokes are effectively removed. For example, an approach would be to use\n`rememberCoroutineScope` in your composable and pass the coroutine scope to your\ntouch listener, allowing you to modify `finishedStrokesState` within the scope\nof Compose.\n| **Note:** An eraser that only removes the parts of the strokes it touches can be implemented by seeing if a stroke intersects with individual line segments of a [`StrokeInputBatch`](/reference/kotlin/androidx/ink/strokes/StrokeInputBatch) and creating new [`StrokeInputBatch`](/reference/kotlin/androidx/ink/strokes/StrokeInputBatch) and [`Stroke`](/reference/kotlin/androidx/ink/strokes/Stroke) objects out of the line segments that aren't intersected."]]