fun Int.sdp(): Dp {
val screenDp =
Resources.getSystem().displayMetrics.widthPixels / Resources.getSystem().displayMetrics.density
return (this.toFloat() / 750 * screenDp).dp
}
fun Double.sdp(): Dp {
val screenDp =
Resources.getSystem().displayMetrics.widthPixels / Resources.getSystem().displayMetrics.density
return (this / 750 * screenDp).toInt().dp
}
fun Int.ssp(): TextUnit {
val screenDp =
Resources.getSystem().displayMetrics.widthPixels / Resources.getSystem().displayMetrics.density
return (this.toFloat() / 750 * screenDp).sp
}
fun Double.ssp(): TextUnit {
val screenDp =
Resources.getSystem().displayMetrics.widthPixels / Resources.getSystem().displayMetrics.density
return (this.toFloat() / 750 * screenDp).sp
}
jetpack compose 屏幕适配
于 2021-11-23 17:44:39 首次发布
这段代码定义了四个函数,用于将尺寸或文本单位根据设备屏幕宽度进行适配。funInt.sdp()和funDouble.sdp()将像素值转换为设备独立像素(DP),而funInt.ssp()和funDouble.ssp()则将数值转换为文本大小(Scale-independent Pixel, SP)。这些函数考虑了设备的密度,确保在不同分辨率的屏幕上显示效果一致。
191





