VectorDrawable - is it available somehow for pre-Lollipop versions of Android?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26548354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:47:19  来源:igfitidea点击:

VectorDrawable - is it available somehow for pre-Lollipop versions of Android?

androidgradlevectorandroid-vectordrawable

提问by android developer

Background

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.

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.

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.

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.

The questions

The questions

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

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?

  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?

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

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

  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?

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

采纳答案by Amit Vaghela

UPDATE ON March 2016

UPDATE ON March 2016

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

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

Please update version of a library in gradle file.

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 VectorDrawableand AnimatedVectorDrawableare now available through two new Support Libraries support-vector-drawableand animated-vector-drawable.new app:srcCompatattribute to reference vector drawables .

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 VectorDrawableand AnimatedVectorDrawableare now available through two new Support Libraries support-vector-drawableand animated-vector-drawable.new app:srcCompatattribute to reference vector drawables .

Check source on githubwith some sample examples.

Check source on githubwith some sample examples.

Changes for v7 appcompat library:

Changes for v7 appcompat library:

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

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

回答by razzledazzle

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

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.

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

Check this 23.4.0 available now

Update: This doesn't work from version 23.3.0 Check herefor details. Proxy drawables don't work. app:srcCompatand setImageResource()work, however.

Update: This doesn't work from version 23.3.0 Check herefor details. Proxy drawables don't work. app:srcCompatand 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.

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.

Firststep would be to bump the AppCompatversion.

Firststep would be to bump the AppCompatversion.

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

Secondenable Vector Drawable support. If using Gradle plugin, 2.0+

Secondenable Vector Drawable support. If using Gradle plugin, 2.0+

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
   }  
}

Otherwise

Otherwise

android {  
   defaultConfig {  
     generatedDensities = []  
   }  

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

Third, refer to the linked answer.

Third, refer to the linked answer.

回答by V_J

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

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

回答by David Ferrand

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

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 diagramto help (valid for Support Library 23.4.0 to - at least - 25.1.0).

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

VectorDrawable cheatsheet

VectorDrawable cheatsheet

回答by gnuanu

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.

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 Githuband it will support android versions 7+.

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

From the official Readme

From the official Readme

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

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,

To create the drawable from the vector XML,

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

Hope this helps.

Hope this helps.

回答by A-student

Lollipop cannot handle SVG files without third-party libs.

Lollipop cannot handle SVG files without third-party libs.

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

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

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

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

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

SVG to VectorDrawable Converteris 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.

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

回答by Mehlyfication

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

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.

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

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

回答by George Mount

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

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.

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.

回答by LOG_TAG

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

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

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

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

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:

Guide to use:

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

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