ANDROID: How to identify the density of the user's screen?

0

I want the application to only download images that are adaptable to the user's screen, for example: if the user's device supports xxhdpi images, it is not necessary to store the xxxhdpi, mdpi, hdpi, and There you go ...

    
asked by anonymous 26.08.2015 / 03:52

3 answers

5

The qualifiers cited are densities, where mdpi is the baseline . And the others are proportional to mdpi .

That is:

  • ldpi = 0.75 * mdpi
  • hdpi = 1.5 * mdpi
  • xhdpi = 2.0 * mdpi
  • xxhdpi = 3.0 * mdpi
  • xxxhdpi = 4.0 * mdpi

You can programmatically identify the screen density of the device, just use:

DisplayMetrics metrics = new DisplayMetrics();
Activity activity = <Sua Activity>;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

int density = metrics.densityDpi; // Densidade (ldpi, mdpi e etc...)

The result of density is one of the values below:

DisplayMetrics.DENSITY_LOW (ldpi)
DisplayMetrics.DENSITY_MEDIUM (mdpi)
DisplayMetrics.DENSITY_HIGH (hdpi)
DisplayMetrics.DENSITY_XHIGH (xhdpi)
DisplayMetrics.DENSITY_XXHIGH (xxhpi)
DisplayMetrics.DENSITY_XXXHIGH (xxxhdpi)

A simple approach to downloading would be to combine the density to mount or select a url (from a collection of urls) image for each density and search for the result of the device.

Obs : Remembering that density is not equal to screen size. In theory, we can have devices with density of 640dpi (xxxhdpi) with resolution of 320x240 and another device with 120dpi and 1920x1080 resolution.

But density is a legal metric for filtering images. A more realistic but much more complicated metric would be to combine screen dementia with density.

    
26.08.2015 / 04:10
1

It is possible to obtain the logical density through the code

 getResources().getDisplayMetrics().density;

It will return you:

0.75 - ldpi (low resolution)

1.0 - mdpi (medium resolution)

1.5 - hdpi (high resolution)

2.0 - xhdpi (great resolution)

3.0 - xxhdpi (+ optimal resolution)

4.0 - xxxhdpi (excellent)

reference: density

    
26.08.2015 / 04:17
0

You can find out what the density bucket category the device belongs to of this question or by @Wakim's response, then pass that information as a parameter to the request to the webservice that will bring the image appropriate (also informed as a parameter).

    
26.08.2015 / 04:11