The company needs to develop on a specific device. After getting the device, it extracts screen size parameters to draw UI diagrams. After searching some materials, it is found that the materials in this aspect are a little messy, so it is sorted out.

! [BOAEUOM8UB2)X)M0)P$8UE.png

/ / get wide
val width=resources.displayMetrics.widthPixels 
// Get height = the height of the screen area
val height=resources.displayMetrics.heightPixels
// Get screen density
val densityDpi=resources.displayMetrics.densityDpi

// Get the screen physical width and height above Android4.2 17val point = Point() display? .getRealSize(point)// Get the screen width
val height = point.x
// Total screen height = Screen display area + navigation bar
val height = point.y
Copy the code

Have pointed out that the resources on the weblog. DisplayMetrics. HeightPixels in full screen the method is unable to get the correct width is high, in the following way as a replacement

val wm = getSystemService(WINDOW_SERVICE) as WindowManager
val dm = DisplayMetrics()
wm.defaultDisplay.getMetrics(dm)
val height = dm.heightPixels
Copy the code

But I tested several full-screen phones and got consistent results

Gets the status bar height

Methods a

val resources: Resources = context.resources
val resourceId: Int = resources.getIdentifier("status_bar_height"."dimen"."android")
vak barHeight = resources.getDimensionPixelSize(resourceId)
Copy the code

Method 2 Do not use this method during initialization. You are advised to use this method in the onWindowFocusChanged method

val rectangle = Rect()
window.decorView.getWindowVisibleDisplayFrame(rectangle)
val barHeight = rectangle.top
Copy the code

Gets the navigation bar height

val resourceId: Int = resources.getIdentifier("navigation_bar_height"."dimen"."android")
val navigationBarHeight = resources.getDimensionPixelSize(resourceId)
Copy the code