通知栏自定义视图中的动画

发布于 2024-10-11 23:21:21 字数 337 浏览 3 评论 0 原文

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

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

不过,我看到了一个 Android 应用程序的视频,该应用程序下载并在下载时在旁边显示动画。 链接:http://www.youtube.com/watch?v=yNcs -sS2nFU&feature=lated

有人可以告诉我实现它的最佳方法吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2024-10-18 23:21:21

我发现在通知中显示自定义动画的最佳方法是使用 AnimationDrawable 作为具有 ID 的资源。然后,只需在发布通知时指定可绘制资源 ID 即可。不需要进一步的代码来更新动画的每一帧。可绘制动画会为您处理该问题。

以下是文档链接: http://developer.android.com/ Reference/android/graphics/drawable/AnimationDrawable.html

例如,您需要:

  1. 将 xml 文件(例如“wheelAnim.xml”)添加到您的 res/drawable/ em> 文件夹包含以下内容:

    
     ;
        <项目 android:drawable="@drawable/wheel0" android:duration="50" />
        <项目 android:drawable="@drawable/wheel1" android:duration="50" />
        <项目 android:drawable="@drawable/wheel2" android:duration="50" />
        <项目 android:drawable="@drawable/wheel3" android:duration="50" />
        <项目 android:drawable="@drawable/wheel4" android:duration="50" />
        <项目 android:drawable="@drawable/wheel5" android:duration="50" />
    
    
  2. 在 xml 中添加每个可绘制对象引用您刚刚为 animation-list 创建的文件(无论是 PNG 还是其他图像格式)也位于 res/drawable/ 文件夹中。

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

    通知通知=新通知(R.drawable.wheelAnim,null,
        System.currentTimeMillis());
    
    PendingIntentendingIntent = PendingIntent.getActivity(this, 0,
        新的意图(),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,通知);
    

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);
    
被翻牌 2024-10-18 23:21:21

在用于创建状态栏通知的文档中,它说您可以循环浏览 LevelListDrawable 中定义的一堆图像 通过更改 iconLevel 属性“nofollow noreferrer”>通知类:

iconLevel字段

该值指示用于绘制的 LevelListDrawable 的当前级别。
通知图标。您可以通过将此值更改为动画状态栏中的图标
与 LevelListDrawable 中定义的可绘制对象相关联。查看 LevelListDrawable
参考以获取更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文