How to get a DPI screen

I am trying to get an image on the screen, but so far it is not working. I tried:

DisplayMetrics dm = new DisplayMetrics(); mainContext.getWindowManager().getDefaultDisplay().getMetrics(dm); SCREEN_DPI = dm.densityDpi; 

But on both the Samsung Galaxy Tab 10.1 and the Samsung Galaxy S I9000, SCREEN_DPI is 160. I also tried SCREEN_DPI = dm.density , but I got a value of 1.0 for both cases.

Any help would be greatly appreciated.

+4
source share
3 answers

According to @CommonsWare: "Samsung can categorize them (devices) so that they are in any density bucket that they want." Thus, in this case, both devices are in the same density bucket.

0
source

Duplicate of this question

Although Android does not use direct pixel matching, it uses a few quantized density values ​​that are independent of density, then scales them to the actual screen size. Thus, the density property will be one of those constants (120, 160 or 240 dpi).

If you need the actual density (perhaps for an OpenGL application), you can get it from the xdpi and ydpi properties for horizontal and vertical, respectively.

+2
source

Try getRealMetrics() instead:

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

This API was officially added to API 17, but in my limited testing it worked correctly even on 4.0 devices, so before that it could be a hidden API.

The official documentation is here , though there seem to be more reasons why the other API is not doing what you expect than text to be meant.

+1
source

Source: https://habr.com/ru/post/1381946/


All Articles