# ViewStrokeRenderer

Artifact: [androidx.ink:ink-rendering](https://developer.android.com/jetpack/androidx/releases/ink) [View Source](https://cs.android.com/search?q=file:androidx/ink/rendering/android/view/ViewStrokeRenderer.kt+class:androidx.ink.rendering.android.view.ViewStrokeRenderer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

*** ** * ** ***


```
public final class ViewStrokeRenderer
```

<br />

*** ** * ** ***

Helps developers using Android Views to draw `https://developer.android.com/reference/androidx/ink/strokes/Stroke` objects in their UI, in an easier way than using `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer` directly. Construct this once for your `https://developer.android.com/reference/android/view/View.html` and reuse it during each `https://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)` call.

This utility is valid as long as `https://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)`

1. Does not call `https://developer.android.com/reference/android/graphics/Canvas.html#setMatrix(android.graphics.Matrix)`.

2. Does not modify `https://developer.android.com/reference/android/graphics/Canvas.html` transform state prior to calling `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function1)`.

3. Does not use `https://developer.android.com/reference/android/graphics/RenderEffect.html`, either setting it on this `https://developer.android.com/reference/android/view/View.html` or a subview using `https://developer.android.com/reference/android/view/View.html#setRenderEffect(android.graphics.RenderEffect)`, or by calling `https://developer.android.com/reference/android/graphics/Canvas.html#drawRenderNode(android.graphics.RenderNode)` using a `https://developer.android.com/reference/android/graphics/RenderNode.html` that has been configured with `https://developer.android.com/reference/android/graphics/RenderNode.html#setRenderEffect(android.graphics.RenderEffect)`. Developers who want to use `https://developer.android.com/reference/android/graphics/RenderEffect.html` in conjunction with `https://developer.android.com/reference/androidx/ink/strokes/Stroke` rendering must use `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` directly.

Example:

```
class MyView(context: Context) : View(context) {
  private val viewStrokeRenderer = ViewStrokeRenderer(myCanvasStrokeRenderer, this)

  override fun onDraw(canvas: Canvas) {
    viewStrokeRenderer.drawWithStrokes(canvas) { scope ->
      canvas.scale(myZoomLevel)
      canvas.rotate(myRotation)
      canvas.translate(myPanX, myPanY)
      scope.drawStroke(myStroke)
      // Draw other objects including more strokes, apply more transformations, etc.
    }
  }
}
```

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#ViewStrokeRenderer(androidx.ink.rendering.android.canvas.CanvasStrokeRenderer,android.view.View)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer canvasStrokeRenderer, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view )` |

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html canvas, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/rendering/android/canvas/StrokeDrawScope, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Kotlin developers should call this at the beginning of `https://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)` and perform their `https://developer.android.com/reference/android/graphics/Canvas.html` manipulations within its scope. |
| `final void` | `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function2)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html canvas, @https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/rendering/android/canvas/StrokeDrawScope, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Java developers should call this at the beginning of `https://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)` and perform their `https://developer.android.com/reference/android/graphics/Canvas.html` manipulations within its scope. |

## Public constructors

### ViewStrokeRenderer

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
public ViewStrokeRenderer(
    @NonNull CanvasStrokeRenderer canvasStrokeRenderer,
    @NonNull View view
)
```

## Public methods

### drawWithStrokes

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
public final void drawWithStrokes(
    @NonNull Canvas canvas,
    @NonNull Function1<@NonNull StrokeDrawScope, Unit> block
)
```

Kotlin developers should call this at the beginning of `https://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)` and perform their `https://developer.android.com/reference/android/graphics/Canvas.html` manipulations within its scope.

For example:

```
viewStrokeRenderer.drawWithStrokes(canvas) { scope ->
  canvas.scale(...) // or concat, or translate, or rotate, etc.
  scope.drawStroke(stroke)
  // Repeat with other strokes, draw other things to the canvas, etc.
}
```

Java callers should prefer to use the non-inline overload of `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function1)` with a non-capturing lambda or an object that is allocated once and reused.

### drawWithStrokes

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
public final void drawWithStrokes(
    @NonNull Canvas canvas,
    @NonNull Function2<@NonNull Canvas, @NonNull StrokeDrawScope, Unit> block
)
```

Java developers should call this at the beginning of `https://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)` and perform their `https://developer.android.com/reference/android/graphics/Canvas.html` manipulations within its scope.

The structure of the callback in this non-inline version makes it easier for Java callers to write performant code, since forwarding the value of `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function2)` allows a lambda to be non-capturing, thereby avoiding an allocation of the lambda on every frame.

For example:

```
viewStrokeRenderer.drawWithStrokes(canvas, (scopedCanvas, scope) -> {
  // Make sure to use `scopedCanvas` rather than `canvas` in this lambda!
  scopedCanvas.scale(...); // or concat, or translate, or rotate, etc.
  scope.drawStroke(stroke);
  // Repeat with other strokes, draw other things to the canvas, etc.
});
```

Alternatively, the callback could be an object that is allocated once and reused.

Kotlin callers should prefer to use the inline overload of `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function2)`, as it better guarantees that the lambda argument will not cause an allocation.