14

I want to use an animation on a Home page Widget, i.e. an AppWidgetProvider. I was hoping to use the "Frame Animation" technique:

http://developer.android.com/guide/topics/graphics/2d-graphics.html#frame-animation

which I've used successfully in an activity. But I can't translate that code to an AppWidgetProvider.

Basically, in an AppWidgetProvider, I create and work with a RemoteViews object, which AFAIK doesn't provide me with a method to get a reference to an ImageView in the layout for me to call start() on the animation. There is also not a handler or a callback for when the widget displays so I can make the start() call.

Is there another way this can be done? I suppose that I can probably do the animation on my own with very fast onUpdate() calls on the widget, but that seems awfully expensive.

David
  • 161
  • 1
  • 2
  • 4

5 Answers5

7

Do not animate app widgets, unless you write you own home screen app.

You are correct that you have no way to manipulate an AnimationDrawable or an Animation to have them work with an app widget.

You are also correct that "very fast onUpdate() calls on the widget...seems awfully expensive", because it is. Updates to app widgets involve inter-process communication, between your AppWidgetProvider and the process hosting the home screen. This system is designed for updates every 30 minutes or so, not 30 frames per second.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    If I want to create an animation button like refresh icon on the TripeAdvisor application,What should I do? – S.M_Emamian Mar 14 '15 at 11:39
  • Does this include simple animations that are inside the android API ? For example, a customized progressBar drawable that spins an image of the app? What's possible, and what's not possible? – android developer May 17 '15 at 14:57
  • @androiddeveloper: Basically, any animations that you can express in the layout resource you use for the `RemoteViews` should work. Anything that requires you to be calling methods on views, animations, etc. will not. Also, continuous animation may be viewed negatively by the user, for power and distraction reasons. – CommonsWare May 17 '15 at 15:12
  • @CommonsWare I am required to use an image that spins forever (for the loading phase of the widget, like when you start the device), like the progressBar . Is it possible? – android developer May 17 '15 at 16:11
  • 1
    @androiddeveloper: You are welcome to try an `ImageView` pointing to an `AnimationDrawable` resource. Or try an indefinite `ProgressBar`. I have tried neither of these. – CommonsWare May 17 '15 at 16:16
  • @CommonsWare I think the indefinite ProgressBar (as shown here: http://stackoverflow.com/a/26121696/878126 ) is better than AnimationDrawable, as it doesn't require frame-by-frame creation. It also works, BTW... :) – android developer May 18 '15 at 06:58
7

I am currently creating an widget that "need" sprite animation, and I have put a blog post on how to animate home widget. Yes, it is expensive to do, so I do it only when the widget is needed. BTW, original android animation is not supported in remote views.

Edit:

Demo Project is up.

xandy
  • 27,357
  • 8
  • 59
  • 64
6

Another option to animate a widget is the use of ViewFlipper, where one can use inAnimationand outAnimation:

    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:flipInterval="5000"
             android:autoStart="true"
             android:inAnimation="@android:anim/fade_in"
             android:outAnimation="@android:anim/fade_out"
             android:animateFirstView="true"/>
rwozniak
  • 1,326
  • 15
  • 15
6

One Widget that is available for use in a RemoteView is the ProgressBar. It will animate itself and will not chew up resources. An in-determinant ProgressBar which is a square will overlay quite well on a homescreen appwidget. See sample code from Android site

Yossi
  • 1,226
  • 1
  • 16
  • 31
5

Create custom animation. Create ProgressBar and set in android:indeterminateDrawable your animation. Add ProgressBar to your widget layout and make it visible(invisible)

Yomodo
  • 231
  • 4
  • 5