Last active
August 30, 2021 02:45
-
-
Save qwertyfinger/66666c8fa0e697cf6ed0981e5f7a4d83 to your computer and use it in GitHub Desktop.
dp to px to sp extension functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dimension(unit = Dimension.DP) | |
fun Number.pxToDp(): Float { | |
return toFloat() / Resources.getSystem().displayMetrics.density | |
} | |
@Dimension(unit = Dimension.SP) | |
fun Number.pxToSp(): Float { | |
return toFloat() / Resources.getSystem().displayMetrics.scaledDensity | |
} | |
@Dimension(unit = Dimension.PX) | |
fun Number.dpToPx(): Float { | |
return toFloat() * Resources.getSystem().displayMetrics.density | |
} | |
@Dimension(unit = Dimension.SP) | |
fun Number.dpToSp(): Float { | |
return dpToPx().pxToSp() | |
} | |
@Dimension(unit = Dimension.PX) | |
fun Number.spToPx(): Float { | |
return toFloat() * Resources.getSystem().displayMetrics.scaledDensity | |
} | |
@Dimension(unit = Dimension.DP) | |
fun Number.spToDp(): Float { | |
return spToPx().pxToDp() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment