Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android homescreen widget animations

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?

like image 896
Adrian Avatar asked Jul 29 '10 13:07

Adrian


People also ask

Can you have animated widgets?

There are some more animated widgets available like swing, spin, dancing widget, etc.

Can I download more widgets for Android?

Android widgets are mini mobile apps that run on your device's home screen. Your device includes several pre-loaded widgets, but you can download more from Google Play. Some phone manufacturers offer widgets exclusively for their devices. For example, you can download Samsung widgets from the Galaxy Store.


3 Answers

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.

like image 99
Jeremy Edwards Avatar answered Oct 08 '22 17:10

Jeremy Edwards


It's possible, but use it with caution, since it is very heavy to the default homescreen implementation and you shouldn't use it very often.

In the Mario Coin Block widget, I am using such technique to do the animation, you can checkout the source code: http://code.google.com/p/mario-coin-block/source/browse/trunk/MarioWidget.CoinBlock/src/com/gueei/mario/coinBlock/view/CoinBlockView.java

Basically the idea is manually draw on an offscreen Bitmap, and replace the BitmapView's bitmap with it using RemoveViews Call.

like image 40
xandy Avatar answered Oct 08 '22 17:10

xandy


I agree with the other answers here, so I won't re-iterate - limited animation on a widget is possible, but heavy on resources, it might make the home screen slow and less responsive, and battery drainer. From my experience - it doesn't run smooth. So the bottom line is - it's ok if it's only few frames changing from time to time, or for some effects seldom upon an event (for example user press or some event from your service.

But here's an idea that probably does not directly answer your question, but may be a suitable alternative (I don't know your use case, it might be not relevant at all) Did you consider implementing a live wallpaper?

pros - highest quality animation, can be controlled from the background

cons - not interactive, replaces the user's wallpaper... and it's hard to satisfy everyone's taste

like image 42
Amir Uval Avatar answered Oct 08 '22 18:10

Amir Uval