How to get the Display Size in Inches in Android?

后端 未结 2 447
执念已碎
执念已碎 2021-02-06 14:57


I need for my Application the exact inch of a display. My current solution is:

double inch;
double x = Math.pow(widthPix/dm.xdpi,2);
double y = Math.p         


        
2条回答
  •  情歌与酒
    2021-02-06 15:42

    Try this.... This will give you the exact size of the display..

    static String getDisplaySize(Activity activity) {
        double x = 0, y = 0;
        int mWidthPixels, mHeightPixels;
        try {
            WindowManager windowManager = activity.getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.getMetrics(displayMetrics);
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            mWidthPixels = realSize.x;
            mHeightPixels = realSize.y;
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            x = Math.pow(mWidthPixels / dm.xdpi, 2);
            y = Math.pow(mHeightPixels / dm.ydpi, 2);
    
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return String.format(Locale.US, "%.2f", Math.sqrt(x + y));
    }
    

提交回复
热议问题