简体   繁体   中英

Does Android prefer the drawable folder if it first finds a resource there?

I have put an image in drawable folder and in drawable-xhdpi , drawable-mdpi , drawable-hdpi folders.

I know that android uses the image that is compatible with the screen density, but i want to know what android will do if the same image is in drawable folder, does android give priority to drawable folder, or it will follow the same approach, and does not check drawable folder first for image.

res/drawable/ is a synonym for res/drawable-mdpi/ , for backwards compatibility from back in the day before we had densities. Bitmaps put in res/drawable/ will be treated as if they were in res/drawable-mdpi/ .

This is why so many developers get screwed over when they put bitmaps in res/drawable/ and call it good — Android treats it as -mdpi and upscales for higher densities, often resulting in OutOfMemoryError .

IMHO, it is a code smell to have bitmaps in res/drawable/ .

It prefers the most specific. So it will use the drawable folder only if there is no override in another folder. The drawable folder is a good place to put things like xml drawables which usually aren't effected much by dpi, screen size, etc.

You can check the official doc .

For example, here are some default and alternative resources:

res/
    drawable/ 
        icon.png
        background.png
    drawable-hdpi/ 
        icon.png
        background.png

The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen. The images in each of these drawable directories are sized for a specific screen density, but the filenames are exactly the same. This way, the resource ID that you use to reference the icon.png or background.png image is always the same, but Android selects the version of each resource that best matches the current device, by comparing the device configuration information with the qualifiers in the resource directory name.

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