我们可以使用VectorDrawable或VectorXML作为Android中推送通知的图标吗?

时间:2022-11-21 12:11:12

I am using PNG image but its size is getting too big so I have to compromise with its quality. So i was thinking vectors may be the another way around ? Example will be a great help.

我正在使用PNG图像,但它的尺寸太大,所以我不得不妥协它的质量。所以我认为矢量可能是另一种方式?示例将是一个很大的帮助。

4 个解决方案

#1


8  

Can we use VectorDrawable or VectorXML as icons for push notifications?

我们可以使用VectorDrawable或VectorXML作为推送通知的图标吗?

Yes, just call the vector drawable the standard way for notifications:

是的,只需将vector drawable称为通知的标准方式:

.setSmallIcon(R.drawable.my_vector)

In order to use the transparency (notification icons are only white and/or transparent), you will have to use the alpha channels when settings the colors in the vector XML, meaning #00000000 for transparent and #FFFFFFFF for white.

为了使用透明度(通知图标只有白色和/或透明),在设置矢量XML中的颜色时必须使用alpha通道,这意味着#00000000表示透明,#FFFFFFFF表示白色。

#2


14  

VectorDrawables will only work as notification icons for versions higher than (or equal to) Android Lollipop - i.e. API 21.

VectorDrawables仅作为高于(或等于)Android Lollipop版本的通知图标 - 即API 21。

I know this because I did try using .setSmallIcon(R.drawable.my_vector) as shown in one of the other answers here, and although this works perfectly fine for API 21 and above, I got the following error for versions prior to Lollipop:

我知道这是因为我确实尝试使用.setSmallIcon(R.drawable.my_vector),如此处的其中一个答案中所示,虽然这对于API 21及更高版本的效果非常好,但我对Lollipop之前的版本有以下错误:

android.app.RemoteServiceException: Bad notification posted from package com.example.app: Couldn't create icon: StatusBarIcon(pkg=com.example.appuser=0 id=0x7f02005a level=0 visible=true num=0 )

android.app.RemoteServiceException:从com.example.app包发布的错误通知:无法创建图标:StatusBarIcon(pkg = com.example.appuser = 0 id = 0x7f02005a level = 0 visible = true num = 0)

There are also other answers on Stack Overflow supporting this argument:

Stack Overflow上还有其他答案支持这个论点:

#3


0  

Probably you should not use VectorDrawable icons in notifications if you are using vector drawable support package - you may encounter errors on pre-lollipop devices.

如果您使用矢量绘图支持包,可能不应在通知中使用VectorDrawable图标 - 您可能会在棒棒糖前设备上遇到错误。

Check out this: Notification throws error when using vector drawables

看看这个:使用向量drawable时通知会抛出错误

Wuthout vector drawable support package, I didn't encounter any errors but after using it, pre-lollipop devices were unable to access the vector icon at the time of notification and threw this error:

Wuthout矢量drawable支持包,我没有遇到任何错误,但在使用它之后,前棒棒糖设备在通知时无法访问矢量图标并抛出此错误:

android.app.RemoteServiceException: Bad notification posted from package com.xxx.xxx: Couldn't create icon: StatusBarIcon(pkg=com.xxx.xxxuser=0 id=0x7f020082 level=0 visible=true num=0)

android.app.RemoteServiceException:从包com.xxx.xxx发布的错误通知:无法创建图标:StatusBarIcon(pkg = com.xxx.xxxuser = 0 id = 0x7f020082 level = 0 visible = true num = 0)

#4


0  

For version < 21,

对于版本<21,

If you want to directly pass in vector drawable resource id into setSmallIcon(): No way.

如果你想直接将vector drawable资源id传递给setSmallIcon():没办法。

For setLargeIcon() indirectly, yes. Use

对于setLargeIcon()间接,是的。使用

VectorDrawableCompat drawable = VectorDrawableCompat.create(context.getResources(), resource id, theme);

then create Bitmap from this drawable and pass into setLargeIcon()

然后从这个drawable创建Bitmap并传入setLargeIcon()

#1


8  

Can we use VectorDrawable or VectorXML as icons for push notifications?

我们可以使用VectorDrawable或VectorXML作为推送通知的图标吗?

Yes, just call the vector drawable the standard way for notifications:

是的,只需将vector drawable称为通知的标准方式:

.setSmallIcon(R.drawable.my_vector)

In order to use the transparency (notification icons are only white and/or transparent), you will have to use the alpha channels when settings the colors in the vector XML, meaning #00000000 for transparent and #FFFFFFFF for white.

为了使用透明度(通知图标只有白色和/或透明),在设置矢量XML中的颜色时必须使用alpha通道,这意味着#00000000表示透明,#FFFFFFFF表示白色。

#2


14  

VectorDrawables will only work as notification icons for versions higher than (or equal to) Android Lollipop - i.e. API 21.

VectorDrawables仅作为高于(或等于)Android Lollipop版本的通知图标 - 即API 21。

I know this because I did try using .setSmallIcon(R.drawable.my_vector) as shown in one of the other answers here, and although this works perfectly fine for API 21 and above, I got the following error for versions prior to Lollipop:

我知道这是因为我确实尝试使用.setSmallIcon(R.drawable.my_vector),如此处的其中一个答案中所示,虽然这对于API 21及更高版本的效果非常好,但我对Lollipop之前的版本有以下错误:

android.app.RemoteServiceException: Bad notification posted from package com.example.app: Couldn't create icon: StatusBarIcon(pkg=com.example.appuser=0 id=0x7f02005a level=0 visible=true num=0 )

android.app.RemoteServiceException:从com.example.app包发布的错误通知:无法创建图标:StatusBarIcon(pkg = com.example.appuser = 0 id = 0x7f02005a level = 0 visible = true num = 0)

There are also other answers on Stack Overflow supporting this argument:

Stack Overflow上还有其他答案支持这个论点:

#3


0  

Probably you should not use VectorDrawable icons in notifications if you are using vector drawable support package - you may encounter errors on pre-lollipop devices.

如果您使用矢量绘图支持包,可能不应在通知中使用VectorDrawable图标 - 您可能会在棒棒糖前设备上遇到错误。

Check out this: Notification throws error when using vector drawables

看看这个:使用向量drawable时通知会抛出错误

Wuthout vector drawable support package, I didn't encounter any errors but after using it, pre-lollipop devices were unable to access the vector icon at the time of notification and threw this error:

Wuthout矢量drawable支持包,我没有遇到任何错误,但在使用它之后,前棒棒糖设备在通知时无法访问矢量图标并抛出此错误:

android.app.RemoteServiceException: Bad notification posted from package com.xxx.xxx: Couldn't create icon: StatusBarIcon(pkg=com.xxx.xxxuser=0 id=0x7f020082 level=0 visible=true num=0)

android.app.RemoteServiceException:从包com.xxx.xxx发布的错误通知:无法创建图标:StatusBarIcon(pkg = com.xxx.xxxuser = 0 id = 0x7f020082 level = 0 visible = true num = 0)

#4


0  

For version < 21,

对于版本<21,

If you want to directly pass in vector drawable resource id into setSmallIcon(): No way.

如果你想直接将vector drawable资源id传递给setSmallIcon():没办法。

For setLargeIcon() indirectly, yes. Use

对于setLargeIcon()间接,是的。使用

VectorDrawableCompat drawable = VectorDrawableCompat.create(context.getResources(), resource id, theme);

then create Bitmap from this drawable and pass into setLargeIcon()

然后从这个drawable创建Bitmap并传入setLargeIcon()