Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import android.app.Activity
  2. import android.content.Context
  3. import android.util.TypedValue
  4. import android.view.ViewGroup
  5. import android.widget.FrameLayout
  6. import android.widget.LinearLayout
  7. import android.widget.RelativeLayout
  8. import android.util.DisplayMetrics
  9.  
  10. object Dimension {
  11. enum class Type(val value: Int) {
  12. FILL(value = ViewGroup.LayoutParams.MATCH_PARENT),
  13. WRAP(value = ViewGroup.LayoutParams.WRAP_CONTENT),
  14. NONE(0)
  15. }
  16.  
  17.  
  18. enum class Unit(val value: Int) {
  19. DP(value = TypedValue.COMPLEX_UNIT_DIP),
  20. SP(value = TypedValue.COMPLEX_UNIT_SP)
  21. }
  22.  
  23.  
  24. fun getSystemSize(context: Context, size: Float, unit: Unit) =
  25. TypedValue.applyDimension(
  26. unit.value,
  27. size,
  28. context.resources.displayMetrics
  29. )
  30.  
  31. fun getViewParams(
  32. width: Type = Type.NONE,
  33. height: Type = Type.NONE,
  34. numericWidth: Int = 0,
  35. numericHeight: Int = 0
  36. ): ViewGroup.LayoutParams =
  37. ViewGroup.LayoutParams(
  38. if (width != Type.NONE) width.value else numericWidth,
  39. if (height != Type.NONE) height.value else numericHeight
  40. )
  41.  
  42. fun getRLayoutParams(
  43. width: Type = Type.NONE,
  44. height: Type = Type.NONE,
  45. numericWidth: Int = 0,
  46. numericHeight: Int = 0
  47. ): RelativeLayout.LayoutParams =
  48. RelativeLayout.LayoutParams(
  49. if (width != Type.NONE) width.value else numericWidth,
  50. if (height != Type.NONE) height.value else numericHeight
  51. )
  52.  
  53.  
  54. fun getLLayoutParams(
  55. width: Type = Type.NONE,
  56. height: Type = Type.NONE,
  57. numericWidth: Int = 0,
  58. numericHeight: Int = 0,
  59. weight: Float = 0f
  60. ): LinearLayout.LayoutParams =
  61. LinearLayout.LayoutParams(
  62. if (width != Type.NONE) width.value else numericWidth,
  63. if (height != Type.NONE) height.value else numericHeight,
  64. weight
  65. )
  66.  
  67. fun getFrameLayoutParams(
  68. width: Type = Type.NONE,
  69. height: Type = Type.NONE,
  70. numericWidth: Int = 0,
  71. numericHeight: Int = 0,
  72. gravity: Int = 0
  73. ): FrameLayout.LayoutParams =
  74. FrameLayout.LayoutParams(
  75. if (width != Type.NONE) width.value else numericWidth,
  76. if (height != Type.NONE) height.value else numericHeight,
  77. gravity
  78. )
  79.  
  80. fun getScreenSize(activity: Activity): Pair<Int, Int> {
  81. val displayMetrics = DisplayMetrics()
  82. activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
  83. val height = displayMetrics.heightPixels
  84. val width = displayMetrics.widthPixels
  85. return Pair(height, width)
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement