Animation in Notification bar Custom View
Asked Answered
S

2

11

As far as I know we can create notifications in Android using Notification Manager + remote Views.

I am Creating a notification for downloading Mp3 files. And I want an animations beside It. So far I have learned from forums that it is not possible.

However I saw A video for an android App which downloads and displays animation beside it while downloading it. Link: http://www.youtube.com/watch?v=yNcs-sS2nFU&feature=related

Can someone tell me the best way to achieve it.

Sarson answered 14/1, 2011 at 4:43 Comment(1)
The video looks like it was from Froyo or Gingerbread. You should check out the NotificationBuilder class which lets you set a progress value to achieve the horizontal bar you see in the video.Tirade
E
26

The best way I have found to show a custom animation in a notification is to use an AnimationDrawable as a resource with an ID. Then simply specify the drawable resource ID when you post your notification. No further code is needed to update each frame of the animation. The animation drawable handles that for you.

Here is a link to documentation: http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

So for example you would need to:

  1. Add an xml file (such as "wheelAnim.xml") to your res/drawable/ folder with the following contents:

    <!-- 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>
    
  2. Add each drawable reference in the xml file you just created for the animation-list (be it PNG or other image format) in the res/drawable/ folder as well.

  3. Use the resource ID of the animation-list (which in this example is "R.drawable.wheelAnim") in your code. For example:

    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);
    
Endocrinology answered 6/1, 2012 at 21:46 Comment(3)
I have tried animated drawables in the newer NotificationBuilder class as the small icon, and it looks great in the status bar. However, when you pull down the notification drawer, the icon is NOT animated. Has anybody had any experience with this, and did you have any success making the icon animated in the pulled down view?Tirade
Where can I get those wheel images part?Protolanguage
@Tirade are you sure its not the large icon in the notification when you pull down?Aleta
B
2

In the documentation for creating status bar notifications, it says you can cycle through a bunch of images that are defined in a LevelListDrawable by changing the iconLevel property of the the Notification class:

iconLevel field

This value indicates the current level of a LevelListDrawable that is used for the notification icon. You can animate the icon in the status bar by changing this value to correlate with the drawable's defined in a LevelListDrawable. See the LevelListDrawable reference for more information.

Braunstein answered 17/1, 2011 at 19:21 Comment(1)
I am aware of that but My issue is animating the remoteView object. I am doing Is sending notifications again and again which gives animation effect. But the thread posts after 3000ms, and if I decrease the value to below 2000ms. My emulator hangs after a few update, and I expect the device to react in the same way. The video shows something very interesting It animates an object with 100ms - 500ms delays. That surprises me.Sarson

© 2022 - 2024 — McMap. All rights reserved.