当前位置:网站首页>Android kotlin SP DP to PX

Android kotlin SP DP to PX

2022-06-22 21:35:00 Too handsome to go out

Android kotlin sp dp turn px
Code

/** *  According to the resolution of the phone sp  Turn into px( Pixels ) */
inline val Double.sp: Int get() = run {
    
    toFloat().sp
}

inline val Int.sp: Int get() = run {
    
    toFloat().sp
}

inline val Float.sp: Int get() = run {
    
    val scale: Float = Resources.getSystem().displayMetrics.scaledDensity
    return (this * scale + 0.5f).toInt()
}

/** *  According to the resolution of the phone dp  Turn into px( Pixels ) */
// Examples of use  10.dp
inline val Double.dp: Int get() = run {
    
    return toFloat().dp
}

inline val Int.dp: Int get() = run {
    
    return toFloat().dp
}

inline val Float.dp: Int get() = run {
    
    val scale: Float = Resources.getSystem().displayMetrics.density
    return (this * scale + 0.5f).toInt()
}
原网站

版权声明
本文为[Too handsome to go out]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222005279962.html