Modules

Ink API is modularized, so you can use only what you need.

Strokes

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

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:指定笔画的样式,包括基准颜色、基准大小和 BrushFamilyBrushFamily 类似于字体系列,用于定义笔画的样式。例如,BrushFamily 可以表示某种特定样式的标记或荧光笔,从而使不同大小和颜色的笔画共享该样式。
  • StockBrushes:提供用于创建即用型 BrushFamily 实例的工厂函数。

Authoring

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

The Rendering module simplifies drawing ink strokes onto an Android Canvas. It provides CanvasStrokeRenderer for Compose and ViewStrokeRenderer for view-based layouts. These renderers optimize rendering performance and help deliver high-quality visuals, including antialiasing.

To render strokes, call the create() method to get a CanvasStrokeRenderer instance, and then call the draw() method to render either finished (Stroke) or in-progress (InProgressStroke) strokes onto a Canvas.

You can transform the canvas when you draw a stroke. Examples include panning, zooming, and rotating. To render the stroke correctly, you must also pass the canvas transform to CanvasStrokeRenderer.draw.

To avoid tracking the canvas transform separately, use ViewStrokeRenderer instead.

Storage

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.