幾何圖形 API
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
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
這都要歸功於 Compose
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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."]]