模組

Ink API 採用模組化設計,因此您只需要使用所需功能。

筆觸

The strokes module serves as the foundation of the Ink API. Key data types within this module are:

  • StrokeInputBatch: Represents a series of pointer inputs, including their position, timestamp, and optionally pressure, tilt, and orientation.
  • InProgressStroke: Represents a stroke that is actively being drawn. InProgressStroke is used to render partial strokes with low latency and to build the final Stroke once input is complete, after which the object can be reused. InProgressStroke is used by the InProgressStrokes composable.
  • Stroke: An immutable representation of a finalized stroke with fixed geometry. Each Stroke has an ImmutableStrokeInputBatch (input points), a Brush (style), and a PartitionedMesh (geometric shape). You can store, manipulate, and render strokes within your application.

幾何學

Geometry 模組支援對基本形狀執行幾何運算 (使用 BoxVec 等專用類別),以及任意形狀 (使用 PartitionedMesh),包括交集偵測和轉換。PartitionedMesh 也可以保留額外資料,以支援算繪作業。

Brush

brush 模組會定義筆觸樣式。當中包含下列兩項主要元素:

  • Brush:指定筆觸的樣式,包括基本顏色、基本大小和 BrushFamilyBrushFamily 類似於字型系列,可定義筆觸的樣式。舉例來說,BrushFamily 可以代表特定樣式的標記或螢光筆,讓不同大小和顏色的筆觸共用該樣式。
  • StockBrushes:提供原廠函式,用於建立可立即使用的 BrushFamily 例項。

編寫

Compose Authoring 模組可擷取使用者觸控輸入內容,並即時在畫面上以低延遲筆觸呈現。這是透過 InProgressStrokes 可組合函式達成,該函式會處理動作事件,並在繪製筆劃時顯示筆劃。

筆劃完成後,可組合函式會使用 InProgressStrokesFinishedListener 回呼通知用戶端應用程式。應用程式可藉此擷取完成的筆劃,以進行算繪或儲存。

在 Compose 中,InProgressStrokes 會在 onStrokesFinished 參數中採用這個回呼。將完成的筆觸傳遞至另一個可組合函式,使用算繪模組將筆觸提交至畫面。

轉譯

使用 Rendering 模組,即可輕鬆在 Android Canvas 上繪製墨水筆觸。當中提供 Compose 的 CanvasStrokeRenderer 和以檢視區塊為基礎的版面配置 ViewStrokeRenderer。這些算繪器可提升算繪效能,並提供高品質的視覺效果,包括反鋸齒。

如要算繪筆劃,請呼叫 create() 方法取得 CanvasStrokeRenderer 例項,然後呼叫 draw() 方法,將完成 (Stroke) 或進行中 (InProgressStroke) 的筆劃算繪到 Canvas 上。

繪製筆觸時,你可以變形畫布。例如平移、縮放及旋轉。如要正確算繪筆觸,您也必須將 canvas 轉換傳遞至 CanvasStrokeRenderer.draw

如要避免個別追蹤 canvas 轉換,請改用 ViewStrokeRenderer

儲存空間

儲存模組提供實用工具,可有效率地序列化及還原序列化筆劃資料,主要著重於 StrokeInputBatch

這個模組使用通訊協定緩衝區和最佳化的差異壓縮技術,與原始方法相比,可大幅節省儲存空間。

儲存空間模組可簡化筆劃的儲存、載入及共用作業。