Skip to content

Instantly share code, notes, and snippets.

@maxost
Created September 5, 2017 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxost/aefa9236dedce8ccb65b8e8d2f7cd518 to your computer and use it in GitHub Desktop.
Save maxost/aefa9236dedce8ccb65b8e8d2f7cd518 to your computer and use it in GitHub Desktop.
Kotlin: dp-px converters
fun Int.dp2px(context: Context): Int = (this * context.resources.displayMetrics.density).toInt()
fun Int.px2dp(context: Context): Int = (this / context.resources.displayMetrics.density).toInt()
fun Float.dp2px(context: Context): Float = this * context.resources.displayMetrics.density
fun Float.px2dp(context: Context): Float = this / context.resources.displayMetrics.density
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment