How to animate the progress notification icon

前端 未结 3 1702
夕颜
夕颜 2020-12-18 08:34

I am building the notification progress bar in Service in such a way that is given below :

private NotificationManager mNotifyManager;
private NotificationCo         


        
相关标签:
3条回答
  • 2020-12-18 09:11

    Create .xml file with below code:

    <!-- 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>
    

    Where wheel0 to 5 are the images of the wheel. Make sure all images are in your drawable folder.

    User below snippet into your code to animate icon:

    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

    0 讨论(0)
  • 2020-12-18 09:13

    you can make the animation list as described above in the answer of Rushab patel but there could be problem if you are using the newst api and if you are targetting the new sdk .

    so this following line would become outdated and deprecated

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

    which is too bad in coding notification , I must suggest you as you have described above that you are using notification builder , thus I would recomend you to directly use this animation list drawable in the notification builder.

    from your snippet

    mBuilder =
                        new NotificationCompat.Builder(DownloadService.this)
                                .setSmallIcon(R.drawable.youAnimationList)
                                .setContentTitle("MayUp")
                                .setContentText("Downloading new tools")
                                .setTicker("Downloading in progress");
                mBuilder.setAutoCancel(true);
    
    0 讨论(0)
  • 2020-12-18 09:28

    By using this code you can display a notification for downloading some files with a animated icon:

    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();
    
    0 讨论(0)
提交回复
热议问题