Closed krawa closed 8 years ago
I do not think it is possible to use vector drawables in notifications because notification icons are handled by system and cannot be intercepted. Workaround is to use PNG for this particular drawable. For large icon you could convert vector drawable to a bitmap via BitmapUtil and to setLargeIcon.
Where else can not use BetterVectorDrawable?
What places do you mean?
Notifications and maybe widgets. What relates to RemoteViews.
You may try to inherit from the RemoteViews
class and override the apply
method:
@Override
public View apply(Context context, ViewGroup parent) {
VectorDrawableCompat.enableResourceInterceptionFor(context.getResources(),
R.drawable.airplane_vector,
R.drawable.bicycle_vector,
R.drawable.lamp_vector,
R.drawable.rocket_vector);
return super.apply(context, parent);
}
Presumably this enables resource interception. But I did not test it.
Did this help?
Sorry, I did not try. I do not use the BetterVectorDrawable in my project.
I got same problem and experimented with workarounds you mentioned. One from Nov 26 did not work for me and felt a bit wrong. I ended up using BitmapUtil. Here is a snippet:
public ClassConstructor(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
this.metrics = context.getResources().getDisplayMetrics();
}
private void createRemoteView() {
expandedRemoteView = new RemoteViews(packageName, R.layout.notification);
// some initialization code
setImage(
expandedRemoteView,
R.id.player_notification_big_jump_back,
R.drawable.vector_notification_backward
);
// some more code
}
private void setImage(RemoteViews remoteView, @IdRes int imageViewId, @DrawableRes int imageId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
remoteView.setImageViewResource(imageViewId, imageId);
} else {
remoteView.setImageViewBitmap(
imageViewId,
BitmapUtil.toBitmap(ContextCompat.getDrawable(context, imageId), metrics)
);
}
}
In my case I use RemoteViews for custom layout in notification, but it should be possible to add notification icon in similar way.
siga111, thank you for providing solution!
I get an crash when I use the BetterVectorDrawable in Notifications
StackTrace