简体   繁体   中英

Trick to supply resources to different drawable folders

Everytime, when I supply resource to drawable folder, here is what I did

drawable-xhdpi (96x96 px)
drawable-hdpi  (72x72 px)
drawable-mdpi  (48x48 px)
drawable-ldpi  (36x36 px)

Most of the time, I merely use GIMP to perform dummy scale down from the largest size image from drawable-xhdpi . I do not perform any further pixel editing on the scaled down image.

Recently, I realize, if I only supply 1 highest resolution image, Android system will internally perform image size scale down.

drawable-xhdpi (96x96 px)
drawable-hdpi  (empty)
drawable-mdpi  (empty)
drawable-ldpi  (empty)

I tested on 2 devices. It works for me. I was wondering is this a good technique to avoid cumbersome work to supply so many different size images? Any side effect on this technique?

I was wondering is this a good technique to avoid cumbersome work to supply so many different size images? Any side effect on this technique?

I imagine there are (at least) two:

  1. This may not always work with 9-patches, especially if the stretchable area is defined by one or more single pixels. For example, if you would provide such a 9patch as xhdpi drawable, then on an mdpi device that single pixel will effectively be 'halved' and thus either disappear completely or blended with the surrounding transparent pixels. The latter is generally true for any upscale/downscale operation, so it's quite likely that your 9patch will not appear as intended.

  2. Larger images will simply take up more space in memory. Especially on low(er)-end devices, with a limited amount of internal memory and a reasonably strict heap space limit, you're likely to run out of memory fast when loading images way larger than required for displaying.


Regarding your comment: it's fairly easy to come up with a sample 9patch that will not visually look as intended across all different screen densities if you supply it only as xhdpi resource. Consider the following 9patch:

在此处输入图片说明

Enlarged snapshot for the sake of visibility:

在此处输入图片说明

Obviously, the idea is that when this 9patch gets stretched, the result is a 1-pixel thick horizontal blue line. Now, drop this 9patch in just the xhdpi folder and compare the results on xhdpi vs. mdpi:

在此处输入图片说明

Clearly, the mdpi device has scaled down the 9patch from the xhdpi folder and the result doesn't look like intended.

Anyways, my point is that in a lot of cases not supplying 9patches for every density bucket may end up looking fine. Just be aware of the fact that there are definitely scenarios out there for which it may not give the desired result. Also, take into consideration the memory argument.

The technique you are using will work on fewer number of devices.. Because in market there are a lot of phones with different resolutions and different sizes. In some low resolution devices whose screen size in smaller,the UI will not look proper... Now take an example that you are using a background image for splash screen whose resolution is 720*1280(xhdpi) and you have put it in Xhdpi drawable folder than when you view this drawable in low end device like Samsung euopa or HTC wildfire the image would look squeezed type.

there would be a lot other situation where you would be required to use different set of images.... If the technique mentioned by you would work in all phones then why would had Android developed different folder for different set of images.... :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM