Issue
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:
A) Referenced drawables don't work if one density reference is missing (I didn't provide an explicit variant in values-ldpi/refs.xml
):
Solution
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. :)
Answered By - davidcesarino
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.