较新版本的 Android Studio 和只有两个可绘制目录 - drawable 和 drawable-v21 [英] Newer versions of Android Studio and only two drawable directory - drawable and drawable-v21

查看:20
本文介绍了较新版本的 Android Studio 和只有两个可绘制目录 - drawable 和 drawable-v21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在旧版本的 Android Studio 中,所有可绘制存储桶文件夹都是默认创建的(即 drawable-mdpi、drawable-hdpi 等).在较新版本的 Android Studio 中,它仅提供 drawable 和 drawable-v21.这是为什么?

With older versions of Android Studio, all of the drawable bucket folders were created by default (i.e. drawable-mdpi, drawable-hdpi, etc.). In newer versions of Android Studio, it only provides drawable and drawable-v21. Why is that?

Android(以其无限智慧)不再为您提供单独的存储桶文件夹是否有原因?

Is there a reason that Android (in its infinite wisdom) no longer gives you the separate bucket folders?

请注意,我知道您可以手动添加它们,但我想确保没有一些新的最佳实践理由不再使用这些文件夹.

Just to note, I know you can just add them manually, but I want to make sure there isn't some new best practice reason to not use those folders anymore.

推荐答案

感谢所有提供帮助的人.您帮助我得出了最终答案,但没有一个解决方案是完全正确的.@user3137702 可能是最接近的,因为它与整个向量/SVG 的移动有关.我找不到明确的答案,比如直接从谷歌那里得到的答案(虽然我想它就在那里),但从我从一堆文章中收集的信息来看,他们这样做可能是有原因的.

Thank you to everyone who tried to help. You helped me reach the final answer, but no one solution was quite right. @user3137702 was probably the closest, as it IS related to the whole move to vectors/SVGs. I couldn't find a definitive answer, like something directly from Google (although I imagine it is out there), but from what I've gathered from a bunch of articles, there is probably a reason they are doing this.

对于初学者来说,这看起来像是从 Android Studio 1.4 开始的.我现在是 1.5.Android 似乎正在朝着不再需要您为可绘制对象创建自己的密度文件夹(即 mdpi、hdpi 等)的方向发展(mipmaps 是不同的,所以请不要将其与我所说的混淆).从 Android Studio 1.4 开始,它将采用您放入常规可绘制文件夹(而不是 v21 文件夹)中的 SVG,将它们转换为 PNG,并在构建序列期间将它们放置在自动生成的密度文件夹中(因此 Gradle对于 API 21 之前的所有版本,基本上是为您执行此操作.对于 21 及更高版本,支持的 SVG 有所不同,这是另一个主题.但这实质上使 SVG 支持一直向后兼容到 API 1!!!

For starters, it looks like this started in Android Studio 1.4. I am in 1.5 right now. It seems that Android is moving in the direction of no longer needing you to create your own density folders (i.e. mdpi, hdpi, etc.) for drawables (mipmaps is different, so please don't confuse that with what I am talking about). As of Android Studio 1.4, it will take the SVGs you put in the regular drawable folder (as in not the v21 folder), convert them to PNGs, and place them in auto-generated density folders for you during the build sequence (so Gradle does this for you, essentially) for all versions older than API 21. For 21 and up, SVG is supported different, which is a whole other topic. But this essentially makes SVG support backwards compatible all the way to API 1!!!

然而,有一个大问题.这种 SVG 转换并不总是像您希望的那样成功.它仅支持 SVG 文件的一个子集,因此根据您的保存方式(即保存时应用的设置),它可能无法正确呈现.即使是常用的设置,例如渐变和图案填充、本地 IRI 参考和转换(尚不支持).如果您正在使用不是您生成的 SVG 文件,则导入它们可能会出现问题.如果您或与您合作的人直接生成它们,您可能需要尝试保存文件的方式,并且您应该经常在旧版 Android 上测试构建,以确保结果如预期.

HOWEVER, there is a BIG catch. This SVG conversion is not always as successful as you might hope. It only supports a subset of SVG files, so depending on how you save it (i.e. what settings are applied when saving), it may not render properly. Even commonly used settings, such as gradient and pattern fills, local IRI references, and transformations are NOT supported (yet). If you are working with SVG files that you didn't generate, you will likely have problems importing them. If you or someone you work with directly generates them, you may have to experiment with how you save the files, and you should test builds often on older versions of Android to make sure it turned out as expected.

要将 SVG 导入 Android Studio 1.4+,请按照以下简单步骤操作:

To import SVGs into Android Studio 1.4+, follow these simple steps:

  1. 右键单击 res/drawable 文件夹
  2. 选择新建"
  3. 选择矢量资产"
  4. 此时,您可以选择一个材质图标",该图标有效真的很好,有一堆漂亮的免费"图标你可以从中选择.对于没有图标设计支持的独立开发者,这很好!
  5. 或者 - 您可以选择本地 SVG 文件"
  6. 然后使用选择"选项从任一选项中选择一个 SVG.警告:如果您导入的 SVG 未正确保存,则可能会出错.
  7. 点击下一步"
  8. 验证它是否保存在正确的位置,然后单击完成"
  9. 此时,它是可引用的:android:icon="@drawable/ic_imagename"(当然,使用您的图像名称而不是 ic_imagename)

@CommonsWare 的回应对找到正确的解决方案非常有帮助,但从我所见,从不同的模板和版本支持设置生成新项目的几种变体,实际上没有任何方法可以让旧的密度文件夹得到自动生成.这里肯定有更多的事情发生,而不仅仅是不同的模板版本选择.但正如他所说,根据您选择的模板/版本,您最终可能会得到一组不同的这两种可绘制文件夹类型.但具体到我的问题,Android Studio 似乎确实强调了这种根本不创建自己的个人可绘制密度文件夹的新方法.

@CommonsWare's response was very helpful in leading to the right solution, but from what I saw, generating several variations of new projects from different template and version support settings, there wasn't any way to actually have the old density folders get auto-generated. There is definitely more going on here than just a different template-version selection. But as he said, depending on what template/version you select, you may end up with a different set of those two drawable folder types. But specific to my question, Android Studio does seem to be putting an emphasis on this new approach of not creating your own individual drawable density folders at all.

很酷,imo,但它仍然需要一些工作.实际上,我可能仍然需要添加可绘制密度文件夹来支持我使用的所有图像,直到这种机制对所有类型的 SVG 渲染有了更多的支持.

It's pretty cool, imo, but it still needs some work. In practical terms, I will likely still need to add the drawable density folders to support all the images I work with, until this mechanism gets a little more supportive of all types of SVG renderings.

还有一个花絮 - 因为这都是通过 Gradle(密度文件夹的实际生成)处理的,所以您可以通过风味机制添加构建设置,以限制要生成的密度文件夹.因此,例如,如果您觉得 mdpi 图像对您的特定用户群的用处已经结束,并希望将该大小/密度从您的应用程序中删除以将应用程序大小减少几 MB,您可以在Gradle 构建风格.

And one more tidbit - Because this is all handled through Gradle (the actual generation of the density folders) you can add build settings through the flavor mechanism to limit which density folders you want to generate. So if, for example, you feel mdpi images have reached the end of their usefulness for your particular user base and would like to leave that size/density out of your app to shave a couple MB off the app size, you can set that in the Gradle build flavor.

这篇关于较新版本的 Android Studio 和只有两个可绘制目录 - drawable 和 drawable-v21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆