简体   繁体   English

如果Android首先在其中找到资源,Android是否更喜欢drawable文件夹?

[英]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. 我将图像放在drawable文件夹中,并将其放在drawable-xhdpidrawable-mdpidrawable-hdpi文件夹中。

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. 我知道android使用的图像与屏幕密度兼容,但是我想知道如果drawable文件夹中的图像相同,android是否将优先级赋予drawable文件夹或将采用相同的方法,那么android将做什么?不会首先检查drawable文件夹中的图像。

res/drawable/ is a synonym for res/drawable-mdpi/ , for backwards compatibility from back in the day before we had densities. res/drawable/res/drawable-mdpi/的同义词,表示从密度大的那一天开始就向后兼容。 Bitmaps put in res/drawable/ will be treated as if they were in res/drawable-mdpi/ . 放在res/drawable/位图将被视为在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 . 这就是为什么这么多的开发人员在将位图放入res/drawable/并称其为好状态时会-mdpi -Android将其视为-mdpi以获得更高的密度,通常会导致OutOfMemoryError

IMHO, it is a code smell to have bitmaps in res/drawable/ . 恕我直言,在res/drawable/有位图是一种代码味道。

It prefers the most specific. 它倾向于最具体的。 So it will use the drawable folder only if there is no override in another folder. 因此,只有在另一个文件夹中没有替代时,它才会使用drawable文件夹。 The drawable folder is a good place to put things like xml drawables which usually aren't effected much by dpi, screen size, etc. drawable文件夹是放置诸如xml drawable之类的东西的好地方,这些东西通常不受dpi,屏幕尺寸等影响很大。

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. hdpi限定符指示该目录中的资源用于具有高密度屏幕的设备。 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. 这样,您用来引用icon.png或background.png图像的资源ID始终是相同的, 但是Android通过将设备配置信息与以下内容中的限定符进行比较,来选择与当前设备最匹配的每种资源的版本资源目录名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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