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

查看:291
本文介绍了较新版本的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的整体迁移有关.我找不到确切的答案,就像直接从Google那里找到的东西一样(尽管我想它已经在那里),但是从我从大量文章中收集到的信息来看,他们这样做的原因很可能是这样.

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开始,它将在构建顺序中将您放入常规drawable文件夹(而不是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. 然后从带有"choose"(选择)选项的任一选项中选择一个SVG.警告:如果您导入的SVG保存不正确,这可能会出错.
  7. 点击下一步"
  8. 验证它是否保存在正确的位置,然后单击完成"
  9. 在这一点上,它可通过以下方式引用:android:icon ="@ drawable/ic_imagename"(当然,使用您的图像名称而不是ic_imagename)
  1. Right-click on the res/drawable folder
  2. Select "New"
  3. Select "Vector Asset"
  4. At this point, you can select a "Material Icon", which works really well, and there are a bunch of beautiful "free" icons you can select from. For indie developers, without icon design support, this is nice!
  5. OR - you can select "Local SVG File"
  6. Then choose an SVG from either option with the "choose" option. WARNING: This is where it could possibly go wrong, if the SVG you import isn't saved properly.
  7. Hit "Next"
  8. Verify it is saving in the right place, and then Click "Finish"
  9. At this point, it is reference-able with: android:icon="@drawable/ic_imagename" (using your image name instead of ic_imagename, of course)

@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天全站免登陆