通知栏自定义视图中的动画 [英] Animation in Notification bar Custom View

查看:37
本文介绍了通知栏自定义视图中的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我们可以使用通知管理器 + 远程视图在 Android 中创建通知.

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

我正在创建下载 Mp3 文件的通知.我想要它旁边的动画.到目前为止,我从论坛了解到这是不可能的.

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.

但是,我看到了一个 android 应用的视频,该视频在下载时会下载并在其旁边显示动画.链接:http://www.youtube.com/watch?v=yNcs-sS2nFU&feature=related

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.

推荐答案

我发现在通知中显示自定义动画的最佳方式是使用 AnimationDrawable 作为具有 ID 的资源.然后在发布通知时简单地指定可绘制资源 ID.不需要进一步的代码来更新动画的每一帧.动画 drawable 会为您处理.

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.

这里是文档的链接:http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

例如,您需要:

  1. 将一个 xml 文件(例如wheelAnim.xml")添加到您的 res/drawable/ 文件夹中,其中包含以下内容:

  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>

  • 在您刚刚为 animation-list(无论是 PNG 还是其他图像格式)创建的 xml 文件中添加每个 drawable 引用到 res/drawable/ 文件夹.

  • 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.

    在您的代码中使用动画列表的资源 ID(在本例中为R.drawable.wheelAnim").例如:

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

  • 这篇关于通知栏自定义视图中的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆