简体   繁体   English

Android主屏幕小部件动画

[英]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. 我正在寻找创建一个支持动画的小部件,理想情况下通过android.view.animation框架,否则通过在后台服务触发的代码中设置远程视图的属性。

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? 有没有人对这两种方法都有任何经验,而且我正在尝试做什么,或者我是否正在走向死胡同?

It's actually possible to animate RemoteView widgets. 实际上可以为RemoteView小部件设置动画。 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. 我的意思是Android只能使用通过xml绑定到布局的res / anim xml文件中表示的动画。 Some RemoteView widgets support this 一些RemoteView小部件支持这一点

An example of this is the News and Weather app widget that comes on a stock android system. 这方面的一个例子是股票Android系统上的新闻和天气应用程序小部件。 What it is doing is using a ViewFlipper to cycle through each news story every 10 seconds or so. 它正在做的是使用ViewFlipper每10秒左右循环一次每个新闻报道。

    <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. 在此示例中,您可以将挂起的意图绑定到每个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. 最后,Android在每个版本中都慢慢添加了对动画视图的支持。 For example TransitionDrawables (cross fading selector drawable) don't cross-fade until Android 3.0. 例如,TransitionDrawables(交叉渐变选择器drawable)在Android 3.0之前不会交叉淡入淡出。

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). 你可以在你的小部件上只有像fadeIn或fadeOut这样的简单动画,它非常简单,你不需要任何布局动画,只需使用ViewFlipper(我花了3天时间调查才发现它很容易)。

Yet it's imposiible to write something really great without using custom launchers 然而,如果不使用自定义发射器来编写非常棒的东西是不可能的

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 Mario Coin Block小部件中 ,我使用这种技术来制作动画,您可以查看源代码: http//code.google.com/p/mario-coin-block/source/browse/trunk/MarioWidget。 coinBlock / src目录/ COM / gueei /马里奥/ coinBlock /查看/ CoinBlockView.java

Basically the idea is manually draw on an offscreen Bitmap, and replace the BitmapView's bitmap with it using RemoveViews Call. 基本上这个想法是在屏幕外的Bitmap上手动绘制,并使用RemoveViews Call替换BitmapView的位图。

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 缺点 - 不是互动,取代了用户的壁纸......而且很难满足每个人的口味

Animations are impossible on RemoteViews, and RemoteViews updates can occur at a rate of once every 30 minutes (or manually)... RemoteViews上无法进行动画制作,RemoteViews更新可以每30分钟(或手动)一次的速度发生......

Anyway, you can try the following link: is-there-a-way-to-animate-on-a-home-widget 无论如何,您可以尝试以下链接: is-there-a-way-to-animate-on-a-home-widget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM