투케이2K

724. (Android/Java) [Android sdk 30 : Display API Deprecated] WindowMetrics 사용해 기기 화면 사이즈 확인 본문

Android

724. (Android/Java) [Android sdk 30 : Display API Deprecated] WindowMetrics 사용해 기기 화면 사이즈 확인

투케이2K 2024. 1. 17. 20:32
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

 

        try {

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

                WindowMetrics windowMetrics  = getSystemService(WindowManager.class).getMaximumWindowMetrics();

                Insets insets = windowMetrics .getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());

                float density = getResources().getDisplayMetrics().density;

                int widthPx = windowMetrics.getBounds().width() - insets.left - insets.right; // [px]
                float widthDp = widthPx/density; // [dp]

                int heightPx = windowMetrics.getBounds().height() - insets.bottom - insets.top; // [px]
                float heightDp = heightPx/density; // [dp]

                S_Log._D_("WindowMetrics", new String[]{
                        "Width (px) :: " + String.valueOf(widthPx),
                        "Width (dp) :: " + String.valueOf(widthDp),
                        "Height (px) :: " + String.valueOf(heightPx),
                        "Height (dp) :: " + String.valueOf(heightDp)
                });

            }

        }
        catch (Exception e){
            e.printStackTrace();
        }
 

[결과 출력]

 

D///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro.onCreate(A_Intro.java:465)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-01-17 16:45:38 수요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: WindowMetrics]
I/: ----------------------------------------------------
I/: [LOG :: Width (px) :: 1080]
I/: ----------------------------------------------------
I/: [LOG :: Width (dp) :: 384.0]
I/: ----------------------------------------------------
I/: [LOG :: Height (px) :: 2131]
I/: ----------------------------------------------------
I/: [LOG :: Height (dp) :: 757.6889]
D///===========//: ================================================

 

반응형
Comments