Java 如何在 Android API 21 下使用矢量可绘制对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34417843/
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
How to use vector drawables in Android API lower 21?
提问by iamatsundere181
I am working on an Android project and I chose <vector>
to display icon because it is adaptable and dynamically, however, I just can run this app on devices running Android, which have API 21 or higher. My question is how can I use <vector>
on lower Android version i.e. API 14 or kind of. Thanks!
我正在处理一个 Android 项目,我选择<vector>
显示图标,因为它具有适应性和动态性,但是,我只能在运行 Android 的设备上运行此应用程序,这些设备具有 API 21 或更高版本。我的问题是如何<vector>
在较低的 Android 版本(即 API 14 或类似版本)上使用。谢谢!
<!-- drawable/ic_android_debug_bridge.xmlxml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="48dp"
android:width="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@color/primaryColorDark"
android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 `0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" /></vector>`
采纳答案by 2Dee
Vector Drawables are now backward compatible, it's just a matter of upgrading your gradle version to 1.4.0-beta3 or higher, and upgrade your IDE :
Vector Drawables 现在向后兼容,只需将您的 gradle 版本升级到 1.4.0-beta3 或更高版本,然后升级您的 IDE:
We are also excited to offer backwards compatibility for your vector assets in Android Studio 1.4. Once you have a vectorDrawable image in your res/drawable, the Gradle plugin will automatically generate raster PNG images for API level 20 and below during build time. This means you only need to update and maintain your vector asset for your app project and Android Studio can take care of image conversion process.
我们也很高兴为您在 Android Studio 1.4 中的矢量资源提供向后兼容性。一旦您的 res/drawable 中有 vectorDrawable 图像,Gradle 插件将在构建期间自动为 API 级别 20 及以下生成光栅 PNG 图像。这意味着您只需要为您的应用项目更新和维护您的矢量资产,Android Studio 可以处理图像转换过程。
http://android-developers.blogspot.com.uy/2015/09/android-studio-14.html
http://android-developers.blogspot.com.uy/2015/09/android-studio-14.html
回答by Zielony
Just to make @2Dee's answer complete:
只是为了使@2Dee 的回答完整:
VectorDrawable cannot be used on API 20 and lower. The official method generates pngs, which then become BitmapDrawables, which breaks the idea of vector graphics.
VectorDrawable 不能用于 API 20 及更低版本。官方方法生成png,然后变成BitmapDrawables,打破了矢量图的思路。
Personally I prefer svg's over xml vectors:
我个人更喜欢 svg 而非 xml 向量:
- such graphics can be exported right from Illustrator or other popular software
- svg supports transformations, text, masks and other things properly
- true scallable vector graphics on all platforms
- smaller size and faster builds
- 此类图形可以直接从 Illustrator 或其他流行软件导出
- svg 正确支持转换、文本、蒙版和其他东西
- 所有平台上真正的可缩放矢量图形
- 更小的尺寸和更快的构建
To use vector graphics you can try https://github.com/BigBadaboom/androidsvg. It's an svg reader and svg-compatible ImageView.
要使用矢量图形,您可以尝试https://github.com/BigBadaboom/androidsvg。它是一个 svg 阅读器和与 svg 兼容的 ImageView。
回答by Alen Siljak
With the support library 23.2, the true support for Vector Drawables has been provided all the way down to API v7. It is recommended to disable the previous version of the support, which rendered PNG during build-time, by adding
在支持库 23.2 中,对 Vector Drawables 的真正支持一直提供到 API v7。建议通过添加禁用在构建时呈现 PNG 的先前版本的支持
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
to the build.gradle
file.
到build.gradle
文件。
The implementation is fairly simple. Just use the new srcCompatattribute on Drawables:
实现相当简单。只需在 Drawables 上使用新的srcCompat属性:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add" /> <= this is new
Vector Drawables are also supported in cases like TextView's drawableLeft
property.
在 TextView 的drawableLeft
属性等情况下也支持 Vector Drawables 。
Source: library announcement
来源:图书馆公告
However, I would still recommend something like Iconics library, AndroidSVG, or another font-icon or SVG solution for full SVG-standand support.
但是,我仍然会推荐像Iconics library、AndroidSVG或其他字体图标或 SVG 解决方案之类的东西,以获得完整的 SVG-standand 支持。
回答by mehrdad khosravi
you need use android Support Repository 30+ if you using android studio and need android support library 23.2.1+ if using Eclipse.
如果您使用 android studio,则需要使用 android Support Repository 30+,如果使用 Eclipse,则需要 android support library 23.2.1+。
check your build.gradle
(project) if using version 2.0+ add below code in your build.gradle
(app)
build.gradle
如果使用 2.0+ 版,请检查您的(项目)在您的build.gradle
(应用程序)中添加以下代码
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
and: if using version 1.5 add below in your build.gradle
(app)
和:如果使用 1.5 版,请在您的build.gradle
(应用程序)中添加以下内容
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
here is sample code for use vector icon:
这是使用矢量图标的示例代码:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add"
tools:ignore="MissingPrefix" />
or
或者
<ImageButton
android:layout_width="wrap_content"
android:background="@android:color/transparent"
app:srcCompat="@drawable/camera"
tools:ignore="MissingPrefix"
android:layout_height="wrap_content"/>
Vector Drawables are also supported in cases like TextView's drawableLeft
property. but it worked api 22+ to me and i still dont know how it will work for low api.
在 TextView 的drawableLeft
属性等情况下也支持 Vector Drawables 。但它对我来说是 22+ 的 api,我仍然不知道它对低 api 是如何工作的。
Also keep in mind if you want to be compatible below API 21:
另外请记住,如果您想在 API 21 以下兼容:
- you cannot use the
android:background
property in xml orView.setBackgroundResource()
function. You need to use theView.setBackground()
. - you cannot use the svg-s in StateListDrawable xml-s or other xml drawables, you have to generate them programmatically.
- you cannot use svg-s in case of notifications.
- 您不能
android:background
在 xml 或View.setBackgroundResource()
函数中使用该属性。您需要使用View.setBackground()
. - 您不能在 StateListDrawable xml-s 或其他 xml drawable 中使用 svg-s,您必须以编程方式生成它们。
- 在通知的情况下,您不能使用 svg-s。
回答by Yazon2006
I found solution! For those who search solution with TextView and other "android" namespace attributes. First of all this is necessary:
我找到了解决方案!对于那些使用 TextView 和其他“android”命名空间属性搜索解决方案的人。首先这是必要的:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
And in application class define this:
在应用程序类中定义:
@Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Now you can use app:srcCompat="@drawable/ic_add"
but if you try to use android:background=
or android:drawableLeft=
it will crash app with "Error inflating" exception.
现在您可以使用,app:srcCompat="@drawable/ic_add"
但如果您尝试使用,android:background=
否则android:drawableLeft=
它会因“错误膨胀”异常而使应用程序崩溃。
We can create wrapped drawable ic_add_wrapped.xml
for this vector:
我们可以ic_add_wrapped.xml
为这个向量创建包装的可绘制对象:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_add"/>
</layer-list>
And now it will work with any property like drawableLeft or background. Just set android:drawableLeft="@drawable/ic_add_wrapped.xml"
.
WARNING! THIS IS A WORKAROUND SOLUTION. So you use it for your own risk.
现在它将适用于任何属性,如 drawableLeft 或背景。刚设置android:drawableLeft="@drawable/ic_add_wrapped.xml"
。警告!这是一个变通的解决方案。所以你用它来承担风险。
回答by cVoronin
When you need to add VectorDrawable (created from SVG) programatically, you can do it like this:
当您需要以编程方式添加 VectorDrawable(从 SVG 创建)时,您可以这样做:
icon = VectorDrawableCompat.create(resources, R.drawable.ic_map_black_24dp, null)
回答by Mark O'Sullivan
I had an issue where my vector images would show up but would be completely black, this was due to an issue where you can't reference color resources in the vector xml file.
我有一个问题,我的矢量图像会显示,但会是全黑的,这是由于您无法在矢量 xml 文件中引用颜色资源的问题。
So instead of @color/primaryColorDark
you have to use the actual color e.g. #212121
因此,@color/primaryColorDark
您不必使用实际颜色,例如#212121
回答by David Ferrand
VectorDrawable are supported pre-Lollipop via the Support Library, but the way to use them depends on the version of Support Library you have. And it may not work in all cases.
VectorDrawable 通过 Support Library 在 Lollipop 之前得到支持,但使用它们的方式取决于您拥有的 Support Library 版本。它可能不适用于所有情况。
I've made this diagramto help (valid for Support Library 23.4.0 to - at least - 25.1.0).
我制作了这张图来提供帮助(对于支持库 23.4.0 到 - 至少 - 25.1.0 有效)。
回答by Patrick Henderson
If you are using Android Studio 3.0.0 you can set
如果您使用的是 Android Studio 3.0.0,您可以设置
android.enableAapt2=false
in gradle.properties
在 gradle.properties 中
https://www.reddit.com/r/androiddev/comments/6mj8di/android_studio_30_canary_6_released/
https://www.reddit.com/r/androiddev/comments/6mj8di/android_studio_30_canary_6_released/
回答by Santanu Sur
You can use programatically ..to set drawableLeft
to your editText
or textView
您可以以编程方式使用 .. 设置drawableLeft
为您的editText
或textView
like
喜欢
Drawable tick_drawable = VectorDrawableCompat.create(getResources(), R.drawable.green_tick, null);
if (tick_drawable != null) {
tick_drawable.setBounds(0, 0, tick_drawable.getIntrinsicWidth(),tick_drawable.getIntrinsicHeight());
}
And to drawable left like this..
并像这样向左绘制..
editText.setCompoundDrawables( tick_drawable , null, null, null );