1

First, some background on what I'm doing. If you are familiar with references in general (simple concept), just skip to section 2, please.

1 The concept of density references

Usually, we use drawables in folders such as drawable-mdpi, drawable-hdpi and so on. Example:

/drawable-mdpi
    ./figure.png (24x24 pixels)
/drawable-hdpi
    ./figure.png (36x36 pixels)
[...]

However, another way to achieve (the same?) behavior is defining the same drawable, in different sizes, inside drawable-nodpi, and then referencing each size in values-mdpi, values-hdpi and so on through drawable references. Example:

/drawable-nodpi
    ./figure_24.png
    ./figure_36.png
/values-mdpi
    ./refs.xml
/values-hdpi
    ./refs.xml

Each refs.xml file should contain lines like this (example for /values-mdpi/refs.xml):

<item name="figure"
    type="drawable">@drawable/figure_24</item>

That way, you can reference the same R.drawable.figure as you would without references. I've found this to be helpful when you need to address the same image in different sizes (e.g., using the same image in 24/36/48/72 and 48/72/96/144) without resizing, because you can reference the same image twice (e.g. again, the 48 and 72px images) without replicating images (thus avoiding bloating apk size).

2 Density references don't work if one is missing

As known, Android does not need all bucket densities to display a drawable properly [1]. For example, you may omit drawable-ldpi entirely and it will load and scale those in other drawable-* folders, presenting it for you in the appropriate size in the tested density. In fact, many apps (including from Google) already omit ldpi and nobody notices.

However, I noticed that, when using drawable references (see section 1, above), this behavior changes. If I omit the reference xml file in the density folder, and try to display that drawable in that density, Android will retrieve the highest density drawable variation, but will not resize it according to that density.

Let me give you an example to make it clearer: if, using referenced drawables, I omit values-ldpi/refs.xml, Android will retrieve the drawable from values-xxhdpi (the highest I provide for this arbitrary drawable) and show this in the size corresponding to xxhdpi, and not ldpi, as it should. Therefore, any layouts displayed in a ldpi device will have huge images. See images below:

A) Normal drawables work fine even if LDPI drawable is missing:

enter image description here enter image description here enter image description here

A) Referenced drawables don't work if one density reference is missing (I didn't provide an explicit variant in values-ldpi/refs.xml):

enter image description here enter image description here enter image description here

davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • How are you using the resources? `android:src` references in a layout? `android:background` references in a layout? Via something or another in Java code? Something else? – CommonsWare Sep 01 '14 at 22:18
  • Hi Mark, these examples are through src tags in ImageViews. – davidcesarino Sep 01 '14 at 22:20
  • 1
    Looking at the issue tracker, according to Google, [this should not work at all](https://code.google.com/p/android/issues/detail?id=74825). I can't explain why you are seeing it work in some cases but not others. – CommonsWare Sep 01 '14 at 22:30
  • @CommonsWare Nevermind, it works in devices/emulator. It only happens in design time, i.e., when previewing layouts in Studio. And by the way, I don't think the person explained the issue correctly (or it's another issue altogether). As we can see, he mentions aliasing the drawable folders themselves (maybe some kind of soft linking the files) as he never mentions using `values` references. IMHO it wasn't entirely clear, hence why that question was first tagged with "NeedsInfo". Thanks for your help. – davidcesarino Sep 01 '14 at 22:58

1 Answers1

1

Nevermind, I tested in an ldpi emulator right now and it works. So it's one of those bugs that only affect the layout preview pane in the IDE.

Yes, I should have tested in an ldpi emulator before asking. :)

davidcesarino
  • 16,160
  • 16
  • 68
  • 109