VectorDrawable——它是否适用于lollipop版本的Android?

时间:2022-11-21 10:32:12

Background

I've noticed that Android now supports some kind of vector drawing, via a class called "VectorDrawable" (and also AnimatedVectorDrawable, BTW). I've found about it by looking at what's new on Android-Studio.

我注意到Android现在支持某种向量绘制,通过一个名为“VectorDrawable”的类(顺便说一句,还支持AnimatedVectorDrawable)。通过Android-Studio的新特性,我发现了这一点。

I wonder if this would be the end of having to put multiple files into multiple folders (mdpi, hdpi, xhdpi, etc). That would be great and might minimize apps sizes on some cases.

我想知道这是否意味着必须将多个文件放入多个文件夹(mdpi、hdpi、xhdpi等)的结束。这很好,可能会在某些情况下减少应用程序的大小。

The questions

I'd like to ask a few questions regarding this new class:

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

  1. Is it possible to use it in older Android versions, maybe via a library of even the support library of Google?

    是否有可能在旧的Android版本中使用它,也许是通过谷歌的支持库?

  2. I'm not familiar with how it works, but can Lollipop handle SVG files? Can it do anything that is achievable on SVG files?

    我不熟悉它是如何工作的,但是棒棒糖能处理SVG文件吗?它能在SVG文件上实现什么吗?

  3. Is there any sample/tutorial/video of using it, other than the documentation I've found?

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

9 个解决方案

#1


37  

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)

通过Android支持库23.2.1更新,支持向量绘制和动画向量绘制。(同样,你也可以使用latestone)

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 .

向量绘制允许您用XML定义的单个向量图形替换多个png资产。虽然以前只局限于棒棒糖和更高的设备,但是矢量绘制和动画矢量绘制现在都可以通过两个新的支持库支持矢量绘制和动画矢量绘制。新应用程序:srcCompat属性,用于引用向量绘制。

Check source on github with some sample examples.

用一些示例检查github上的源代码。

Changes for v7 appcompat library:

v7 appcompat库的更改:

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

恢复对向量资产的依赖,这样使用appcompat库的开发人员就不会*使用VectorDrawable及其相关的构建标志。

#2


12  

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

您可以尝试这个支持库。它支持在Lollipop中引入具有完全向后兼容性的VectorDrawable和AnimatedVectorDrawable。

#3


8  

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

更新2:支持库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.

对于AppCompat用户,我们添加了一个可选的API,通过AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)重新启用资源中的支持向量绘制(行为见23.2)——请记住,这仍然会导致内存使用问题和配置实例更新问题,因此默认情况下它被禁用。

Check this 23.4.0 available now

现在就来看看这个23.4.0

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.

更新:这在23.3.0版本中不能使用,请在这里查看详细信息。代理画板不工作。但是,srcCompat和setImageResource()可以工作。


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.

矢量绘图支持可以从23.2版本的支持库中获得。但是,要正确地使用这些drawables,必须间接地引用它们。

First step would be to bump the AppCompat version.

第一步是启动AppCompat版本。

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

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

第二个支持向量的可绘制支持。如果使用Gradle插件,2.0+。

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
   }  
}

Otherwise

否则

android {  
   defaultConfig {  
     generatedDensities = []  
   }  

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

Third, refer to the linked answer.

第三,参考链接的答案。

#4


6  

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.

不幸的是,此时在支持库中无法使用VectorDrawable和AnimatedVectorDrawable。但是要在棒棒糖的前版本中使用这个特性,您可以使用非官方的称为MrVector的后台端口。

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

MrVector在Github上是可用的,它将支持android版本7+。

From the official Readme

从官方的自述

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

要添加MrVector依赖项,请向您的构建中添加以下一行。gradle依赖性。

compile 'com.telly:mrvector:0.2.0'

To create the drawable from the vector XML,

要从矢量XML中创建可绘图文件,

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

Hope this helps.

希望这个有帮助。

#5


3  

Lollipop cannot handle SVG files without third-party libs.

Lollipop没有第三方libs无法处理SVG文件。

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

我找到的最佳解决方案是BetterVectorDrawable lib和SVG到VectorDrawable转换器。

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

BetterVectorDrawable是Android 4.0+的VectorDrawable实现,可以在Android 5.0+上使用可配置的回退行为。

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

SVG到VectorDrawable转换器是SVG图像到Android VectorDrawable XML资源文件的批处理转换器。在线版本

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

链接指向readmes,它提供了关于如何使用lib和转换器的足够信息。

#6


3  

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

如果您正在使用VectorDrawable, Android Studio将根据PNG文件(基于您的XML文件)自动生成lollipop版本。

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.

注意,在运行API低于21的设备上,生成的PNG文件被认为是位图绘制,而不是向量绘制,因此不能在这些设备上进行动画或类似的操作。

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

详情请参阅“向后兼容”:http://android- developers.blogspot.co.at5/09/android - studio14.html

#7


3  

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

为了补充这里的一些答案:是的,您可以获得对VectorDrawables pre-Lollipop的支持,至少是部分支持。

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

部分如何?这取决于-我做了这个图来帮助(支持库23.4.0到-至少- 25.1.0)。

VectorDrawable——它是否适用于lollipop版本的Android?

#8


2  

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

在这个时候,支持库中没有VectorDrawables。

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.

Funkystein是对的——VectorDrawable类似于SVG,只支持最需要的矢量绘图功能,以便android能够专注于性能。例如,pathData具有与SVG的“d”字符串相同的格式。

#9


1  

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

好消息是谷歌发布了Android支持库23.2支持向量绘制和动画向量绘制!

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

这就是AppCompat库的优点所在,它们可以将Android的许多新特性带回更早的版本。有了新实现的VectorDrawable类,开发人员现在可以一直使用vector映像到API 7 (Android 2.1 Eclair)。动画矢量有点局限,只追溯到API 11 (Android 3.0蜂巢),但它仍然包含了超过97%的活跃设备

Guide to use:

使用指南:

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

请参考“age-of-the-vectors”@chrisbanes

#1


37  

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)

通过Android支持库23.2.1更新,支持向量绘制和动画向量绘制。(同样,你也可以使用latestone)

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 .

向量绘制允许您用XML定义的单个向量图形替换多个png资产。虽然以前只局限于棒棒糖和更高的设备,但是矢量绘制和动画矢量绘制现在都可以通过两个新的支持库支持矢量绘制和动画矢量绘制。新应用程序:srcCompat属性,用于引用向量绘制。

Check source on github with some sample examples.

用一些示例检查github上的源代码。

Changes for v7 appcompat library:

v7 appcompat库的更改:

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

恢复对向量资产的依赖,这样使用appcompat库的开发人员就不会*使用VectorDrawable及其相关的构建标志。

#2


12  

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

您可以尝试这个支持库。它支持在Lollipop中引入具有完全向后兼容性的VectorDrawable和AnimatedVectorDrawable。

#3


8  

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

更新2:支持库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.

对于AppCompat用户,我们添加了一个可选的API,通过AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)重新启用资源中的支持向量绘制(行为见23.2)——请记住,这仍然会导致内存使用问题和配置实例更新问题,因此默认情况下它被禁用。

Check this 23.4.0 available now

现在就来看看这个23.4.0

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.

更新:这在23.3.0版本中不能使用,请在这里查看详细信息。代理画板不工作。但是,srcCompat和setImageResource()可以工作。


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.

矢量绘图支持可以从23.2版本的支持库中获得。但是,要正确地使用这些drawables,必须间接地引用它们。

First step would be to bump the AppCompat version.

第一步是启动AppCompat版本。

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

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

第二个支持向量的可绘制支持。如果使用Gradle插件,2.0+。

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
   }  
}

Otherwise

否则

android {  
   defaultConfig {  
     generatedDensities = []  
   }  

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

Third, refer to the linked answer.

第三,参考链接的答案。

#4


6  

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.

不幸的是,此时在支持库中无法使用VectorDrawable和AnimatedVectorDrawable。但是要在棒棒糖的前版本中使用这个特性,您可以使用非官方的称为MrVector的后台端口。

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

MrVector在Github上是可用的,它将支持android版本7+。

From the official Readme

从官方的自述

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

要添加MrVector依赖项,请向您的构建中添加以下一行。gradle依赖性。

compile 'com.telly:mrvector:0.2.0'

To create the drawable from the vector XML,

要从矢量XML中创建可绘图文件,

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

Hope this helps.

希望这个有帮助。

#5


3  

Lollipop cannot handle SVG files without third-party libs.

Lollipop没有第三方libs无法处理SVG文件。

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

我找到的最佳解决方案是BetterVectorDrawable lib和SVG到VectorDrawable转换器。

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

BetterVectorDrawable是Android 4.0+的VectorDrawable实现,可以在Android 5.0+上使用可配置的回退行为。

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

SVG到VectorDrawable转换器是SVG图像到Android VectorDrawable XML资源文件的批处理转换器。在线版本

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

链接指向readmes,它提供了关于如何使用lib和转换器的足够信息。

#6


3  

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

如果您正在使用VectorDrawable, Android Studio将根据PNG文件(基于您的XML文件)自动生成lollipop版本。

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.

注意,在运行API低于21的设备上,生成的PNG文件被认为是位图绘制,而不是向量绘制,因此不能在这些设备上进行动画或类似的操作。

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

详情请参阅“向后兼容”:http://android- developers.blogspot.co.at5/09/android - studio14.html

#7


3  

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

为了补充这里的一些答案:是的,您可以获得对VectorDrawables pre-Lollipop的支持,至少是部分支持。

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

部分如何?这取决于-我做了这个图来帮助(支持库23.4.0到-至少- 25.1.0)。

VectorDrawable——它是否适用于lollipop版本的Android?

#8


2  

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

在这个时候,支持库中没有VectorDrawables。

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.

Funkystein是对的——VectorDrawable类似于SVG,只支持最需要的矢量绘图功能,以便android能够专注于性能。例如,pathData具有与SVG的“d”字符串相同的格式。

#9


1  

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

好消息是谷歌发布了Android支持库23.2支持向量绘制和动画向量绘制!

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

这就是AppCompat库的优点所在,它们可以将Android的许多新特性带回更早的版本。有了新实现的VectorDrawable类,开发人员现在可以一直使用vector映像到API 7 (Android 2.1 Eclair)。动画矢量有点局限,只追溯到API 11 (Android 3.0蜂巢),但它仍然包含了超过97%的活跃设备

Guide to use:

使用指南:

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

请参考“age-of-the-vectors”@chrisbanes