其他分享
首页 > 其他分享> > android-为什么总是使屏幕英寸尺寸错误?

android-为什么总是使屏幕英寸尺寸错误?

作者:互联网

我想按英寸计算设备尺寸.我正在使用此代码,每次搜索都会出现.但是问题是当我将4.5英寸的设备放在android studio中时,我的答案是4.我尝试了5.2英寸的设备,然后又得到了4.3英寸和10.1英寸->答案是9.0.那么如何获得准确的答案?

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

    double wi=(double)dm.widthPixels/dm.xdpi;
    double hi=(double)dm.heightPixels/dm.ydpi;
    double x = Math.pow(wi,2);
    double y = Math.pow(hi,2);
    double screenInches = Math.sqrt(x+y);
    Log.e("hello"," "+screenInches);

解决方法:

您的代码存在的问题是它没有考虑在内

动作条

软键

屏幕方向

使用以下答案

 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)
    {
    }


 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);

归功于him

标签:screen-size,android-screen,android
来源: https://codeday.me/bug/20191111/2020378.html