android – AppCompatDrawableManager.get()vs VectorDrawableCompat.create()

我正在使用支持lib版本24.2.1,并使用AppCompatDelegate.setCompatVectorFromresourcesEnabled(true)启用了支持向量;

这些关于支持向量的函数有什么不同?我使用的是VectorDrawableCompat.create(getResources(),R.drawable.my_vector,null).但是这样在我的测试设备(Android 4.3)上生成一个drawable时,在编程上按钮上设置一个drawable就像这样:

button.setCompoundDrawablesWithIntrinsicBounds(icon,null,null);

使用AppCompatDrawableManager.get().getDrawable(getActivity(),R.drawable.my_vector); (包含在状态列表选择器中)似乎工作正常,虽然我似乎无法找到它的文档.

解决方法

What is the difference in these functions regarding support vectors?

AppCompatDrawable.getDrawable(…)将膨胀各种drawables,包括

> API 21下面的矢量drawables(仅当构建脚本启用支持向量drawable时;进一步阅读)
>适当主题的AppCompat drawables
> ContextCompat.getDrawable可获得的任何其他drawable(Context,int)

方法在内部调用AppCompatDrawableManager.get().getDrawable(Context,int),它不是公共API的一部分.从消费者的角度来看,这两种方法功能相同.

VectorDrawableCompat.create(…)只会扩展矢量drawable(仅当构建脚本启用了支持向量drawable时;进一步阅读).

But this does not produce a drawable on my test device (Android 4.3)

VectorDrawableCompat.create(…)将在出错时返回null.如果引用的drawable不是vector drawable,如果你没有正确配置构建插件并且为API 21以下的平台生成PNG,那么就会发生这种情况.

通过更改app模块build.gradle来激活API 21下面的矢量drawables支持

// Gradle Plugin 2.0+  
android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
  }  
}

有关详细信息,请参阅Android Support Library 23.2博客文章.

I am using support lib version 24.2.1,and have enabled support vectors with AppCompatDelegate.setCompatVectorFromresourcesEnabled(true)

方法不“启用支持向量”.首先,您需要在build.gradle中启用支持向量drawable,如上所述.

之后,此方法基本上支持可绘制容器内的支持向量drawable,如LayerDrawable或StateListDrawable.

有关如何滥用此信息的详细信息,请参阅AppCompat — Age of the vectors.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...