What Is The Difference Between -anydpi And -nodpi?

AndroidAndroid Resources

Android Problem Overview


If you use the Vector Asset wizard in Android Studio 1.5.0, any vector drawable XML you import using that wizard goes into res/drawable/.

However, the build/ directory, and the resulting APK show that those XML files get moved into a res/drawable-anydpi-v21/ resource directory. The -v21 part makes sense, as VectorDrawable is only supported on API Level 21+. However, -anydpi seems to be undocumented. I would have expected -nodpi, both for the original import destination and for where the build system elects to move it.

Has anyone seen official statements for what -anydpi means, and what its relationship is with -nodpi? I am looking for practical effects, not merely what some code comments hint at.

Android Solutions


Solution 1 - Android

nodpi

> These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.

For instance:

  • drawable-nodpi/dot.png

The dot will appear small on xxhdpi, big on ldpi.

However, the resource resolver will match a specific qualifier if it exists.

For instance

  • drawable-hdpi/eg.png
  • drawable-nodpi-v21/eg.xml

On a Lollipop (API 21) hdpi device, the bitmap is used.

On a Lollipop (API 21) xhdpi device, the vector is used.

anydpi

> These resources take precedence in any dpi.

For instance

  • drawable-hdpi/eg.png
  • drawable-anydpi-v21/eg.xml

On a Lollipop (API 21) hdpi device, the vector is used.

On a Lollipop (API 21) xhdpi device, the vector is used.

Reference

Note: anydpi was added in change Ic3288d0236fe0bff20bb1599aba2582c25b0db32.

Solution 2 - Android

I use drawable-nodpi for everything, including plenty of large graphics for my game. An undocumented consequence of scaling up your graphics with -anydpi is that it increases memory use exponentially. So if you have a 1MB graphic in drawable, it will get scaled to 4MB, 16MB, 64MB, or more depending on the resolution of the user device. And device resolutions keep going up. That scaling up doesn't actually increase the sharpness of the graphic, of course. The drawing actions can direct how large each graphic should be in relation to screen size anyway, no need to bloat the app. Nor with multiple resolution specific draw folders.

Solution 3 - Android

The source code contains the following comments (line 639):

/**
 * Value for {@link #densityDpi} for resources that scale to any density (vector drawables).
 * {@hide}
 */
public static final int DENSITY_DPI_ANY = 0xfffe;

/**
 * Value for {@link #densityDpi} for resources that are not meant to be scaled.
 * {@hide}
 */
public static final int DENSITY_DPI_NONE = 0xffff;

Hope this clears out the confusion.

Solution 4 - Android

nodpi: Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.

anydpi: This qualifier matches all screen densities and takes precedence over other qualifiers. This is useful for vector drawables. Added in API Level 21.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCommonsWareView Question on Stackoverflow
Solution 1 - AndroidrdsView Answer on Stackoverflow
Solution 2 - AndroidAndroidcoderView Answer on Stackoverflow
Solution 3 - AndroidVishavjeet SinghView Answer on Stackoverflow
Solution 4 - AndroiddzikovskyyView Answer on Stackoverflow