模块
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Ink API 采用模块化设计,因此您只需使用所需的功能。
笔画
Strokes 模块是 Ink 库的基础,提供主要 API 接口,并包含用于与库交互的核心数据类型。此模块中的主要数据类型包括:
几何体
Geometry 模块为基本形状和复杂形状提供了一套几何图形基元,以及用于交叉检测和转换的操作。这些基元可与手写笔触无缝集成,让您能够构建全笔触橡皮擦和矩形选择工具等功能。
虽然 Box
和 Vec
等类主要用于执行几何操作,但 PartitionedMesh
可能还包含渲染专用数据。
Brush
Brush 模块
用作笔触创建和渲染的声明性配置,
其功能与文本字体类似。答
Brush
对象
以下属性:
- 颜色:可以是纯色,也可以是分层效果和
纹理。
- 尺寸:可以是固定的,也可以用作动态尺寸调整的基础。
- 系列:与文本字体类似,系列定义了笔触的整体样式。
- Epsilon:控制笔触矢量几何图形的细节级别,表示视觉区分的最小单位。
epsilon
属性会播放
在定义坐标系的精度方面起着至关重要的作用。如需有关如何选择合适的 epsilon 值的更多指导,请参阅画笔 API 部分。
BrushFamily
充当
强大的配置,无需深入研究即可创建富有表现力的笔画
复杂几何图形或渲染代码。该库提供了一组预定义的
StockBrushes
,其中包括
压敏笔、荧光笔和记号笔。
编写
借助 Authoring
模块,开发者可以捕获用户触摸输入,并将其实时渲染为屏幕上的低延迟笔触。这是通过
InProgressStrokesView
类,
该组件会处理动作事件,并在绘制时直观呈现笔触。
当笔画完成时,该模块会使用
onStrokesFinished()
回调
InProgressStrokesFinishedListener
。
借助回调,应用可以检索完成的笔触数据以进行渲染或存储。
渲染
渲染模块简化了在
Android Canvas
。模块
提供 CanvasStrokeRenderer
和
ViewStrokeRenderer
基于 View 的布局,这两者都能优化渲染性能,并确保
高品质视觉元素,包括抗锯齿功能。
如需将笔触渲染到画布,请使用 create()
方法获取 CanvasStrokeRenderer
实例。然后,使用 draw()
方法将完成或正在进行的笔触渲染到画布上。
在绘制笔触的过程中,可以对画布进行转换(平移、缩放或旋转),但为了确保笔触在屏幕上绘制的效果最佳,必须将应用于画布的转换也传递给 CanvasStrokeRenderer#draw()
。为避免需要单独跟踪此信息,请改用 ViewStrokeRenderer
。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Modules\n\nThe [Ink API](/jetpack/androidx/releases/ink#1.0.0-alpha01) is modularized, so you can use only what you need.\n\nStrokes\n-------\n\nThe [Strokes](/reference/kotlin/androidx/ink/strokes/package-summary) module serves as the foundation of the Ink library, offering the\nprimary API interface and containing the core data types for interacting with\nthe library. Key data types within this module include:\n\n- [**`StrokeInputBatch`**](/reference/kotlin/androidx/ink/strokes/StrokeInputBatch): Represents a series of pointer inputs, encompassing position, timestamp, and optionally pressure, tilt, and orientation. This data can be utilized with the [`Stroke`](/reference/kotlin/androidx/ink/strokes/Stroke) or [`InProgressStroke`](/reference/kotlin/androidx/ink/strokes/InProgressStroke) classes, and is the type that would provide the inputs for a handwriting recognition library.\n- [**`Stroke`**](/reference/kotlin/androidx/ink/strokes/Stroke): An immutable representation of a finalized stroke with fixed geometry. Stroke comprises an [`ImmutableStrokeInputBatch`](/reference/kotlin/androidx/ink/strokes/ImmutableStrokeInputBatch) (input points), a [`Brush`](/reference/kotlin/androidx/ink/brush/Brush) (styling), and a [`PartitionedMesh`](/reference/kotlin/androidx/ink/geometry/PartitionedMesh) (geometric shape). Strokes can be stored, manipulated, and rendered within your application.\n- [**`InProgressStroke`**](/reference/kotlin/androidx/ink/strokes/InProgressStroke): A mutable counterpart to [`Stroke`](/reference/kotlin/androidx/ink/strokes/Stroke) designed for incremental input handling and real-time rendering during the drawing process. While often used indirectly through [`InProgressStrokesView`](/reference/kotlin/androidx/ink/authoring/InProgressStrokesView), [`InProgressStroke`](/reference/kotlin/androidx/ink/strokes/InProgressStroke) can be leveraged directly for advanced customization.\n\nGeometry\n--------\n\nThe [Geometry](/reference/kotlin/androidx/ink/geometry/package-summary) module\nprovides a suite of geometry primitives for both basic and complex\nshapes along with operations for intersection detection and transformations.\nThese primitives seamlessly integrate with Ink strokes, empowering you to build\nfeatures like whole-stroke erasers and marquee selection tools.\n\nWhile classes like [`Box`](/reference/kotlin/androidx/ink/geometry/Box) and\n[`Vec`](/reference/kotlin/androidx/ink/geometry/Vec) primarily facilitate\ngeometric operations,\n[`PartitionedMesh`](/reference/kotlin/androidx/ink/geometry/PartitionedMesh) may\nalso include rendering-specific data.\n\nBrush\n-----\n\nThe [Brush](/reference/kotlin/androidx/ink/brush/package-summary) module\nacts as a declarative configuration for stroke creation and rendering,\nfunctioning similarly to a text font. A\n[`Brush`](/reference/kotlin/androidx/ink/brush/Brush) object ha the\nfollowing properties:\n\n- **Color**: Can be a solid color or the foundation for layered effects and textures.\n- **Size**: Can be fixed or serve as a base for dynamic size adjustments.\n- **Family**: Analogous to a text typeface, family defines the stroke's overall style.\n- **Epsilon**: Controls the level of detail in the stroke's vector geometry, representing the smallest unit of visual distinction.\n\nThe [`epsilon`](/reference/kotlin/androidx/ink/brush/Brush#epsilon()) property plays a\ncrucial role in defining the precision of your coordinate system. More guidance\nis provided in the [Brush APIs](?tab=t.0#heading=h.j5tn81xa7fph) section on how\nto choose an appropriate epsilon value.\n\nThe [`BrushFamily`](/reference/kotlin/androidx/ink/brush/BrushFamily) serves as\na powerful configuration for creating expressive strokes without delving into\ncomplex geometry or rendering code. The library provides a set of predefined\n[`StockBrushes`](/reference/kotlin/androidx/ink/brush/StockBrushes), including a\npressure-sensitive pen, highlighter, and marker.\n\nAuthoring\n---------\n\nThe [`Authoring`](/reference/kotlin/androidx/ink/authoring/package-summary)\nmodule enables developers to capture user touch input and render it as\nlow-latency strokes on the screen in real-time. This is achieved through the\n[`InProgressStrokesView`](/reference/kotlin/androidx/ink/authoring/InProgressStrokesView) class,\nwhich processes motion events and visualizes the strokes as they're drawn.\n\nOnce a stroke is completed, the module notifies the client application using the\n[`onStrokesFinished()`](/reference/kotlin/androidx/ink/authoring/InProgressStrokesFinishedListener#onStrokesFinished(kotlin.collections.Map))\ncallback of\n[`InProgressStrokesFinishedListener`](/reference/kotlin/androidx/ink/authoring/InProgressStrokesFinishedListener).\nThe callback allows the application to retrieve the\nfinished stroke data for rendering or storage.\n\nRendering\n---------\n\nThe Rendering module simplifies the process of drawing ink strokes onto an\nAndroid [`Canvas`](/reference/kotlin/android/graphics/Canvas). The module\nprovides a [`CanvasStrokeRenderer`](/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer)\nfor Compose and\n[`ViewStrokeRenderer`](/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer)\nfor view-based layouts, both of which optimize rendering performance and ensure\nhigh-quality visuals, including anti-aliasing.\n\nTo render strokes to a canvas, obtain a\n[`CanvasStrokeRenderer`](/reference/kotlin/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer)\ninstance using the\n[`create()`](/reference/kotlin/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#create())\nmethod. Then, use the\n[`draw()`](/reference/kotlin/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform))\nmethod to render either finished or in-progress strokes.\nstrokes onto a canvas.\n\nThe canvas can be transformed (panned, zoomed, or rotated) as part of drawing the\nstroke, but to make sure the stroke looks its best drawn on screen, the\ntransform applied to the canvas must also be passed to\n`CanvasStrokeRenderer#draw()`.\nTo avoid needing to keep track of this separately, use\n[`ViewStrokeRenderer`](/reference/kotlin/androidx/ink/rendering/android/view/ViewStrokeRenderer)\ninstead."]]