有没有办法以厘米或英寸为单位确定android物理屏幕高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2193457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:53:28  来源:igfitidea点击:

Is there a way to determine android physical screen height in cm or inches?

androidscreenresolutioninches

提问by Nathan

I need to know exactly how big the screen is on the device in real units of length so I can calculate the acceleration due to gravity in pixels per millisecond.

我需要以实际长度单位确切知道设备上的屏幕有多大,以便我可以计算重力加速度(以像素/毫秒为单位)。

Is there a method somewhere in the Android API for this?

Android API 中的某处是否有用于此的方法?

回答by Bghaak

Use the following:

使用以下内容:

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(mWidthPixels/dm.xdpi,2);
    double y = Math.pow(mHeightPixels/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("debug","Screen inches : " + screenInches);

When mWidthPixels and mHeightPixels are taken from below code

当从下面的代码中获取 mWidthPixels 和 mHeightPixels 时

private void setRealDeviceSizeInPixels()
{
    WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);


    // since SDK_INT = 1;
    mWidthPixels = displayMetrics.widthPixels;
    mHeightPixels = displayMetrics.heightPixels;

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
    {
        try
        {
            mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        }
        catch (Exception ignored)
        {
        }
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
    {
        try
        {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            mWidthPixels = realSize.x;
            mHeightPixels = realSize.y;
        }
        catch (Exception ignored)
        {
        }
    }

See this post for reference: Get screen dimensions in pixels

请参阅此帖子以供参考: 以像素为单位获取屏幕尺寸

回答by gilix

for getting the current size use Math.round at the end.

要获得当前大小,请在最后使用 Math.round。

 DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels/dm.xdpi,2);
    double y = Math.pow(dm.heightPixels/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("debug","Screen inches : " + screenInches);

screenInches=  (double)Math.round(screenInches * 10) / 10;

回答by gingerbreadboy

android developers screen info.

android 开发者屏幕信息。

use xdpi * widthPixelsand ydpi * heightPixelsmight get you what you want i think.

使用xdpi * widthPixels并可ydpi * heightPixels能让你得到你想要的我认为。

回答by Pankaj Lilan

Following code snippet will help you to get Screen Size in Inches

以下代码片段将帮助您获得以英寸为单位的屏幕尺寸

public static double getScreenSizeInches(Activity activity){
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    // since SDK_INT = 1;
    int mWidthPixels = displayMetrics.widthPixels;
    int mHeightPixels = displayMetrics.heightPixels;

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
        try{
            mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        } catch (Exception ignored) {}
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17) {
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            mWidthPixels = realSize.x;
            mHeightPixels = realSize.y;
        } catch (Exception ignored) {}
    }

    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(mWidthPixels / dm.xdpi, 2);
    double y = Math.pow(mHeightPixels / dm.ydpi, 2);
    return Math.sqrt(x + y);
}

Hope this help.

希望这有帮助。

回答by Anish

I have tried most of the ways mentioned in the other answers, but those methods fail on particular devices. So here is bit of my contribution to solving this problem. The code is written in Kotlin

我已经尝试了其他答案中提到的大多数方法,但这些方法在特定设备上失败。所以这是我对解决这个问题的一点贡献。代码是用 Kotlin 编写的

Get the DisplayMetrics object :

获取 DisplayMetrics 对象:

    val manager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val displayMetrics = DisplayMetrics()
    manager.getDefaultDisplay().getMetrics(displayMetrics)

Calculate the Screen width in inches as follows :

以英寸为单位计算屏幕宽度,如下所示:

    val widthInDotPoints : Double = (displayMetrics.widthPixels * (160 * 1.0 /displayMetrics.densityDpi))
    val widthInInches : Double = width / displayMetrics.xdpi

Here, I have calculated the width of device in terms of dot points(dp) using widthPixels and then converted it to width in terms of inches. I have used 160 .i.e DisplayMetrics.DENSITY_MEDIUM as conversion factor to convert the widthPixels to widthInDotPoints.

在这里,我使用 widthPixels 以点点(dp)为单位计算了设备的宽度,然后将其转换为以英寸为单位的宽度。我使用 160 .ie DisplayMetrics.DENSITY_MEDIUM 作为转换因子将 widthPixels 转换为 widthInDotPoints。

Similarly, calculate the Screen Height in inches :

同样,以英寸为单位计算屏幕高度:

    val heightInDotPoints : Double = (displayMetrics.heightPixels * (160 * 1.0 /displayMetrics.densityDpi))
    val heightInInches : Double = height / displayMetrics.ydpi

回答by leegor

I used this to get the real size

我用它来获得真实尺寸

final DisplayMetrics metrics = new DisplayMetrics();
final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    windowManager.getDefaultDisplay().getRealMetrics(metrics);
}
else {
    windowManager.getDefaultDisplay().getMetrics(metrics);
}
int realHeight = metrics.heightPixels; // This is real height
int realWidth = metrics.widthPixels; // This is real width

回答by CaseyB

You need to use the screen density to calculate this.

您需要使用屏幕密度来计算它。

Context.getResources().getDisplayMetrics().density

According to the documentation:

根据文档:

The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

显示的逻辑密度。这是密度独立像素单元的缩放因子,其中一个 DIP 是大约 160 dpi 屏幕(例如 240x320、1.5"x2" 屏幕)上的一个像素,提供系统显示的基线。因此,在 160dpi 屏幕上,此密度值为 1;在 120 dpi 的屏幕上,它将是 0.75;等等。

此值并不完全遵循实际屏幕大小(由 xdpi 和 ydpi 给出,而是用于根据显示 dpi 的总体变化逐步缩放整个 UI 的大小。例如,240x320 屏幕将具有即使其宽度为 1.8"、1.3" 等,密度也为 1。但是,如果屏幕分辨率增加到 320x480,但屏幕尺寸仍为 1.5"x2",那么密度将增加(可能增加到 1.5)。