Brush API

Brush API 可为您提供用于定义笔画视觉样式的工具。您可以创建具有不同颜色、大小和系列的笔刷,以实现各种外观效果。

创建画笔

如需创建画笔,请使用带有命名实参(例如 Brush.Companion.createWithComposeColor)的 Compose Brush 辅助方法。此类可让您设置以下属性:

  • family:笔刷的样式,类似于文本中的字体。 如需了解可用的 BrushFamily 值,请参阅 StockBrushes
  • color:画笔的颜色。您可以使用 ColorLong 设置颜色。
  • size:使用画笔创建的笔画的整体粗细。
  • epsilon:为了生成笔画几何图形,应将两个点视为在视觉上不同的最小距离。epsilon 和笔画点的比率控制着笔画在不产生伪影的情况下可放大的程度,但会消耗内存。笔画单位的良好起点是 1 px,而 epsilon 的良好起点是 0.1。较高的 epsilon 值会使用较少的内存,但允许的缩放程度较低,之后会出现三角形伪影。 通过实验找出适合您使用场景的值。
val brush = Brush.createWithComposeColor(
  family = StockBrushes.pressure(),
  colorIntArgb = Color.Black,
  size = 5F,
  epsilon = 0.1F
)

修改笔刷属性

您可以使用 copyWithComposeColor() 方法创建现有笔刷的副本,从而更改笔刷的任何属性。

val redBrush = Brush.createWithComposeColor(
  family = StockBrushes.pressurePen(),
  colorIntArgb = Color.RED,
  size = 5F,
  epsilon = 0.1F
)

val blueBrush = redBrush.copyWithComposeColor(color = Color.BLUE)

自定义笔刷

While StockBrushes provides a versatile set of common brushes, Ink API also offers an advanced path for creating entirely new brush behaviors for unique artistic effects or to replicate specific existing brushes for backward compatibility.

A custom BrushFamily is loaded from its serialized format. The required format is the gzipped binary encoding of the BrushFamily protocol buffer. This lets you load and use custom brush files today. Once deserialized, the custom BrushFamily can be used to create a new Brush with a specific color and size, just like any of the StockBrushes families.