绘制笔画

为了帮助您以惯用的 Compose 方式创作笔画,Ink API 提供了用于创作、笔刷和几何图形的 Compose 互操作性模块。

如需在 Compose 中绘制笔画,请使用 InProgressStrokes 可组合项,该可组合项需要默认画笔实例、用于替换默认画笔的方法以及用于处理完成的笔画的回调。

  1. 设置界面组件

    InProgressStrokes(
      defaultBrush = currentBrush,
      nextBrush = onGetNextBrush,
      onStrokesFinished = onStrokesFinished,
    )
    
  2. 处理完成的笔画

    当湿笔画变为干笔画时,它们会通过 InProgressStrokesonStrokesFinished 回调实参传递给应用。

    您的应用必须将完成的笔画传递给同一界面线程中的另一个可组合项,才能将其提交到屏幕。

    @Composable
    fun DrawingScreen(
      finishedStrokes: List<Strokes>,
      onStrokesFinished: (List<Stroke>) -> Unit,
      currentBrush: Brush,
      onGetNextBrush: () -> Brush,
      modifier: Modifier = Modifier
    ) {
      val canvasStrokeRenderer = remember { CanvasStrokeRenderer.create() }
    
      Box(modifier = Modifier.fillMaxSize()) {
          // The Canvas for drawing the permanent, dry strokes.
          Canvas(modifier = Modifier.fillMaxSize()) {
              finishedStrokes.forEach { stroke ->
                  canvasStrokeRenderer.draw(
                      stroke = stroke,
                      canvas = this,
                      strokeToScreenTransform = Matrix()
                  )
              }
          }
    
          //The wet ink layer for live drawing.
          // The InProgressStrokes composable for the wet ink layer goes here.
      }
    }