안드로이드 디스플레이 픽셀을 알아봐줄래
엔긴하여서 순정 프로그래밍을 하여야 하였는 거여요.
(google search ai) android real screen pixel
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
// Inside an Activity or a Context-aware class
public void getRealScreenDimensions() {
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
// Option 1: Using DisplayMetrics and getRealMetrics()
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
int realWidthPixels = metrics.widthPixels;
int realHeightPixels = metrics.heightPixels;
// Option 2: Using Point and getRealSize() (API Level 17 and above)
Point size = new Point();
display.getRealSize(size);
int realWidthPixelsAlt = size.x;
int realHeightPixelsAlt = size.y;
// Now, realWidthPixels and realHeightPixels (or their alternatives)
// contain the actual screen dimensions in pixels, including any
// system-reserved areas like the navigation bar.
// You can use these values for your calculations or display them.
Log.d("ScreenDimensions", "Real Width (DisplayMetrics): " + realWidthPixels + "px");
Log.d("ScreenDimensions", "Real Height (DisplayMetrics): " + realHeightPixels + "px");
Log.d("ScreenDimensions", "Real Width (Point): " + realWidthPixelsAlt + "px");
Log.d("ScreenDimensions", "Real Height (Point): " + realHeightPixelsAlt + "px");
}Explanation:
WindowManager: This service provides access to the system's window manager, which controls the display.
Display: Represents the display device on which the activity is shown.
DisplayMetrics: An object that holds information about a display, such as its size, density, and font scaling.
getRealMetrics(DisplayMetrics outMetrics): This method fills the DisplayMetrics object with the real metrics of the display, including system decorations.
getRealSize(Point outSize): (Available from API Level 17) This method fills a Point object with the real size of the display in pixels.
By using getRealMetrics() or getRealSize(), you ensure that you are retrieving the total physical pixel count of the screen, even if parts of it are used by the system UI. This is in contrast to getMetrics() or getSize(), which might return the dimensions of the application window, potentially excluding areas like the navigation bar.
(업스테이지 / 카카오톡 기본) 위에 코드를 android api level 30 대응으로 고쳐줄래?
Android API Level 30(API 30, Android 11)부터 Display 관련 메서드가 DEPRECATED되거나 변경된 사항을 반영하여 코드를 업데이트해야 합니다. 특히 getRealMetrics()와 getRealSize()는 API 30부터 보안/권한 제약이 추가되었으며, DISPLAY.GETREALSIZE()는 DEPRECATED되었습니다.
아래는 API 30+에 대응한 수정된 코드입니다:
✅ ANDROID API 30+ 대응 코드
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import androidx.annotation.RequiresApi;
public void getRealScreenDimensions() {
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
// API 30+에서는 기본 디스플레이를 직접 가져오지 않고, Context.getDisplay() 권장
Display display = getWindowManager().getCurrentWindowMetrics().getBounds().getDisplay();
if (display == null) {
Log.e("ScreenDimensions", "Display is null");
return;
}
// ✅ API 30+에서 권장되는 방식: WindowMetrics를 사용
if (Build.VERSION.SDKINT >= Build.VERSIONCODES.R) {
WindowMetrics windowMetrics = windowManager.getCurrentWindowMetrics();
// 실제 화면 크기 (시스템 UI 제외)
android.graphics.Rect bounds = windowMetrics.getBounds();
int realWidthPixels = bounds.right - bounds.left;
int realHeightPixels = bounds.bottom - bounds.top;
Log.d("ScreenDimensions", "Real Width (WindowMetrics): " + realWidthPixels + "px");
Log.d("ScreenDimensions", "Real Height (WindowMetrics): " + realHeightPixels + "px");
// ❗ 실제 물리적 화면 크기(시스템 UI 포함)를 얻으려면 다음 사용
WindowMetrics maxMetrics = windowManager.getMaximumWindowMetrics();
realWidthPixels = maxMetrics.getBounds().width();
realHeightPixels = maxMetrics.getBounds().height();
Log.d("ScreenDimensions", "Physical Width (MaxMetrics): " + realWidthPixels + "px");
Log.d("ScreenDimensions", "Physical Height (MaxMetrics): " + realHeightPixels + "px");
} else {
// API 29 이하에서는 기존 방법 사용
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
int realWidthPixels = metrics.widthPixels;
int realHeightPixels = metrics.heightPixels;
Log.d("ScreenDimensions", "Real Width (DisplayMetrics): " + realWidthPixels + "px");
Log.d("ScreenDimensions", "Real Height (DisplayMetrics): " + realHeightPixels + "px");
Point size = new Point();
display.getRealSize(size);
int realWidthPixelsAlt = size.x;
int realHeightPixelsAlt = size.y;
Log.d("ScreenDimensions", "Real Width (Point): " + realWidthPixelsAlt + "px");
Log.d("ScreenDimensions", "Real Height (Point): " + realHeightPixelsAlt + "px");
}
}🔍 주요 변경 사항
1. GETMAXIMUMWINDOWMETRICS()
• 시스템 UI(네비게이션 바, 상태 바)를 포함한 물리적 화면 크기를 제공합니다.
• WindowMetrics 객체를 반환하며, getBounds()로 실제 픽셀 값을 얻을 수 있습니다.
2. GETCURRENTWINDOWMETRICS()
• 현재 앱의 가시 영역 (시스템 UI 제외)을 반환합니다.
• bounds는 앱에서 실제로 사용 가능한 화면 크기입니다.
3. DISPLAY.GETREALSIZE() 및 GETREALMETRICS()
• API 30부터는 보안 제약으로 인해 일부 기기에서만 작동합니다.
• 대신 getMaximumWindowMetrics()를 사용하는 것이 권장됩니다.
📌 권한 요구사항 (API 30+)
• WindowMetrics를 사용하려면 특별한 권한이 필요하지 않습니다.
• 하지만 전체 화면 모드를 구현하려면 SYSTEMALERTWINDOW 권한이 필요할 수 있습니다.
✅ 추천 사용 시나리오
• 물리적 화면 크기 필요 시: getMaximumWindowMetrics()
• 앱 가시 영역 필요 시: getCurrentWindowMetrics()
이렇게 수정하면 API 30 이상에서도 안전하게 화면 크기를 조회할 수 있습니다.
위에 처럼 되었다 하더라도 공식 문서를 확인하는 편이 좋아요.
안드로이드는 가상 시스템 요소로 화면 크기가 줄어서 논리 단위를 반환하기에 주의하여야 해요.
아이 .. 포프tv 유튜버가 프로그래머는 네이밍을 주의하여야 하는 거라고 하였어요.
안가봐서 모르는 거이지만 구글은 네이밍 표준이 없는거 아니나요.
예로 Display → Screen → View 따르는 거인데 터치디스플레이 대신하여 터치스크린으로 스크린은 일반적으로 서열이 낮았쩌. 이럼 getDisplaySize, getScreenSize 나누어서 실제크기, 가상크기 이럼 선방하는 거여요.
fun WindowManager.getDisplaySize(): Rect {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
currentWindowMetrics.bounds
} else {
Point().let {
defaultDisplay.getRealSize(it)
Rect(0, 0, it.x, it.y) }
}
}