AppCompat 23.2将VectorDrawableCompat与API< 21上的RemoteViews(AppWidget)一起使用 [英] AppCompat 23.2 use VectorDrawableCompat with RemoteViews (AppWidget) on API<21

本文介绍了AppCompat 23.2将VectorDrawableCompat与API< 21上的RemoteViews(AppWidget)一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AppWidget,并且我想在Lollipop之前的设备中也使用VectorDrawables. VectorDrawableCompat不适用于我创建的RemoteView.

I have an AppWidget and I'd like to use VectorDrawables in it also on pre-Lollipop devices. VectorDrawableCompat won't work with the RemoteViews I create.

为了减小我的应用APK大小,我不想为较旧的API平台添加其他可绘制对象的PNG版本.

To keep my app APK size down, I don't want to add alternative PNG versions of my drawables for older API platforms.

我该怎么做?

推荐答案

更新22/10/2017

@ user924指出,现在 AppCompatDrawableManager 访问仅限于其自己的库. ContextCompat.getDrawable(...)应该可以解决问题.

As noted by @user924 now AppCompatDrawableManager access is restricted to its own library. ContextCompat.getDrawable(...) should do the trick.

更新2016年5月9日

正如@ kirill-kulakov在回答中指出的那样,支持库的最新更新将TintContextWrapper的可见性限制在其自己的程序包中. 我正在更新我的答案,以删除错误的代码,但请感谢Kirill的改正!

As noted by @kirill-kulakov in its answer, the latest updates of the Support Library restricted the TintContextWrapper visibility to its own package. I'm updating my answer to remove the incorrect code, but please thank Kirill for the correction!

您可以避免通过简单的 hack 添加矢量可绘制资源的其他光栅化版本:通过 TintContextWrapper 使用AppCompat TintResources 使用 AppCompatDrawableManager 使用 ContextCompat .

You can avoid adding alternative rasterized versions of your vector drawable resources with an easy hack: use AppCompat TintResources through TintContextWrapper using AppCompatDrawableManager using ContextCompat.

TintResources AppCompatDrawableManager ContextCompat 是其中的一类棒棒糖之前的设备,解析 VectorDrawable 的XML文件并将其转换为 VectorDrawableCompat 实例,这些实例可以一直使用到API 7.

TintResources AppCompatDrawableManager ContextCompat is the class that among other things, on pre-Lollipop devices, parses the VectorDrawables XML files and convert them into VectorDrawableCompat instances that can be used all the way down to API 7.

然后,一旦有了 VectorDrawableCompat 实例,就将其栅格化到位图上.稍后,您将在远程 ImageView 中使用此位图.

Then, once you have a VectorDrawableCompat instance, rasterize it onto a Bitmap. You'll later use this bitmap in a remote ImageView.


确保您使用的是Android Studio 2.0+,并已按照以下步骤配置了应用程序 build.gradle 文件:

Ensure you are using Android Studio 2.0+ and have configured your app build.gradle file as follows:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

dependencies {
  compile 'com.android.support:appcompat-v7:23.3.0'
}


首先:不要在 RemoteViews 布局文件中设置矢量可绘制资源(android:srcapp:srcCompat都不起作用).您必须以编程方式设置它们.

First of all: don't set your vector drawable resources inside your RemoteViews layout file (neither android:src nor app:srcCompat will work). You'll have to set them programmatically.

在您的 AppWidgetProvider 类内部,根据API级别设置矢量资源或栅格化版本:

Inside your AppWidgetProvider class set the vector resource or a rasterized version depending on the API level:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  remoteViews.setImageViewResource(R.id.imageView, R.drawable.vector);

} else {
  Drawable d = ContextCompat.getDrawable(context, R.drawable.vector);
  Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(),
                                 d.getIntrinsicHeight(),
                                 Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(b);
  d.setBounds(0, 0, c.getWidth(), c.getHeight());
  d.draw(c);
  remoteViews.setImageViewBitmap(R.id.imageView, b);
}


  • ContextCompat source code
  • Chris Banes' AppCompat v23.2 - Age of the vectors blog post introduces VectorDrawableCompat and explains the hack that AppCompat uses to make them work on pre-Lollipop devices.

这篇关于AppCompat 23.2将VectorDrawableCompat与API< 21上的RemoteViews(AppWidget)一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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