How to get the Display Size in Inches in Android?

后端 未结 2 446
执念已碎
执念已碎 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:40

    The following gave me a a result quite close to the specs:

        DisplayMetrics dm = getResources().getDisplayMetrics();
    
        double density = dm.density * 160;
        double x = Math.pow(dm.widthPixels / density, 2);
        double y = Math.pow(dm.heightPixels / density, 2);
        double screenInches = Math.sqrt(x + y);
        log.info("inches: {}", screenInches);
    

    Output: inches: 4.589389937671455
    Specs: Samsung Galaxy Nexus (720 x 1280 px, ~320 dpi, 4.65")

    Please note, that dm.heightPixels (or dm.widthPixels depending on your orientatation) does not necessarily provide accurate information, since soft keys (as used in the Galaxy Nexus) are not added to the height (or width).

提交回复
热议问题