What you’ll study
- What
Canvas,PaintandPatheach decide, and how Compose’sDrawScopemaps onto them. - Every
Paintsetting that changes how a stroke looks: style, width, caps, joins, the miter limit. - Building shapes with
Path, measuring them withPathMeasure, and combining them withPath.op. - Path effects: rounded corners and dashes without changing your path.
- Blend modes, and why an eraser needs its own layer.
- What the GPU does with your paths, and the costs that follow.
How it connects
Every drawing call answers three questions. Where to draw is the Canvas: a view’s surface, a
bitmap, or a layer. What to draw is a shape, usually a Path. How to draw it is the Paint:
colour, style, width, caps, joins, effects and blending. Compose asks the same three questions with
different names: the DrawScope is the canvas, the shape is a Compose Path, and the paint is split
into a colour or brush, a DrawStyle (Fill or Stroke) and a BlendMode.
This is the second guide in the ink series. Guide 1
showed what goes wrong in a naive drawing app; several of those fixes are Paint settings, and this
guide explains all of them.
Read it at your level
| Level | Read | You should be able to |
|---|---|---|
| Student | The basics, the paint lab | Draw a stroked and a filled shape and say what each Paint setting changes |
| Fresher | + Style and width, caps and joins | Pick caps and joins for ink, and explain a hairline |
| Junior | + Path, path effects, the Compose section | Build curves, round corners, dash a line, and draw the same thing in Compose |
| Mid | + Blend modes and erasing | Build an eraser that clears only the ink |
| Senior | + What the GPU does, edge cases | Predict which drawing choices cost frames, and why |
The basics
class StrokeView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val path = Path().apply { // what
moveTo(40f, 200f)
lineTo(110f, 60f)
quadTo(160f, 0f, 240f, 120f)
}
private val ink = Paint().apply { // how (anti-aliased by default from Android 12)
color = Color.BLACK
style = Paint.Style.STROKE
strokeWidth = 12f
strokeCap = Paint.Cap.ROUND
strokeJoin = Paint.Join.ROUND
}
override fun onDraw(canvas: Canvas) { // where
canvas.drawPath(path, ink)
}
}
Create Path and Paint objects once, not in onDraw(): onDraw() runs every frame the view changes,
and allocating there creates garbage on the main thread.
The same stroke in Compose:
@Composable
fun StrokeCanvas(modifier: Modifier = Modifier) {
Canvas(modifier.fillMaxSize()) {
val path = Path().apply {
moveTo(40f, 200f)
lineTo(110f, 60f)
quadraticTo(160f, 0f, 240f, 120f)
}
drawPath(
path = path,
color = Color.Black,
style = Stroke(width = 12f, cap = StrokeCap.Round, join = StrokeJoin.Round),
)
}
}
Try it: the paint lab
One zigzag (with one very sharp turn) and one triangle, drawn with every setting this guide covers. The faint dashed line is the path itself, so you can see how far the stroke reaches past it. The lab uses the browser’s canvas, which has the same ideas under other names; the eraser below it is covered in Blend modes and erasing.
Style and width
Paint.Style has three values:
| Style | What it draws |
|---|---|
FILL |
The inside of the shape. The reference says it ignores “all stroke-related settings in the paint”. |
STROKE |
A band along the outline, using width, cap, join and effects. |
FILL_AND_STROKE |
Both at once, with one colour. |
A stroke is centred on the path: half the width falls inside, half outside. That is why a 12-pixel stroke around a 100-pixel square covers 112 pixels, and why erasers and hit tests must use the stroke’s outline, not its path.
Width 0 is special. setStrokeWidth
says: “Pass 0 to stroke in hairline mode. Hairlines always draws a single pixel independent of the
canvas’s matrix.” Zoom in 10 times and a hairline is still one pixel. That is what you want for grid
lines and selection outlines, and not what you want for ink. Compose has the same rule:
Stroke.HairlineWidth is 0, “a hairline stroke of 1 pixel”. Try width 0 and Zoom ×3 in the lab.
Caps, joins and the miter limit
Caps decide how an open line ends; joins decide how two segments meet.
| Setting | Values | What you see |
|---|---|---|
Paint.Cap |
BUTT (the default), ROUND, SQUARE |
Butt stops exactly at the end point; round and square reach half the width past it |
Paint.Join |
MITER (the default), ROUND, BEVEL |
Miter makes a sharp point, round an arc, bevel a flat cut |
setStrokeMiter |
a number | How long a miter point may get before it turns into a bevel |
The defaults come from Skia, the graphics library under Android’s canvas:
SkPaint.h sets
kDefault_Cap = kButt_Cap and kDefault_Join = kMiter_Join.
A miter join at a sharp turn grows a long spike, because the outer edges keep going until they meet.
The miter limit caps that: when the spike would be longer than the limit times the stroke width,
the join falls back to a bevel. Compose’s default (Stroke.DefaultMiter) is 4. In the lab, pick
Miter, raise the width, and move the limit while watching the sharpest turn of the zigzag.
For handwriting and ink, use round caps and round joins: strokes are open, their turns are sharp and unpredictable, and round never spikes or notches.
Anti-aliasing blends the edge pixels so a line doesn’t look stepped. From Android 12, Paint()
turns it on for you; AOSP’s
Paint.java
documents that “On devices running S and above, ANTI_ALIAS_FLAG is set by this constructor”. On
Android 11 and older, Paint() started with no flags, so pass Paint.ANTI_ALIAS_FLAG if you support
them.
Path: the shape
Path is a list of drawing steps:
| Call | Adds |
|---|---|
moveTo(x, y) |
Start a new contour here, without drawing |
lineTo(x, y) |
A straight line |
quadTo(x1, y1, x2, y2) |
“A quadratic bezier from the last point, approaching control point (x1,y1), and ending at (x2,y2)” |
cubicTo(...) |
A cubic Bézier, with two control points |
close() |
A line back to the contour’s start |
Guide 1’s midpoint trick (quadTo through each sample, ending at the midpoint of the next two) is the
cheapest way to turn touch samples into a smooth curve.
Two tools turn a path from something you draw into something you can reason about:
PathMeasure walks along a path. getLength() gives its length, getPosTan(distance, pos, tan)
gives the point and direction at any distance along it, and getSegment() copies out a piece of it.
That is how you place brush stamps at even spacing, trim the start of a stroke, or animate a line
being drawn.
val measure = PathMeasure(path, false)
val pos = FloatArray(2)
val tan = FloatArray(2)
measure.getPosTan(measure.length / 2, pos, tan) // the halfway point, and which way the stroke is heading
Path.op (API 19) combines two shapes: union, intersection, difference, XOR. It works on the
areas paths enclose, so a vector eraser has to turn each stroke into its outline first; that is a job
for the eraser guide later in the series.
val remaining = Path(shape).apply { op(holeShape, Path.Op.DIFFERENCE) }
Path effects
A path effect changes the shape as it is drawn, without touching your Path.
| Effect | Views | Compose | Use it for |
|---|---|---|---|
| Rounded corners | CornerPathEffect(radius) |
PathEffect.cornerPathEffect(radius) |
The cheapest smoothing there is: every sharp angle becomes an arc |
| Dashes | DashPathEffect(intervals, phase) |
PathEffect.dashPathEffect(intervals, phase) |
Guides, selection marquees; animate phase for marching ants |
| Combined | ComposePathEffect, SumPathEffect |
PathEffect.chainPathEffect(outer, inner) |
Rounded dashes and similar |
| Stamps | PathDashPathEffect |
PathEffect.stampedPathEffect(...) |
A shape repeated along the line, like a chain or a textured brush |
CornerPathEffect “replaces any sharp angles between line segments into rounded angles of the
specified radius”, for both strokes and fills. Pick Rounded corners in the lab. For DashPathEffect,
the intervals array holds on and off lengths in turn, and must have an even number of entries.
Blend modes and erasing
A blend mode decides how new pixels combine with the ones already there. Since API 29, Paint takes a
BlendMode (before that, a
PorterDuffXfermode). The one ink apps care about is CLEAR: “Destination pixels covered by the
source are cleared to 0.” Draw a stroke with it and you get a pixel eraser.
The catch is what gets cleared. On a canvas that also holds everything behind your ink, the eraser clears that too. Compose’s graphics modifiers guide says it directly: if you don’t use an offscreen layer, “the BlendMode interacts with all the background content”. The lab shows both: drag in either box and compare.
The fix is to give the ink its own layer, so CLEAR only reaches the ink.
class InkView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
init {
// The view draws into its own GPU layer, so CLEAR can only clear this view's pixels.
setLayerType(LAYER_TYPE_HARDWARE, null)
}
private val eraser = Paint().apply {
style = Paint.Style.STROKE
strokeWidth = 40f
strokeCap = Paint.Cap.ROUND
strokeJoin = Paint.Join.ROUND
blendMode = BlendMode.CLEAR // API 29; before that: xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
}
}
Canvas.saveLayer() also gives you an offscreen layer, inside one draw call, but the
Canvas reference warns that it is
“very expensive, incurring more than double rendering cost for contained content”, and recommends a
hardware layer on the view instead.
In Compose, the layer comes from graphicsLayer:
Canvas(
modifier.graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen),
) {
drawPath(inkPath, Color.Black, style = Stroke(width = 8.dp.toPx()))
drawPath(eraserPath, Color.Black, style = Stroke(width = 40.dp.toPx()), blendMode = BlendMode.Clear)
}
A pixel eraser is simple but destructive: the strokes are gone as objects, so you can’t undo one stroke, move it, or save it as vectors. Most note and whiteboard apps erase whole strokes, or cut strokes as geometry. The eraser guide covers both.
Raster or retained
There are two ways to keep a drawing:
Raster: draw into a Bitmap |
Retained: keep a list of strokes | |
|---|---|---|
| Drawing a frame | One drawBitmap(), however much is drawn |
Grows with the number of strokes, unless finished strokes are grouped |
| Memory | Width × height × 4 bytes, even for an empty page | Grows with the number of points |
| Zoom | Pixels blur | Stays sharp |
| Undo, move, erase one stroke | Hard | Natural |
| Save | A picture | Vectors you can edit and sync |
Guide 1 used a bitmap for finished strokes because it is the quickest fix. Ink apps that need undo, zoom and sync keep strokes as data and group finished ones into render nodes, which later guides build.
What the GPU does with your drawing
Views draw through the GPU: hardware acceleration has been on by default for apps that target API 14 or higher. Android’s hardware acceleration guide lists what that changes. The parts that matter for ink:
- Paths become texture masks. “Every time you create or modify a path, the hardware pipeline creates a new mask, which can be expensive.” A live stroke that changes every frame pays this every frame; a finished stroke that never changes pays it once. Another reason to keep the live stroke separate from finished ones.
- Bitmaps are uploaded again when they change. “Every time you change the content of a bitmap, it is uploaded again as a GPU texture the next time you draw it.” A raster drawing that changes every frame uploads the whole bitmap every frame.
- Translucent views cost double. A view made translucent with
setAlpha()is “rendered in an off-screen buffer which doubles the required fill-rate”. - Some features are missing or arrived late. In the guide’s table,
setMaskFilter()(blur and emboss) is not supported at all on the hardware path, path effects on lines arrived in API 28, andclipPath()in API 18. Check the table before you build a brush on one of them.
The same toolkit in Compose
| Views | Compose |
|---|---|
Canvas in onDraw() |
DrawScope in Canvas { }, Modifier.drawBehind { }, Modifier.drawWithContent { } |
Paint.Style.FILL / STROKE |
Fill / Stroke(width, miter, cap, join, pathEffect) |
Paint.Cap, Paint.Join |
StrokeCap, StrokeJoin |
CornerPathEffect, DashPathEffect |
PathEffect.cornerPathEffect, PathEffect.dashPathEffect |
paint.blendMode |
the blendMode argument of each draw call |
| A hardware layer for an eraser | graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen) |
Create Path and Paint once, outside onDraw() |
Modifier.drawWithCache { } |
drawWithCache “keeps the objects that are created inside of it cached. The objects are cached as long
as the size of the drawing area is the same, or any state objects that are read have not changed.”
Use it for anything expensive to build, such as a background grid:
Modifier.drawWithCache {
val grid = Path().apply {
var y = 0f
while (y < size.height) {
moveTo(0f, y)
lineTo(size.width, y)
y += 24.dp.toPx()
}
}
onDrawBehind { drawPath(grid, Color.LightGray, style = Stroke(width = Stroke.HairlineWidth)) }
}
Checkpoint
You should be able to say:
Canvasis where,Pathis what,Paintis how; Compose splits the paint into colour, style and blend mode.- A stroke is centred on its path, and width 0 is a one-pixel hairline at any zoom.
- Ink wants round caps and joins; the defaults are butt and miter.
CLEARclears everything under it on that canvas, so an eraser needs its own layer.- Every changed path costs the GPU a new mask.
Edge cases
- Very thin strokes on high-density screens. A 1-pixel stroke is barely visible at 3× density;
set widths in dp (
TypedValue.applyDimensionin Views,.dp.toPx()in Compose), and use the hairline only when you mean one physical pixel. - A tap is a stroke with one point. Don’t rely on how a renderer draws a path of zero length;
draw a tap explicitly as a dot, with
drawCircle()ordrawPoint()and a round cap. - Erasing the background by accident. Any
CLEARdrawn without its own layer reaches whatever is under the ink on that canvas, including backgrounds drawn by the same view.
Cross questions
You draw a 20-pixel stroke around a 100-pixel square. How big is the result?
120 pixels. A stroke is centred on the path, so half of the width (10 pixels) falls outside on every side.
What does strokeWidth = 0f draw?
A hairline: one pixel wide whatever the canvas scale. Useful for grids and outlines, wrong for ink you expect to zoom.
Your thick strokes grow sharp spikes at quick turns. Name two fixes.
Use Paint.Join.ROUND (or BEVEL), or lower the miter limit so long miters fall back to bevels. For ink, round joins and round caps.
Your eraser, drawn with BlendMode.CLEAR, also cuts holes in the background. Why, and what is the fix?
CLEAR clears every pixel under it on that canvas, including what was drawn behind the ink. Draw the ink on its own layer: a hardware layer on the view, or CompositingStrategy.Offscreen in Compose.
Why not use saveLayer() for every eraser stroke?
The reference calls it very expensive, more than double the rendering cost of what it contains. A hardware layer on the view does the same job much more cheaply.
The live stroke is janky, but finished strokes are fine. What is the GPU doing?
Building a new texture mask for the live path every time it changes. Keep the live stroke short and separate, and keep finished strokes unchanged so their masks are reused.
What is the Compose version of creating your Paint once, outside onDraw()?
Modifier.drawWithCache, which keeps what you build until the size or the state it reads changes.
Revision: key points
Canvasis where,Pathis what,Paintis how.FILLignores stroke settings; a stroke is centred on the path; width 0 is a one-pixel hairline.- Caps end lines, joins meet them; the defaults are butt and miter; ink wants round and round.
- The miter limit turns long spikes into bevels; Compose’s default is 4.
Paint()is anti-aliased from Android 12; pass the flag for older releases.PathMeasurewalks a path;Path.opcombines shapes (API 19).CornerPathEffectandDashPathEffectchange a shape as it is drawn; Compose has the same inPathEffect.BlendMode.CLEAR(API 29) erases pixels, but only safely on the ink’s own layer;saveLayer()is the expensive way to get one.- Every changed path is a new GPU mask, and every changed bitmap is a new upload.
Cheat sheet
Each concept in a few words.
| Concept | In a few words |
|---|---|
| Canvas, Path, Paint | Where to draw, what shape, and how it looks. |
| FILL and STROKE | Fill paints the inside; stroke paints a line centred on the path. |
| Hairline | Stroke width 0 draws one pixel wide. |
| Cap | How a line’s ends look: butt, round or square. |
| Join | How corners look: miter, round or bevel. |
| Miter limit | Past it, a long miter spike becomes a bevel. |
PathMeasure |
Walks a path: length, points, tangents. |
Path.op |
Combines two shapes: union, difference, intersect (API 19). |
| Path effects | Corner rounding or dashes applied as a path is drawn. |
BlendMode.CLEAR |
Erases pixels; safe only on the ink’s own layer (API 29). |
saveLayer() |
An offscreen buffer inside one draw; costly. |
| Raster vs retained | A bitmap of pixels, or a list of strokes you can edit. |
| GPU mask, upload | Every changed path is a new mask; every changed bitmap is uploaded again. |
Read next
- References:
Paint,Paint.Style,Paint.Cap,Paint.Join,Canvas,Path,PathMeasure,BlendMode - Hardware acceleration: the supported-features table and the performance tips
- Compose: Graphics in Compose, Graphics modifiers,
Stroke,PathEffect - Under the hood: AOSP’s
Paint.javaand Skia’sSkPaint.h
Search terms that find more: stroke join miter limit, Porter-Duff compositing, hardware layer, path tessellation, Skia.
The fine print
Facts are from the Android and Compose reference pages, the hardware acceleration guide, AOSP main
and Skia main, as of November 2026. The paint lab runs on the browser’s canvas, which follows the
same model (its destination-out plays the part of CLEAR), so pixels can differ slightly from a
device.
Part of the ink series: 1. Why naive drawing feels bad.