Class

BorderRenderer

BorderRenderer provides rendering and caching logic for drawing borders inside a DrawScope.

Source set: Common
public class BorderRenderer constructor(private val onProvideGraphicsLayer: () -> GraphicsLayer)

BorderRenderer provides rendering and caching logic for drawing borders inside a DrawScope.

BorderRenderer provides the same border drawing that androidx.compose.foundation.border uses so you can draw borders from any drawing context, such as a androidx.compose.ui.node.DrawModifierNode. This can be useful when you need to render multiple inset borders at once, or efficiently animate borders. For simple cases you can continue to use androidx.compose.foundation.border.

Borders drawn with BorderRenderer are internal borders - they grow inwards as the border width increases.

BorderRenderer contains caches to efficiently draw borders over time. As a result, make sure to:

  • Reuse the same BorderRenderer instance each time you draw the border
  • want to draw two borders, inset from each other, each border should have its own BorderRenderer. See the samples for more information.

  • Use a separate instance for each border that is drawn at the same time. For example, if you

Drawing a border from inside a androidx.compose.ui.node.DrawModifierNode:

Drawing two borders inset from each other (using separate BorderRenderers):

Parameters

onProvideGraphicsLayer Provides a GraphicsLayer needed for rendering certain borders. This lambda will be invoked when needed, and may be invoked multiple times. As a result make sure to cache / return the same valid GraphicsLayer to avoid creating many layers. This GraphicsLayer should not be used by any other BorderRenderer instances or drawing code at the same time. The caller is responsible for its lifecycle - make sure to call androidx.compose.ui.graphics.GraphicsContext.releaseGraphicsLayer when you no longer need it (such as when a Modifier.Node using this is detached). If you release the layer, make sure to return a new valid GraphicsLayer when this is called again (if the node is re-attached later). See the samples for example usage.

Functions

drawBorder

Source set: Common
public fun drawBorder(
        drawScope: DrawScope,
        width: Dp,
        brush: Brush,
        outline: Outline,
    ): Unit

Draws a border defined by outline, with width and brush, into drawScope. The border will be drawn using the size defined by outline, not DrawScope.size. Borders drawn are internal borders (drawn within the bounding box, not outside), and grow inwards as width increases.

To modify the drawn border you can use DrawScope transformation APIs such as translate, just as for any other drawing commands.

To draw several different borders at the same time, use a separate BorderRenderer for each one.

Parameters

drawScope The DrawScope to draw the border into.
width The width of the border in Dp.
brush The Brush to paint the border with.
outline The Outline of the border.
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: