在棒棒糖崩溃前使用android矢量Drawables [英] Using android vector Drawables on pre Lollipop crash

查看:124
本文介绍了在棒棒糖崩溃前使用android矢量Drawables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lollipop之前的 android系统中使用矢量可绘制对象,这些是我的某些库和工具版本:

I'm using vector drawables in android prior to Lollipop and these are of some of my libraries and tool versions:

  • Android Studio:2.0
  • Android Gradle插件:2.0.0
  • 构建工具:23.0.2
  • Android支持库:23.3.0

我在应用程序级别Build.Gradle

android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
   }  
}

值得一提的是,我使用了一个额外的可绘制对象,如Android官方博客(

It is also worth mentioning that I use an extra drawable such as LayerDrawable(layer_list) as stated in Android official Blog (link here) for setting drawables for vector drawables outside of app:srcCompat

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/search"/>
</level-list>

您会发现直接引用了矢量可绘制对象的外部 app:srcCompat将在棒棒糖之前失败.但是,AppCompat确实 当在另一个中引用时,支持加载矢量可绘制对象 可绘制容器,例如StateListDrawable,InsetDrawable, LayerDrawable,LevelListDrawable和RotateDrawable.通过使用这个 间接,在诸如TextView的情况下,您可以使用矢量绘制 android:drawableLeft属性,该属性通常无法 支持矢量可绘制对象.

You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop. However, AppCompat does support loading vector drawables when they are referenced in another drawable container such as a StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable. By using this indirection, you can use vector drawables in cases such as TextView’s android:drawableLeft attribute, which wouldn’t normally be able to support vector drawables.

当我使用app:srcCompat时,一切正常,但是当我使用时:

When I'm using app:srcCompat everything works fine, but when I use:

android:background
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom

在棒棒糖之前的ImageViewImageButtonTextViewEditText上,它会产生期望值:

on ImageView, ImageButton, TextView or EditText prior to Lollipop, it throws an expection:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/search_toggle.xml from drawable resource ID #0x7f0200a9

推荐答案

最新更新-2019年6月

自原始答案以来,支持库已作了一些更改.现在,即使是Gradle的Android插件也能够在构建时自动生成PNG.因此,下面是目前应该使用的两种新方法.您可以在此处找到更多信息:

Support Library has changed a bit since the original answer. Now, even the Android plugin for Gradle is able to automatically generate the PNG at build time. So, below are two new approaches that should work these days. You can find more info here:

PNG生成

PNG Generation

Gradle可以在构建时根据您的资产自动创建PNG图像.但是,采用这种方法,并非所有xml元素都受支持.此解决方案很方便,因为您无需更改代码或build.gradle中的任何内容.只要确保您使用的是 Android插件1.5.0或更高版本 Android Studio 2.2或更高版本.

Gradle can automatically create PNG images from your assets at build time. However, in this approach, not all xml elements are supported. This solution is convenient because you don't need to change anything in your code or in your build.gradle. Just make sure you are using Android Plugin 1.5.0 or higher and Android Studio 2.2 or higher.

我正在我的应用程序中使用此解决方案,并且工作正常.不需要其他的 build.gradle 标志.无需黑客.如果您转到/build/generated/res/pngs/...,则可以看到所有生成的PNG.

I'm using this solution in my app and works fine. No additional build.gradle flag necessary. No hacks is necessary. If you go to /build/generated/res/pngs/... you can see all generated PNGs.

因此,如果您有一些简单的图标(因为并非所有xml元素都受支持),则此解决方案可能对您有用.只需更新您的Android Studio和适用于Gradle的Android插件即可.

So, if you have some simple icon (since not all xml elements are supported), this solution may work for you. Just update your Android Studio and your Android plugin for Gradle.

支持库

Support Library

这可能是适合您的解决方案.如果您来到这里,则意味着您的Android Studio不会自动生成PNG.因此,您的应用程序崩溃了.

Probably, this is the solution that will work for you. If you came here, it means your Android Studio is not generating the PNGs automatically. So, your app is crashing.

或者,也许您根本不希望Android Studio生成任何PNG.

Or maybe, you don't want Android Studio to generate any PNG at all.

与支持XML元素子集的自动PNG生成"不同,此解决方案支持所有xml标签.因此,您对矢量可绘制对象具有完全的支持.

Differently from that "Auto-PNG generation" which supports a subset of XML element, this solution, supports all xml tags. So, you have full support to your vector drawable.

您必须首先更新您的 build.gradle 以支持它:

You must first, update your build.gradle to support it:

android {
  defaultConfig {
    // This flag will also prevents Android Studio from generating PNGs automatically
    vectorDrawables.useSupportLibrary = true
  }
}

dependencies {
  // Use this for Support Library
  implementation 'com.android.support:appcompat-v7:23.2.0' // OR HIGHER

  // Use this for AndroidX
  implementation 'androidx.appcompat:appcompat:1.1.0' // OR HIGHER
}

然后,在加载VectorDrawables时使用app:srcCompat而不是android:src.不要忘记这一点.

And then, use app:srcCompat instead of android:src while loading VectorDrawables. Don't forget this.

对于TextView,如果使用的是支持库的androidx版本,则可以使用app:drawableLeftCompat(或右,上,下)代替app:drawableLeft

For TextView, if you are using the androidx version of the Support Library, you can use app:drawableLeftCompat (or right, top, bottom) instead of app:drawableLeft

对于CheckBox/RadioButton,请使用app:buttonCompat而不是android:button.

In case of CheckBox/RadioButton, use app:buttonCompat instead of android:button.

如果您未使用支持库的androidx版本并且minSdkVersion17或更高版本或使用按钮,则可以尝试通过以下方式进行编程设置:

If you are not using the androidx version of the Support Library and your minSdkVersion is 17 or higher or using a button, you may try to set programmatically via

Drawable icon = AppCompatResources.getDrawable(context, <drawable_id>);
textView.setCompoundDrawablesWithIntrinsicBounds(<leftIcon>,<topIcon>,<rightIcon>,<bottomIcon>);


更新-2016年7月

他们在
中重新启用了VectorDrawable Android支持库23.4.0

They re-enabled that VectorDrawable in
Android Support Library 23.4.0

对于AppCompat用户,我们添加了选择加入 API,以通过AppCompatDelegate.setCompatVectorFromResourcesEnabled重新启用资源中的Vector Drawable支持(行为在23.2中). (true)-请记住,这仍然可能导致内存使用问题和更新配置实例的问题,因此默认情况下为什么将其禁用.

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.

也许build.gradle设置现在已过时,您只需要在适当的活动中启用它即可(但是,需要测试).

Maybe, build.gradle setting is now obsolete and you just need to enable it in proper activities (however, need to test).

现在,要启用它,您必须执行以下操作:

Now, to enable it, you must do:

public class MainActivity extends AppCompatActivity {
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    ...
}


原始答案-2016年4月

我认为这是由于在最新的库版本23.3.0中禁用了Support Vector而引起的.

I think this is happening because Support Vector was disabled in the latest library version: 23.3.0

根据此 POST :

对于AppCompat用户,由于在版本23.2.0/23.2.1中的实现中发现的问题,我们决定删除可用于从棒棒糖设备上的资源中使用矢量绘制工具的功能. ://code.google.com/p/android/issues/detail?id = 205236"rel =" noreferrer>(问题205236).使用app:srcCompat和setImageResource()继续工作.

For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1 (ISSUE 205236). Using app:srcCompat and setImageResource() continues to work.

如果您访问问题问题205236 ,它们将在将来启用,但是内存问题不会很快得到解决:

If you visit issue ISSUE 205236, it seems that they will enable in the future but the memory issue will not be fixed soon:

在下一个版本中,我添加了一个选择加入的API,您可以在其中重新启用已删除的VectorDrawable支持.它具有与以前相同的警告(内存使用情况和配置更新问题).

In the next release I've added an opt-in API where you can re-enable the VectorDrawable support which was removed. It comes with the same caveats as before though (memory usage and problems with Configuration updating).

我有一个类似的问题.因此,就我而言,我将所有使用vector drawable的图标从资源重新还原为PNG图像(因为即使提供重新启用选项,内存问题仍然会发生).

I had a similar issue. So, in my case, I reverted all icons which use vector drawable from resource to PNG images again (since the memory issue will keep happening even after they provide an option to enable it again).

我不确定这是否是最佳选择,但我认为它可以解决所有崩溃问题.

I'm not sure if this is the best option, but it fixes all the crashes in my opinion.

这篇关于在棒棒糖崩溃前使用android矢量Drawables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆