模組

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

筆觸

笔画模块是 Ink API 的基础。此模块中的关键数据类型包括:

  • StrokeInputBatch:表示一系列指针输入,包括其位置、时间戳,以及可选的压力、倾斜度和方向。
  • InProgressStroke:表示正在绘制的笔画。InProgressStroke 用于以低延迟渲染部分笔画,并在输入完成后构建最终的 Stroke,之后该对象可以重复使用。 InProgressStroke 由 InProgressStrokes 可组合项使用。
  • Stroke:已完成的笔画的不可变表示形式,具有固定的几何形状。每个 Stroke 都有一个 ImmutableStrokeInputBatch(输入点)、一个 Brush(样式)和一个 PartitionedMesh(几何形状)。您可以在应用中存储、处理和渲染笔画。

幾何學

几何图形模块支持对基本形状(使用 Box 和 Vec 等专用类)以及任意形状(使用 PartitionedMesh)进行几何运算,包括相交检测和转换。 PartitionedMesh 还可以保存其他数据以支持渲染。

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。

该模块使用 Protocol Buffer 和优化的增量压缩技术,与简单的方法相比,可显著节省存储空间。

存储模块简化了笔画的保存、加载和分享。