如何为进度通知图标添加动画效果

4
我正在按照以下方式构建服务中的通知进度条:
private NotificationManager mNotifyManager;
private NotificationCompat.Builder mBuilder;



 mBuilder =
                    new NotificationCompat.Builder(DownloadService.this)
                            .setSmallIcon(R.drawable.download)
                            .setContentTitle("MayUp")
                            .setContentText("Downloading new tools")
                            .setTicker("Downloading in progress");
            mBuilder.setAutoCancel(true);
            Intent targetIntent = new Intent(DownloadService.this, ListClass.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(DownloadService.this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MyActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(targetIntent);
//            PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//            mBuilder.setContentIntent(contentIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);

            mNotifyManager.notify(1, mBuilder.build());

            mBuilder.setProgress(100, 0, false);
            mNotifyManager.notify(1, mBuilder.build());

一切正常,通知和进度条都显示得很好。我想要简单的进度条在通知中,它是简单的,我对此没有问题,但我想要的是:

我想要的:

我希望我的通知图标能像Google Play商店应用程序一样动画化,以显示下载进度。我想做同样的事情。请告诉我如何使用builder和notification manager来实现这一点,我已经看了很多教程,但它们已经过时了。


据我所知,按顺序推送具有相同ID的通知将更新同一通知。因此,您可以获得该行为。 - Emre Aktürk
我正在更新通知下面的进度条,我知道如何使用相同的ID推送更新,我想问一下如何仅动画化图标。 - Allay Khalil
3个回答

37

通过使用此代码,您可以显示一个带有动画图标的通知来下载一些文件:

mBuilder.setContentTitle("Downloading... ")
        .setContentText("Please wait...")
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setSmallIcon(android.R.drawable.stat_sys_download)  // here is the animated icon
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), android.R.drawable.stat_sys_download)).build();

5
如果有人需要上传图标,它是android.R.drawable.stat_sys_upload - Manohar
@ManoharReddy 天哪,你太棒了兄弟!我找了这么久终于找到了。非常感谢你! - DIRTY DAVE

6
使用以下代码创建 .xml 文件:
<!-- Animation frames are wheel0.png -- wheel5.png files inside the
 res/drawable/ folder -->
 <animation-list android:id="selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>

其中wheel0到wheel5是轮子的图片。确保所有图片都在drawable文件夹中。

将以下代码片段插入您的代码以使图标动画:

Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis());

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.setLatestEventInfo(this, getText(R.string.someTitle),getText(R.string.someText), pendingIntent);

((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(uid, notification);

Ref link


但是,Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis()); 已经被弃用。 - Allay Khalil
简单来说,这种通知方式已经过时了,建议使用通知构建器和管理器。 - Allay Khalil
1
另一种方法是维护一个计时器,每200毫秒更新一次通知图标并通知相同的通知ID。 mBuilder.setSmallIcon(R.drawable.ic_download); mNotifyManager.notify(0,mBuilder.build()); - Rushabh Patel

1
你可以按照Rushab Patel的回答中所描述的制作动画列表,但是如果你使用的是最新的API并且针对的是新的SDK,可能会出现问题。因此下面这行代码将变得过时并被弃用:

Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis());

这在编码通知时非常不好。我建议你如上所述使用通知构建器直接在其中使用这个动画列表可绘制对象。根据你提供的片段内容。
mBuilder =
                    new NotificationCompat.Builder(DownloadService.this)
                            .setSmallIcon(R.drawable.youAnimationList)
                            .setContentTitle("MayUp")
                            .setContentText("Downloading new tools")
                            .setTicker("Downloading in progress");
            mBuilder.setAutoCancel(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接