【问题标题】:Animation in Notification bar Custom View通知栏自定义视图中的动画
【发布时间】:2011-01-14 04:43:59
【问题描述】:

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

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

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

谁能告诉我实现它的最佳方法。

【问题讨论】:

  • 视频看起来像是来自 Froyo 或 Gingerbread。您应该查看 NotificationBuilder 类,该类可让您设置进度值以实现您在视频中看到的水平条。

标签: android animation widget android-layout push-notification


【解决方案1】:

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

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

例如,您需要:

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

    <!-- 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. 在您刚刚为res/drawable/ 文件夹中的animation-list(无论是PNG 还是其他图像格式)创建的xml 文件中添加每个可绘制引用。

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

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

【讨论】:

  • 我已经在新的 NotificationBuilder 类中尝试了动画绘图作为小图标,它在状态栏中看起来很棒。但是,当您拉下通知抽屉时,图标不是动画的。有没有人有这方面的经验,你有没有成功地在下拉视图中制作图标动画?
  • 我在哪里可以获得这些车轮图像部分?
  • @iHearGeoff 确定不是下拉时通知中的大图标?
【解决方案2】:

在用于创建状态栏通知的documentation 中,它说您可以通过更改Notification 类的iconLevel 属性来循环浏览LevelListDrawable 中定义的一堆图像:

iconLevel 字段

该值表示 LevelListDrawable 的当前级别,用于 通知图标。您可以通过将此值更改为 与LevelListDrawable 中定义的drawable 相关。查看 LevelListDrawable 参考以获取更多信息。

【讨论】:

  • 我知道这一点,但我的问题是为 remoteView 对象设置动画。我正在一次又一次地发送通知,这会产生动画效果。但是线程在 3000 毫秒后发布,如果我将值降低到 2000 毫秒以下。我的模拟器在几次更新后挂起,我希望设备以同样的方式做出反应。该视频展示了一些非常有趣的东西它以 100 毫秒 - 500 毫秒的延迟为对象设置动画。这让我很吃惊。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
相关资源
最近更新 更多