Android

Android 取得手機螢幕大小

解析度&密度

val displayMetrics = DisplayMetrics()
display?.getRealMetrics(displayMetrics)

val heightPixels = displayMetrics.heightPixels //手機高度
val widthPixels = displayMetrics.widthPixels //手機寬度
val density = displayMetrics.density  // 螢幕密度
val densityDpi = displayMetrics.densityDpi // 螢幕密度DPI
val height = displayMetrics.heightPixels / displayMetrics.density //手機真實高度
val weight = displayMetrics.widthPixels / displayMetrics.density //手機真實寬度

狀態列高度

val statusBar = resources.getIdentifier("status_bar_height", "dimen", "android")
val height = resources.getDimensionPixelSize(statusBar)

導航列高度

val navigationBar = resources.getIdentifier("navigation_bar_height", "dimen", "android")
val height = resources.getDimensionPixelSize(navigationBar)

APP內容區域

val rect = Rect()
window.decorView.getWindowVisibleDisplayFrame(rect)
val height = rect.height()
val weight = rect.width()

發表迴響