|
@@ -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
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|