Browse Source

Add DensityUtil

cooperku_kdanmobile 5 năm trước cách đây
mục cha
commit
fd892c368e

+ 38 - 0
reader/src/main/java/com/kdanmobile/reader/utils/DensityUtil.kt

@@ -0,0 +1,38 @@
+package com.kdanmobile.reader.utils
+
+import android.content.Context
+
+
+class DensityUtil private constructor() {
+
+    init {
+        throw UnsupportedOperationException("cannot be instantiated")
+    }
+
+    companion object {
+
+        fun getScreenWidthPx(context: Context): Int {
+            return context.resources.displayMetrics.widthPixels
+        }
+
+        fun getScreenHeightPx(context: Context): Int {
+            return context.resources.displayMetrics.heightPixels
+        }
+
+        fun dp2px(context: Context, dpVal: Float): Int {
+            return (dpVal * context.resources.displayMetrics.density + 0.5f).toInt()
+        }
+
+        fun sp2px(context: Context, spVal: Float): Int {
+            return (spVal * context.resources.displayMetrics.scaledDensity + 0.5f).toInt()
+        }
+
+        fun px2dp(context: Context, pxVal: Float): Float {
+            return pxVal / context.resources.displayMetrics.density
+        }
+
+        fun px2sp(context: Context, pxVal: Float): Float {
+            return pxVal / context.resources.displayMetrics.scaledDensity
+        }
+    }
+}