ANDROID: How to identify user screen density?

Asked

Viewed 958 times

0

I want the application to just download the images that are adaptable to the user screen, such as: if the user device supports xxhdpi images, it will not be necessary to store the images of xxxhdpi, mdpi, hdpi, and so on...

3 answers

5


The qualifiers cited are densities, where mdpi is the baseline. And the rest 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

Programmatically it is possible to identify which 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 download approach would be to combine the density to mount or select an image url (from a collection of urls) for each density and search as the result of the device.

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

But density is a cool metric for filtering images. A more real metric, but much more complicated, would be to combine the screen demensions with the density.

  • Thank you so much @Wakim!!

1

It is possible to obtain the logical density through the code

 getResources().getDisplayMetrics().density;

He will return you:

0.75 - ldpi (low resolution)

1.0 - mdpi (average resolution)

1.5 - hdpi (high resolution)

2.0 - xhdpi (great resolution)

3.0 - xxhdpi (+optimal resolution)

4.0 - xxxhdpi (excellent)

enter image description here

reference: Density

0

You can find out what the density category is (Density Bucket) to which the device belongs through of this question or by the @Wakim response, then pass this information as parameter for the request to the webservice that will bring the appropriate image (also informed as parameter).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.