기존에 사용하던 코드이지만 SDK 30 이상부터는 Deprecation 되어 대응하는 코드가 필요했다.

val displaySize = Point()
windowManager.defaultDisplay.getSize(displaySize)

 

 

대응코드

fun WindowManager.currentWindowMetricsPointCompat(): Point {
    return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
        val windowInsets = currentWindowMetrics.windowInsets
        var insets: Insets = windowInsets.getInsets(WindowInsets.Type.navigationBars())
        windowInsets.displayCutout?.run {
            insets = Insets.max(insets, Insets.of(safeInsetLeft, safeInsetTop, safeInsetRight, safeInsetBottom))
        }
        val insetsWidth = insets.right + insets.left
        val insetsHeight = insets.top + insets.bottom
        Point(currentWindowMetrics.bounds.width() - insetsWidth, currentWindowMetrics.bounds.height() - insetsHeight)
    } else {
        Point().apply {
            @Suppress("DEPRECATION")
            defaultDisplay.getSize(this)
        }
    }
}

 

사용방법

val displaySize = windowManager.currentWindowMetricsPointCompat()

+ Recent posts