android - 在 android 通知中显示带 Glide 的 GIF

标签 android android-notifications gif android-glide remoteview

我一直在尝试使用 Glide 库在我的自定义通知中显示 .gif 图像。我只能在通知面板上显示静态图像。

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

可绘制对象是一个 .gif 文件。当通知弹出时,它会显示静态图像,因为我正在使用 .asBitmap()。我尝试使用

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

但我收到错误“无法将方法解析为(NotificationTarget)”。 我四处寻找解决方案,但找不到。 那么如何在android的通知面板中显示带有 Glide 的GIF文件呢?可能吗?

最佳答案

Android 不支持通知中的 GIF 和视频,因为通知是使用 RemoteViewsRemoteViews 构建的 doesn't support GIF or Videos .

此外,Glide 维护者也回答了类似的问题 here .我引用的是答案。

It doesn't look like RemoteViews supports setting a Drawable? 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.

关于android - 在 android 通知中显示带 Glide 的 GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58482692/

相关文章:

c++ - 文本框隐藏在gif动画后面

java - 使用 java 将透明 gif/png 转换为 jpeg

java - 为什么抛出 RuntimeException 错误而不是 IOException 消息?

android - 如何以编程方式创建 ScrollView ?

android - 如何在 ParseUser 对象中查找用户

java - Android 8.+ 上不显示前台服务通知

java - NotificationManager 和 NotificationManagerCompat 之间有什么区别?

java - 尝试将背景从白色更改为黑色,但无法运行此 timer() 方法,屏幕只有白色

android - 有没有一种方法可以让应用程序检查它是否被允许访问通知?

linux - 如何增加gif图像的分辨率?