计算安卓的屏幕尺寸?[英] Calculating android screen size?

本文是小编为大家收集整理的关于计算安卓的屏幕尺寸?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否可以以英寸计算Android屏幕对角线尺寸.

我已经尝试过两种方法,两者都不适合我的情况.

方法1:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int absoluteHeightInPx = displayMetrics.heightPixels;
int absoluteWidthInPx = displayMetrics.widthPixels;     
double diagonalPixels = Math.sqrt((absoluteHeightInPx * absoluteHeightInPx) + (absoluteWidthInPx * absoluteWidthInPx));
double diagonalInInches = diagonalPixels / displayMetrics.densityDpi;

方法2:

float actualHeight = absoluteHeightInPx * deviceDensity;
float actualWidth = absoluteWidthInPx * deviceDensity;
float deviceDensity = displayMetrics.density;
float physicalPixelPerInchX = displayMetrics.xdpi;
float physicalPixelPerInchY = displayMetrics.ydpi;
float heightInInches = actualHeight / physicalPixelPerInchY;
float widhtInInches = actualWidth / physicalPixelPerInchX;
double diagonalInInches = Math.sqrt((heightInInches * heightInInches) + (widhtInInches * widhtInInches));

如果我错了,你能纠正.

谢谢, easwar

推荐答案

我在8个Android设备中测试了我的方法.第二个几乎准确.

但对于少数设备来说,这不是给出适当的答案.所以我在第二种方法之后得出结论.

其他推荐答案

我使用下面的代码,它适用于我的moto dey.

 DisplayMetrics metrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    float height=metrics.heightPixels/metrics.xdpi;
    float width=metrics.widthPixels/metrics.ydpi;

    TextView tv=(TextView) findViewById(R.id.textview);
    tv.setText("The screen size is:"+FloatMath.sqrt(height*height+width*width));

真实的对角线长度为3.7",代码给出3.6979072,足够精确.根据SDK的描述,我认为我的方法应该在其他设备上工作.

本文地址:https://www.itbaoku.cn/post/102314.html

问题描述

Is it possible to calculate the android screen diagonal dimension in inches.

I have tried below two approaches both is not suitable for my case.

Approach 1:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int absoluteHeightInPx = displayMetrics.heightPixels;
int absoluteWidthInPx = displayMetrics.widthPixels;     
double diagonalPixels = Math.sqrt((absoluteHeightInPx * absoluteHeightInPx) + (absoluteWidthInPx * absoluteWidthInPx));
double diagonalInInches = diagonalPixels / displayMetrics.densityDpi;

Approach 2:

float actualHeight = absoluteHeightInPx * deviceDensity;
float actualWidth = absoluteWidthInPx * deviceDensity;
float deviceDensity = displayMetrics.density;
float physicalPixelPerInchX = displayMetrics.xdpi;
float physicalPixelPerInchY = displayMetrics.ydpi;
float heightInInches = actualHeight / physicalPixelPerInchY;
float widhtInInches = actualWidth / physicalPixelPerInchX;
double diagonalInInches = Math.sqrt((heightInInches * heightInInches) + (widhtInInches * widhtInInches));

Could you correct if i am wrong.

Thanks, Easwar

推荐答案

I have tested both of my approach in 8 android devices. The second is almost accurate.

But for few devices this is not giving proper answer. So I concluded following the second approach.

其他推荐答案

I use the code below, and it works on my Moto Dey.

 DisplayMetrics metrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    float height=metrics.heightPixels/metrics.xdpi;
    float width=metrics.widthPixels/metrics.ydpi;

    TextView tv=(TextView) findViewById(R.id.textview);
    tv.setText("The screen size is:"+FloatMath.sqrt(height*height+width*width));

The real diagonal length is 3.7" and the code gives out 3.6979072, precise enough. According to the descriptions in SDK, I think my method should work on other devices.