Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. import android.content.res.Resources
  2.  
  3. /** Value in dp */
  4. val Int.dp: Int
  5. get() = (this / Resources.getSystem().displayMetrics.density).toInt()
  6.  
  7. /** Value in px */
  8. val Int.px: Int
  9. get() = (this * Resources.getSystem().displayMetrics.density).toInt()
  10.  
  11. /** Value in dp */
  12. val Float.dp: Float
  13. get() = (this / Resources.getSystem().displayMetrics.density)
  14.  
  15. /** Value in px */
  16. val Float.px: Float
  17. get() = (this * Resources.getSystem().displayMetrics.density)
  18.  
  19. // with these extensions, we can do:
  20. // val width = 4.dp
  21. // val height = 2.5f.dp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement