如何动画进度通知图标

How to animate the progress notification icon

我正在按照下面给出的方式在服务中构建通知进度条:

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

它工作正常,通知和进度条显示一切正常,我想要通知中的简单进度条,它很简单,我对此没有问题,但我想要的如下:

我想要的:

I want that My notification Icon should animate like the google play store app does, it animates its icon to show that download in progress. I want to do the same. Please tell me by using builder and notification manager How can I get this , I have seen many tutorial but they are deprecated.

使用以下代码创建 .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到5是轮子的图片。确保所有图像都在您的可绘制文件夹中。

用户将下面的代码片段添加到您的代码中以制作图标动画:

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

您可以按照 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);

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

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