HIBIIRO
Implementing a Partial Stroke Eraser in an Android Handwriting App with AndroidX Ink
Introduction
While building my Android handwriting notebook app (Hibiiro) using AndroidX Ink, I needed a partial eraser that erases only the touched section of a line rather than deleting the entire stroke.
Although a drawn line looks like pixels on screen, AndroidX Ink represents each stroke as a vector batch of stylus input points (position, timestamp, pressure, tilt, and orientation). Deleting a Stroke object removes the entire continuous line drawn in one pen-down gesture.
Here is how I implemented partial erasing by trimming away the segments touched by the eraser and reconstructing the remaining pieces as new independent strokes.
Environment
- Kotlin
- AndroidX Ink 1.0.0
Splitting a Stroke Along the Eraser Path
A stroke records discrete input points along the stylus path. When the pen moves quickly, the distance between consecutive sample points can be quite wide.
If we only checked whether recorded vertices fell inside the eraser radius, an eraser swipe passing right between two widely spaced vertices wouldn’t erase anything:
●──────■──────●
Eraser
Therefore, we test the line segment connecting each pair of adjacent points against the eraser shape, interpolate new boundary points where the segment enters and leaves the eraser, and split the stroke there.
1. Building the Eraser Sweep Shape
Because touch events also arrive as discrete samples, we connect the previous eraser position (startX, startY) and current position (endX, endY) into a padded segment using AndroidX Ink’s MutableParallelogram:
val shape = MutableParallelogram()
.populateFromSegmentAndPadding(
MutableSegment(
MutableVec(startX, startY),
MutableVec(endX, endY)
),
padding
)Touch coordinates are first transformed from zoomed/panned screen space into notebook page coordinates. In two-page spread view, the eraser path is clipped to the active page boundary so swiping near the spine doesn’t erase the opposite page.
2. Broad-Phase Intersection Test
Before inspecting individual input points, we run a fast intersection check against each stroke’s mesh shape. Strokes that don’t touch the eraser are kept as-is:
for (stroke in strokes) {
if (!stroke.shape.intersects(
shape,
AffineTransform.IDENTITY
)
) {
result.add(stroke)
continue
}
// Inspect and split intersecting stroke
// result.addAll(splitStroke(...))
}3. Finding the Erased Interval Along Each Segment
For intersecting strokes, we transform the segment endpoints into a local coordinate space aligned with the eraser’s movement vector, treating the sweep area as an axis-aligned box.
Parameterizing each segment from t = 0.0 (start) to t = 1.0 (end), we solve for the entry and exit parameters. For example, if 0.3 .. 0.6 falls inside the eraser, we keep 0.0 .. 0.3 and 0.6 .. 1.0 as separate stroke fragments. To prevent rounded stroke caps from protruding into the erased gap, we expand the effective radius by half the brush size:
val radius = padding + stroke.brush.size / 2.04. Interpolating Boundary Input Points
At the cut boundaries, we linearly interpolate new input points between point A and point B (for instance, at t = 0.4f):
val x = a.x + (b.x - a.x) * 0.4f
val y = a.y + (b.y - a.y) * 0.4fAlong with X/Y coordinates, we also interpolate the elapsed timestamp, pressure, tilt, and orientation (taking the shortest angular arc so interpolating between 359° and 1° doesn’t flip through 180°).
5. Rebuilding the Remaining Strokes
Finally, each surviving fragment of input points is packed into a new MutableStrokeInputBatch using the original stroke’s brush:
return fragments.map { fragment ->
val batch = MutableStrokeInputBatch()
batch.add(fragment)
Stroke(
brush = stroke.brush,
inputs = batch
)
}References (Android Developers Documentation)
- Geometry APIs — Intersection testing and eraser concepts
- Ink API Modules — Overview of Strokes, Brushes, and Rendering
- Draw Strokes — Stylus input capture and coordinate transforms
- MutableStrokeInputBatch Reference — Adding input points and timestamp/duplication constraints