模块

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.

几何体

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

笔刷

The brush module defines the style of strokes. It consists of two main parts:

  • Brush: Specifies the style of a stroke including base color, base size, and BrushFamily. BrushFamily is analogous to a font family, it defines a stroke's style. For example, a BrushFamily can represent a specific style of marker or highlighter, allowing strokes with different sizes and colors to share that style.
  • StockBrushes: Provides factory functions for creating ready-to-use BrushFamily instances.

编写

借助 Compose 创作模块,您可以捕获用户触摸输入,并将其实时呈现为屏幕上的低延迟笔画。这是通过 InProgressStrokes 可组合项实现的,该可组合项可处理运动事件并在绘制笔画时显示笔画。

笔画完成后,可组合项会使用 InProgressStrokesFinishedListener 回调通知客户端应用。这允许应用检索完成的笔画以进行渲染或存储。

在 Compose 中,InProgressStrokes 通过 onStrokesFinished 参数接收此回调。将完成的笔画传递给另一个可组合项,以使用渲染模块将其提交到屏幕。

渲染

渲染模块可简化在 Android Canvas 上绘制墨水笔画的过程。 它为 Compose 提供 CanvasStrokeRenderer,为基于视图的布局提供 ViewStrokeRenderer。这些渲染器可优化渲染性能,并有助于呈现高质量的视觉效果,包括抗锯齿。

如需渲染笔画,请调用 create() 方法以获取 CanvasStrokeRenderer 实例,然后调用 draw() 方法,以将已完成 (Stroke) 或正在进行 (InProgressStroke) 的笔画渲染到 Canvas 上。

您可以在绘制笔画时转换画布。例如平移、缩放和旋转。为了正确渲染描边,您还必须将 canvas 转换传递给 CanvasStrokeRenderer.draw

为避免单独跟踪 canvas 转换,请改用 ViewStrokeRenderer

存储

The storage module provides utilities for efficiently serializing and deserializing stroke data, primarily focusing on StrokeInputBatch.

The module uses protocol buffers and optimized delta compression techniques, resulting in significant storage savings compared to naive methods.

The storage module simplifies saving, loading, and sharing strokes.