Skip to content

Commit 11f6edf

Browse files
fix(*): 解决屏幕密度获取字段错误的问题
1 parent bf3bdae commit 11f6edf

File tree

1 file changed

+7
-7
lines changed
  • lite/src/main/java/com/pengxh/kt/lite/extensions

1 file changed

+7
-7
lines changed

lite/src/main/java/com/pengxh/kt/lite/extensions/Context.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,28 +218,28 @@ fun Context.getStatusBarHeight(): Int {
218218
*/
219219
@Deprecated("", ReplaceWith("getScreenDensity()"))
220220
fun Context.obtainScreenDensity(): Float {
221-
val manager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
221+
val windowManager = this.getSystemService<WindowManager>()!!
222222
val dm = DisplayMetrics()
223-
manager.defaultDisplay.getMetrics(dm)
223+
windowManager.defaultDisplay.getMetrics(dm)
224224
return dm.density
225225
}
226226

227-
fun Context.getScreenDensity(): Int {
228-
val windowManager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
227+
fun Context.getScreenDensity(): Float {
228+
val windowManager = this.getSystemService<WindowManager>()!!
229229
val displayMetrics = DisplayMetrics()
230230
val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
231231
this.display
232232
} else {
233233
windowManager.defaultDisplay
234234
}
235235
if (display == null) {
236-
return 0
236+
return 0f
237237
}
238238
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
239-
this.resources.configuration.densityDpi
239+
this.resources.displayMetrics.density
240240
} else {
241241
display.getMetrics(displayMetrics)
242-
displayMetrics.density.toInt()
242+
displayMetrics.density
243243
}
244244
}
245245

0 commit comments

Comments
 (0)