简体   繁体   English

在 android 通知中显示带有滑动的 GIF

[英]Show GIF with glide in android notification

I have been trying to show a.gif image inside my custom notification using Glide library.我一直在尝试使用 Glide 库在我的自定义通知中显示 a.gif 图像。 I am only able to show static image on the notification panel.我只能在通知面板上显示 static 图像。

    RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.notification_collapsed);
    RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);

    collapsedView.setTextViewText(R.id.text_view_collapsed_1,"Hello World!");


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(collapsedView)
            .setCustomBigContentView(expandedView);

    final Notification notification = mBuilder.build();


    notificationManager.notify(NOTIFICATION_ID,notification);

    notificationTarget = new NotificationTarget(
            getApplicationContext(),
            R.id.image_view_expanded,
            expandedView,
            notification,
            NOTIFICATION_ID
            );

    Glide.with(MainActivity.this)
            .asBitmap()
            .load(R.drawable.giphy)
            .into(notificationTarget);

The drawable is a.gif file. drawable 是一个 .gif 文件。 When the notification pops up it shows a static image as I am using.asBitmap().当通知弹出时,它会显示 static 图像,因为我正在使用.asBitmap()。 I tried using我尝试使用

Glide.with(MainActivity.this)
            .asGif()
            .load(R.drawable.giphy)
            .into(notificationTarget);

but i am getting an error "Cannot resolve method 'into(NotificationTarget)'".但我收到一个错误“无法解析方法'into(NotificationTarget)'”。 I have looked around for solutions but i was unable to find one.我四处寻找解决方案,但我找不到。 So how can I show a GIF file with glide in android's notification panel?那么如何在 android 的通知面板中显示带有 glide 的 GIF 文件呢? Is it possible?可能吗?

Android doesn't support GIF and Videos in notification because notification is built using RemoteViews and RemoteViews doesn't support GIF or Videos . Android 不支持通知中的 GIF 和视频,因为通知是使用RemoteViews构建的,而RemoteViews 不支持 GIF 或视频

Also, Glide maintainer answered a similar question here .此外,Glide 维护者在这里回答了一个类似的问题。 I am quoting the answer.我在引用答案。

It doesn't look like RemoteViews supports setting a Drawable?看起来 RemoteViews 不支持设置 Drawable? https://developer.android.com/reference/android/widget/RemoteViews.html . https://developer.android.com/reference/android/widget/RemoteViews.html

If there's a way to set an animated drawable in a notification in general, you can probably find a way to do it with a custom Target in Glide.如果有一种方法可以在通知中设置动画可绘制对象,那么您可能会找到一种方法来使用 Glide 中的自定义 Target。

Indeed.的确。 @Somesh Kumar answered your question. @Somesh Kumar 回答了你的问题。

You cannot add GIF's or Videos like he said to your Notifications您不能像他所说的那样将 GIF 或视频添加到您的通知中

In fact, I must ask you: Have you ever seen any notification on your phone that was actively playing a Video or a GIF?事实上,我必须问你:你有没有在手机上看到任何正在播放视频或 GIF 的通知?

Notifications are not meant to play Videos or GIFs as that would bring some issues as for example higher battery consumption.通知并不意味着播放视频或 GIF,因为这会带来一些问题,例如更高的电池消耗。 Notifications should be simple and fast to understand.通知应该简单易懂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM