Android homescreen widget animations

2019-03-11 16:10发布

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?

5条回答
混吃等死
2楼-- · 2019-03-11 16:38

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

查看更多
做自己的国王
3楼-- · 2019-03-11 16:41

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.

查看更多
该账号已被封号
4楼-- · 2019-03-11 16:42

You can have only simple animations like fadeIn or fadeOut on your widget, it's very simple, you don't need any layout animations, just use ViewFlipper(it took me 3 days of investigation to find out that it is so easy).

Yet it's imposiible to write something really great without using custom launchers

查看更多
来,给爷笑一个
5楼-- · 2019-03-11 16:47

Animations are impossible on RemoteViews, and RemoteViews updates can occur at a rate of once every 30 minutes (or manually)...

Anyway, you can try the following link: is-there-a-way-to-animate-on-a-home-widget

查看更多
虎瘦雄心在
6楼-- · 2019-03-11 16:50

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.

查看更多
登录 后发表回答