안드로이드 개발

Android에서 화면 크기를 픽셀로 얻는 방법

피커 2023. 5. 12. 21:39
728x90
반응형

안드로이드 앱을 개발하다보면 스마트폰의 각각 다른 화면 크기로 인해 어려움을 겪고는 합니다.

이럴때는 스마트폰의 화면 크기 값을 불러와서 거기에 맞추어 코딩을 하면 됩니다.

이때 꼭 필요한것이 현재 화면의 크기를 가져오는것입니다.

 

아주 간단히 아래처럼 코드를 입력하면 해결이 됩니다.

1. 화면 크기 가져오는 코드

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.d(TAG, "Display size, width = " + width + " height = " + height);

 

2. 앱 실행 결과

 

 

3. 관련 백서

Provides information about the size and density of a logical display.

The display area is described in two different ways.

  • The application display area specifies the part of the display that may contain an application window, excluding the system decorations. The application display area may be smaller than the real display area because the system subtracts the space needed for decor elements such as the status bar. Use WindowMetrics#getBounds() to query the application window bounds.
  • The real display area specifies the part of the display that is accessible to an application in the current system state. The real display area may be smaller than the physical size of the display in a few scenarios. Use WindowManager#getCurrentWindowMetrics() to identify the current size of the activity window. UI-related work, such as choosing UI layouts, should rely upon WindowMetrics#getBounds(). See getRealSize(Point) / getRealMetrics(DisplayMetrics) for details.

A logical display does not necessarily represent a particular physical display device such as the internal display or an external display. The contents of a logical display may be presented on one or more physical displays according to the devices that are currently attached and whether mirroring has been enabled.

반응형