Android homescreen widget animations

Adrian picture Adrian · Jul 29, 2010 · Viewed 14.3k times · Source

I'm looking into creating a widget that supports animation, ideally via the android.view.animation framework, otherwise by setting properties on the remote views in code triggered from a background service.

Does anyone have any experience with either of these approaches, and is what I'm trying doable, or am I heading up a blind alley?

Answer

Jeremy Edwards picture Jeremy Edwards · Feb 13, 2012

It's actually possible to animate RemoteView widgets. The problem is it is super restrictive which is by design because of the security implications of running custom code in a system process.

What I mean by this is that Android will only work with animations that are expressed in res/anim xml files that are tied to layouts via xml. Some RemoteView widgets support this

An example of this is the News and Weather app widget that comes on a stock android system. What it is doing is using a ViewFlipper to cycle through each news story every 10 seconds or so.

    <ViewFlipper android:layout_width="match_parent" android:layout_height="wrap_content" android:measureAllChildren="true" android:flipInterval="10000" android:autoStart="true"
android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out" android:animateFirstView="true">
      <TextView android:id="@+id/Description1TextView" style="@style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
      <TextView android:id="@+id/Description2TextView" style="@style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
      <TextView android:id="@+id/Description3TextView" style="@style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
      <TextView android:id="@+id/Description4TextView" style="@style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
    </ViewFlipper>

In this example you can tie pending intents to each TextView. So when a user clicks on any one a different action can occur.

Lastly, Android has been slowly adding support for animated views in each version. For example TransitionDrawables (cross fading selector drawable) don't cross-fade until Android 3.0.