Ink API 採用模組化設計,因此您只需要使用所需功能。
筆觸
筆劃模組是 Ink API 的基礎,這個模組中的主要資料類型包括:
StrokeInputBatch:代表一系列指標輸入內容,包括位置、時間戳記,以及選用的壓力、傾斜度和方向。InProgressStroke:代表正在繪製的筆劃。InProgressStroke用於以低延遲時間算繪部分筆劃,並在輸入完成後建構最終的Stroke,之後即可重複使用該物件。InProgressStroke由InProgressStrokes可組合函式使用。Stroke:最終筆劃的不可變更表示法,具有固定幾何圖形。每個Stroke都有ImmutableStrokeInputBatch(輸入點)、Brush(樣式) 和PartitionedMesh(幾何形狀)。您可以在應用程式中儲存、操控及算繪筆劃。
幾何學
The Geometry module supports geometric operations on primitive shapes (using dedicated classes like Box and Vec), as well as arbitrary shapes (using PartitionedMesh), including intersection detection and transformation. PartitionedMesh can also hold additional data to support rendering.
Brush
brush 模組會定義筆觸樣式。當中包含下列兩項主要元素:
Brush:指定筆觸的樣式,包括基本顏色、基本大小和BrushFamily。BrushFamily類似於字型系列,可定義筆觸的樣式。舉例來說,BrushFamily可以代表特定樣式的標記或螢光筆,讓不同大小和顏色的筆觸共用該樣式。StockBrushes:提供原廠函式,用於建立可立即使用的BrushFamily例項。
編寫
The Compose Authoring module lets you capture user touch input and render it
as low-latency strokes on the screen in real time. This is achieved through
the InProgressStrokes composable, which processes motion events and displays
the strokes as they are drawn.
Once a stroke is completed, the composable notifies the client application using
an InProgressStrokesFinishedListener callback. This allows the application
to retrieve the finished strokes for rendering or storage.
In Compose, InProgressStrokes takes this callback in the onStrokesFinished
parameter. Pass the finished strokes to another composable to commit them to the
screen using the rendering module.
轉譯
使用 Rendering 模組,即可輕鬆在 Android Canvas 上繪製墨水筆觸。當中提供 Compose 的 CanvasStrokeRenderer 和以檢視區塊為基礎的版面配置 ViewStrokeRenderer。這些算繪器可提升算繪效能,並提供高品質的視覺效果,包括反鋸齒。
如要算繪筆劃,請呼叫 create() 方法取得 CanvasStrokeRenderer 例項,然後呼叫 draw() 方法,將完成 (Stroke) 或進行中 (InProgressStroke) 的筆劃算繪到 Canvas 上。
繪製筆觸時,你可以變形畫布。例如平移、縮放及旋轉。如要正確算繪筆觸,您也必須將 canvas 轉換傳遞至 CanvasStrokeRenderer.draw。
如要避免個別追蹤 canvas 轉換,請改用 ViewStrokeRenderer。
儲存空間
儲存模組提供實用工具,可有效率地序列化及還原序列化筆劃資料,主要著重於 StrokeInputBatch。
這個模組使用通訊協定緩衝區和最佳化的差異壓縮技術,與原始方法相比,可大幅節省儲存空間。
儲存空間模組可簡化筆劃的儲存、載入及共用作業。