Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

android - AppCompat 23.2 use VectorDrawableCompat with RemoteViews (AppWidget) on API<21

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.

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

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE 22/10/2017

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

UPDATE 05/09/2016

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!

VectorDrawable and RemoteViews pre-Lollipop

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 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.

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


Before we begin: AppCompat Library

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'
}


Update your AppWidgetProvider

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.

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);
}


References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...