VectorDrawable-是否可以在安卓的前棒棒糖版本中使用?

背景资料

我注意到 Android 现在支持某种类型的矢量绘图,通过一个叫做“ < strong > VectorDrawable ”的类(还有 < strong > AnimatedVectorDrawable ,顺便说一下)。我是通过观看 < strong > Android-Studio 的新节目发现的。

我想知道这是否是必须将多个文件放入多个文件夹(mdpihdpixhdpi等)的结束。这将是伟大的,并可能在某些情况下最小化应用程序的大小。

那些问题

关于这门新课,我想问几个问题:

  1. 有没有可能在旧的 Android 版本中使用它,甚至是通过谷歌的支持库?

  2. 我不太熟悉它的工作原理,但是棒棒糖可以处理 SVG 文件吗?它能做任何 SVG 文件上可以做到的事情吗?

  3. 除了我找到的文档之外,还有其他使用它的示例/教程/视频吗?

56575 次浏览

There are no VectorDrawables in the support library at this time.

Funkystein is right -- VectorDrawable is similar to SVG, only supporting the features of vector drawing that are in highest demand so that android can focus on performance. pathData, for example has the same format as SVG's "d" string.

Unfortunately, at this point of time VectorDrawable and AnimatedVectorDrawable are not available in support library. But to avail this feature in Pre-Lollipop versions, you can use the unofficial backport called MrVector.

MrVector is available in Github and it will support android versions 7+.

From the official Readme

To add MrVector dependency add the following line to your build.gradle dependencies block.

compile 'com.telly:mrvector:0.2.0'

To create the drawable from the vector XML,

Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_android);

Hope this helps.

You can try this support library. It supports VectorDrawable and AnimatedVectorDrawable introduced in Lollipop with fully backwards compatibility.

Lollipop cannot handle SVG files without third-party libs.

The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+.

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version

Links point to readmes, which provide enough information on how to use the lib and the converter.

If you are using VectorDrawable, Android Studio will automatically generate according PNG files (based on your XML files) for Pre-Lollipop versions.

Note that those generated PNG files are considered BitmapDrawables instead of VectorDrawables on devices running API below 21 and therefore can't be animated or similar on those devices.

See "backwards compatibility" for further details: http://android-developers.blogspot.co.at/2015/09/android-studio-14.html

The great news is that Google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables!

But thanks go to the people who ported this library before Google!

This is where the AppCompat libraries are great, they can bring many of the new features of Android back to much earlier versions. With the newly implemented VectorDrawable class, developers can now use vector images all the way back to API 7 (Android 2.1 Eclair). Animated vectors are a bit more limited, going only as far back as API 11 (Android 3.0 Honeycomb), but that still encompasses more than 97% of devices in active use today

Guide to use:

Refer "age-of-the-vectors" by @chrisbanes

UPDATE ON March 2016

By Android Support Library 23.2.1 update, Support Vector Drawables and Animated Vector Drawables. (you can also use latestone for the same)

Please update version of a library in gradle file.

compile 'com.android.support:recyclerview-v7:23.2.1'

Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices, both VectorDrawable and AnimatedVectorDrawable are now available through two new Support Libraries support-vector-drawable and animated-vector-drawable. new app:srcCompat attribute to reference vector drawables .

Check source on github with some sample examples.

Changes for v7 appcompat library:

Reverted dependency on vector assets so that developers using the appcompat library are not forced to use VectorDrawable and its associated build flags.

Update 2: They enable it again in Support Library 23.4.0:

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

Check this 23.4.0 available now

Update: This doesn't work from version 23.3.0 Check here for details. Proxy drawables don't work. app:srcCompat and setImageResource() work, however.


Vector Drawable support is available from the Support Library of version 23.2 and beyond. However, to properly use those drawables, they must be referenced indirectly.

First step would be to bump the AppCompat version.

compile 'com.android.support:appcompat-v7:23.2.0'

Second enable Vector Drawable support. If using Gradle plugin, 2.0+

android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

Otherwise

android {
defaultConfig {
generatedDensities = []
}


aaptOptions {
additionalParameters "--no-version-vectors"
}
}

Third, refer to the linked answer.

To complement some of the answers here: yes, you can get support for VectorDrawables pre-Lollipop, at least partial.

How partial? It depends - I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).

VectorDrawable cheatsheet