Is there a device-agnostic way to draw a 1 px path that follows the physical rounded edge of an Android display?
As part of a larger problem, I'm trying to draw a 1 px line that hugs the physical screen edge with no gaps. Using WindowInsets.getRoundedCorner(...).radius directly in drawRoundRect does not work on my device or on any emulators I tried: the straight edges draw correctly, but the corner arcs are completely clipped.
I have an empirical workaround that produces the result I want on my personal test device, but I am concerned about portability and feel there is an actual intended solution that I've missed.
val insets = windowManager.currentWindowMetrics.windowInsets val r = insets.getRoundedCorner(RoundedCorner.POSITION_TOP_LEFT)?.radius?.toFloat() ?: 0f Canvas(modifier = Modifier.fillMaxSize()) { drawRoundRect( color = Color.White, topLeft = Offset(0f, 0f), size = size, cornerRadius = CornerRadius(r), style = Stroke(width = 1f) ) }
A 1 px white line that follows the physical screen edge, including the rounded corners.
The straight edges are visible, but the corner arcs are completely clipped.
From testing on my device, there seem to be multiple things going on:
Pixels can exist in software coordinate space but still land outside the physical glass boundary, in which case they are silently clipped.
If I want an inset curve D pixels from the screen edge, I cannot just shrink the bounding rect by D and keep the same corner radius. That produces curves that intersect rather than remain parallel.
The radius returned by getRoundedCorner() does not appear to match the curve needed to visually follow the physical glass edge on my device.
I've attached these images to show what I'm seeing:
The only way I found to make the border visually follow the physical corner was to calibrate the geometry empirically on the device.
What I did was:
draw multiple candidate arcs with different radii
pin them to the same fixed anchor point after finding the true corner (x,y)
compare them visually against the physical corner
use the radius and inset that matched the hardware
Once those values were found, the final draw itself was workable:
@Composable fun EdgePixelScreen() { val R = 68f // empirically matched physical corner radius on test device val magentaD = 8f // inset where clipping artifacts stopped on test device val context = LocalContext.current var rectParams by remember { mutableStateOf<Pair<Offset, Size>?>(null) } var canvasSize by remember { mutableStateOf<Size?>(null) } LaunchedEffect(canvasSize) { val cs = canvasSize ?: return@LaunchedEffect val windowManager = context.getSystemService(WindowManager::class.java) val insets: AndroidWindowInsets = windowManager.currentWindowMetrics.windowInsets val tlR = insets .getRoundedCorner(RoundedCorner.POSITION_TOP_LEFT) ?.radius ?.toFloat() ?: R val invSqrt2 = (1.0 / sqrt(2.0)).toFloat() // 45° point of the reference arc val tipX = tlR - (tlR - magentaD) * invSqrt2 // Center of the empirically matched arc passing through that point val cx = tipX + R * invSqrt2 val left = cx - R rectParams = Pair( Offset(left, left), Size(cs.width - left * 2f, cs.height - left * 2f) ) } Box( modifier = Modifier .fillMaxSize() .background(Color.Black) ) { Canvas(modifier = Modifier.fillMaxSize()) { if (canvasSize == null) canvasSize = size rectParams?.let { (topLeft, rectSize) -> drawRoundRect( color = Color.White, topLeft = topLeft, size = rectSize, cornerRadius = CornerRadius(R), style = Stroke(width = 1f) ) } } } }
That works on my test device, but it feels like a workaround rather than a real solution.
If the goal is to draw a path that actually hugs the physical rounded corner of the display, what is the correct Android-supported way to derive that geometry?
More specifically, is there a device-agnostic way to reconcile the software drawable boundary with the physical glass edge, or is empirical per-device calibration the only option?