SHARE
TWEET

Untitled

a guest Oct 13th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top