Open aczeccssa opened 2 months ago
var renderSize= UIScreen.main.bounds.size
var pixelSize = UIScreen.main.nativeBounds.size
// Platform API
val renderSize= UIScreen.main.bounds...
val pixelSize = UIScreen.main.nativeBounds...
With the help of expect/actual, you can use LocalConfiguration.current.screenWidthDp on Android and LocalWindowInfo.current.containerSize on non-Android (starting from 1.5.10-beta01).
/** Getting screen size info for UI-related calculations */
data class ScreenSizeInfo(val hPX: Int, val wPX: Int, val hDP: Dp, val wDP: Dp)
@Composable expect fun getScreenSizeInfo(): ScreenSizeInfo
@OptIn(ExperimentalComposeUiApi::class)
@Composable
actual fun getScreenSizeInfo(): ScreenSizeInfo {
val density = LocalDensity.current
val config = LocalWindowInfo.current.containerSize
return remember(density, config) {
ScreenSizeInfo(
hPX = config.height,
wPX = config.width,
hDP = with(density) { config.height.toDp() },
wDP = with(density) { config.width.toDp() }
)
}
}
expect/actual
)@Composable
actual fun getScreenSizeInfo(): ScreenSizeInfo {
val density = LocalDensity.current
val config = LocalConfiguration.current
val hDp = config.screenHeightDp.dp
val wDp = config.screenWidthDp.dp
return remember(density, config) {
ScreenSizeInfo(
hPX = with(density) { hDp.roundToPx() },
wPX = with(density) { wDp.roundToPx() },
hDP = hDp,
wDP = wDp
)
}
}
Example:
The render resolution is different with pixel resolution(screen), but the specific of between iOS and android is not the same.