Skip to content

Instantly share code, notes, and snippets.

@indrih17
Created August 3, 2022 18:49
Show Gist options
  • Save indrih17/d173645d5761be4208373953f68fdf30 to your computer and use it in GitHub Desktop.
Save indrih17/d173645d5761be4208373953f68fdf30 to your computer and use it in GitHub Desktop.
/** Перевод из Px в Dp. */
fun Int.pxToDp(displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics): Int =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, toFloat(), displayMetrics).roundToInt()
/** Перевод из Dp в Px. */
fun Int.dpToPx(displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics): Int =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, toFloat(), displayMetrics).roundToInt()
/** Перевод из Sp в Px. */
fun Int.spToPx(displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics): Int =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, toFloat(), displayMetrics).roundToInt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment