투케이2K

456. (kotlin/코틀린) [Android sdk 30 : Display API Deprecated] WindowMetrics 사용해 기기 화면 사이즈 확인 본문

Kotlin

456. (kotlin/코틀린) [Android sdk 30 : Display API Deprecated] WindowMetrics 사용해 기기 화면 사이즈 확인

투케이2K 2024. 1. 29. 19:30

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Kotlin

 

[소스 코드]

        // ----------------------------------------------------------------------------------
        // TODO [SEARCH FAST] : [RETURN] getMobileSize : 모바일 화면 크기 구하기 DP : W / S / M / L / LX / NO
        // ----------------------------------------------------------------------------------
        fun getMobileSize(mContext: Context) : String {

            /**
             * // -----------------------------------------
             * [getMobileSize 메소드 설명]
             * // -----------------------------------------
             * 1. 모바일 화면 크기 구하기 DP : W / S / M / L / LX / NO
             * // -----------------------------------------
             * 2. 호출 방법 : C_App.getMobileSize(this@A_Intro)
             * // -----------------------------------------
             * 3. 리턴 데이터 : M
             * // -----------------------------------------
             * */


            // [리턴 변수 선언]
            var returnData = ""
            var dpWidthValues = ""
            var dpHeightValues = ""


            // [로직 처리 실시]
            try {

                var dpWidthValue = 0
                var dpHeightValue = 0

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

                    val windowMetrics = mContext.getSystemService(WindowManager::class.java).maximumWindowMetrics

                    val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())

                    val density = mContext.resources.displayMetrics.density

                    val widthPx = windowMetrics.bounds.width() - insets.left - insets.right // [px]
                    dpWidthValue = (widthPx / density).toInt() // [dp]

                    val heightPx = windowMetrics.bounds.height() - insets.bottom - insets.top // [px]
                    dpHeightValue = (heightPx / density).toInt() // [dp]

                }
                else {

                    val display = (mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
                    val outMetrics = DisplayMetrics()
                    display.getMetrics(outMetrics)
                    val density = mContext.resources.displayMetrics.density
                    val dpHeight = outMetrics.heightPixels / density
                    val dpWidth = outMetrics.widthPixels / density

                    dpWidthValue = dpWidth.toInt()
                    dpHeightValue = dpHeight.toInt()

                }


                /**
                 * // -----------------------------------------
                 *    [장치]   [가로]   [세로]
                 * // -----------------------------------------
                 * 1) Watch =  250dp    250dp
                 * // -----------------------------------------
                 * 2) mobile = 320dp    569dp (s6)
                 * // -----------------------------------------
                 * 3) mobile = 400dp    730dp (note 5)
                 * // -----------------------------------------
                 * 4) mobile = 400dp    810dp (note 10 / LG Q9)
                 * // -----------------------------------------
                 * 5) mobile = 800dp    1280dp (Tab)
                 * // -----------------------------------------
                 * 6) Tablet = 960dp    600dp (Tab)
                 *             1280dp   800dp
                 * // -----------------------------------------
                 */


                if (dpHeightValue < 320) { //0 ~ 319
                    returnData = "W"
                } else if (dpHeightValue < 660) { //320 ~ 659
                    returnData = "S"
                } else if (dpHeightValue < 750) { //660 ~ 749
                    returnData = "M"
                } else if (dpHeightValue < 900) { //750 ~ 899
                    returnData = "L"
                } else if (dpHeightValue < 1600) { //900 ~ 1599
                    returnData = "XL"
                } else {
                    returnData = "NO"
                }

                dpHeightValues = dpHeightValue.toString()
                dpWidthValues = dpWidthValue.toString()

            }
            catch (e: Exception) {
                S_Log._printStackTrace_(mContext, S_FinalMsg.LOG_BUG_STATE, null, e)
            }


            // [로그 출력 실시]
            //*
            // ===============================================================
            S_Log._D_("모바일 화면 크기 구하기 DP", arrayOf(
                "모델 :: " + Build.MODEL.toString(),
                "제조사 :: " + Build.MANUFACTURER.toString(),
                "가로 (DP) :: $dpWidthValues",
                "세로 (DP) :: $dpHeightValues",
                "RETURN :: $returnData"
            ))
            // ===============================================================
            // */


            // [리턴 반환 실시]
            return returnData

        }
 

[결과 출력]

 

 

반응형
Comments