VectorDrawable: Android loads xhdpi PNG's instead of the vector resource

巧了我就是萌 提交于 2019-12-07 03:32:31

问题


I'm trying to use a VectorDrawable on API21, but Android loads the PNG resource from xxhdpi folder instead. My current res structure as follows:

  • res
    • drawable-xxhdpi
      • test_icon.png
    • drawable-21
      • test_icon.xml

And my XML layout:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/test_icon"/>

Are there any other ways to solve this? From my understanding Android will always pick the PNG resource, but if that's the case, how one can use VectorDrawables for API21 and PNG for lower API's?

[Update 1]

If we use a drawable-xxhdpi-21 resource folder, Android will pick the vector instead of the PNG resource. But that means we would have to have a copy (or symlink) of the file for other densities as well (e.g. xhdpi, hdpi, etc)


回答1:


drawable-anydpi-v21 is right for that

drawable-anydpi-v21/test_icon.xml

https://google.github.io/material-design-icons/#icons-for-android




回答2:


The solution I found is a bit of a hack but seems to work. Instead of putting your VertorDrawable xml files in each of the bucket with the version, if you put them in drawable-w1dp-v21 it takes the right resource and don't need to have multiple copies.

This is because, when deciding which resource to use, Android checks the density and after the minimum width density, which in this case is always gonna be more than 1dp :)

Update: as @pengrad mentions best to use the anydpi bucket. :)



来源:https://stackoverflow.com/questions/29001230/vectordrawable-android-loads-xhdpi-pngs-instead-of-the-vector-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!