vide
10/11/2023, 6:02 PMResources.getSystem().displayMetrics.widthPixels
gives an inconsistent value inside the same preview. Is there something I should be aware of? Repro and screenshot in 🧵@Preview(device="width=1920,height=1080")
@Composable
fun P1() {
Resources.getSystem().displayMetrics.widthPixels // value here is 1920
// multiple levels deep:
Resources.getSystem().displayMetrics.widthPixels // value here is 3840
}
@Preview(device="width=3840,height=2160")
@Composable
fun P2() {
Resources.getSystem().displayMetrics.widthPixels // value here is 3840
}
Resources.getSystem()
isn't safe to call..?@Preview(device = "spec:shape=Normal,width=3840,height=2160,unit=px,dpi=480")
@Composable
private fun Test1() = Text("${Resources.getSystem().displayMetrics.widthPixels}", fontSize = 100.sp)
@Preview(device = "spec:shape=Normal,width=5000,height=5000,unit=px,dpi=800")
@Composable
private fun Test2() = Column {
Text("${Resources.getSystem().displayMetrics.widthPixels}", fontSize = 100.sp)
Boundary()
}
@Composable
fun Boundary() {
val a = animateFloatAsState(0f)
Text("${Resources.getSystem().displayMetrics.widthPixels}", fontSize = 100.sp)
}
Alex Vanyo
10/11/2023, 7:00 PMResources.getSystem().displayMetrics.widthPixels
is a super suspicious call. What are you trying to use that value for?vide
10/11/2023, 7:10 PMAlex Vanyo
10/11/2023, 8:35 PMWindowMetricsCalculator
?
Resources.getSystem
is returning something that isn’t intended to be configured for the current screen: https://developer.android.com/reference/android/content/res/Resources#getSystem()
LocalContext.current.resources.displayMetrics.widthPixels
might work slightly better since it’ll be the resources for your current screen (but again in general this is something to avoid, that doesn’t take into account multi-window mode, changing displays, orientation, etc.)vide
10/12/2023, 3:59 AMLocalContext.current
for every single size definition from a perf perspective. It would also require adding a composable context to the helper function, meaning no more definitions outside composable contexts.inline val Int.dpx get(): Dp = (this * dpxToDpMultiplier).dp
where the multiplier is global, set on based the maxWidth constraint available to the app root view at startup time (and set once per preview by a preview wrapper composable)