# 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.android.kt+class:androidx.ink.rendering.android.view.ViewStrokeRenderer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

*** ** * ** ***


```
class ViewStrokeRenderer
```

<br />

*** ** * ** ***

Helps developers using Android Views to draw `https://developer.android.com/reference/kotlin/androidx/ink/strokes/Stroke` objects in their UI, in an easier way than using `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/androidx/ink/strokes/Stroke` rendering must use `https://developer.android.com/reference/kotlin/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` directly.

Example:

```kotlin
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/kotlin/androidx/ink/rendering/android/view/ViewStrokeRenderer#ViewStrokeRenderer(androidx.ink.rendering.android.canvas.CanvasStrokeRenderer,android.view.View)( canvasStrokeRenderer: https://developer.android.com/reference/kotlin/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer, view: https://developer.android.com/reference/android/view/View.html )` |

| ### Public functions |
|---|---|
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function1)(canvas: https://developer.android.com/reference/android/graphics/Canvas.html, block: (https://developer.android.com/reference/kotlin/androidx/ink/rendering/android/canvas/StrokeDrawScope) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` 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. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/ink/rendering/android/view/ViewStrokeRenderer#drawWithStrokes(android.graphics.Canvas,kotlin.Function2)( canvas: https://developer.android.com/reference/android/graphics/Canvas.html, block: (scopedCanvas: https://developer.android.com/reference/android/graphics/Canvas.html, https://developer.android.com/reference/kotlin/androidx/ink/rendering/android/canvas/StrokeDrawScope) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` 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)

```
ViewStrokeRenderer(
    canvasStrokeRenderer: CanvasStrokeRenderer,
    view: View
)
```

## Public functions

### drawWithStrokes

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

```
inline fun drawWithStrokes(canvas: Canvas, block: (StrokeDrawScope) -> Unit): Unit
```

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:

```kotlin
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/kotlin/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)

```
fun drawWithStrokes(
    canvas: Canvas,
    block: (scopedCanvas: Canvas, StrokeDrawScope) -> Unit
): Unit
```

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/kotlin/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:

```kotlin
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/kotlin/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.