在 Android 中启动 AnimationDrawable
我应该在哪里启动一个在显示活动时需要动画的 AnimationDrawable
?
开发者指南建议使用onWindowFocusChanged
,但当 Activity 是 TabHost
的一部分时,并不总是调用此函数。
我引用:
需要注意的是, 调用start()方法 无法调用AnimationDrawable 在你的 onCreate() 方法中 活动,因为 AnimationDrawable 尚未完全完成 贴在窗户上。如果你想 立即播放动画, 不需要交互,那么 你可能想从 onWindowFocusChanged() 方法在你的 Activity,当 Android 使您的窗口成为焦点。
Where should I start an AnimationDrawable
that needs to animate when the activity is shown?
The developer guide recommends using onWindowFocusChanged
, but this isn't always called when the activity is part of a TabHost
.
I quote:
It's important to note that the
start() method called on the
AnimationDrawable cannot be called
during the onCreate() method of your
Activity, because the
AnimationDrawable is not yet fully
attached to the window. If you want to
play the animation immediately,
without requiring interaction, then
you might want to call it from the
onWindowFocusChanged() method in your
Activity, which will get called when
Android brings your window into focus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这个问题有点老了,但这可能对像我一样遇到这个问题的人有帮助。我启动 AnimationDrawable 的一种方法是创建一个新的 Runnable 并使用 ImageView 中的 post 方法。
你可以这样做:
I know this question is a little bit old, but this may be helpful to someone happening across this question like I did. One way that I start my AnimationDrawable's is by creating a new Runnable and using the post method from the ImageView.
You can do like:
并行线程方法似乎是最流行的一种,但它确实提出了 2 个主要问题:
因此,我建议使用名称非常恰当的 runOnUiThread() 方法。在 onResume() 中调用它将确保您的动画代码将在主线程上运行,它将在附加窗口后运行,您会知道消息将在哪里被处理,并且小猫不需要丢失它们的信息生活:
The parallel thread approach seems to be the most popular one, but it does raise 2 major issues:
So, I suggest using the very aptly named runOnUiThread() method. Calling it in onResume() will assure you that your animation code will run on the main thread, that it would run after the window is attached, you'd know where exactly the message is about to be processed and no kittens need to lose their lives:
当 Activity 进入前台时,始终会调用 Activity 的
onResume()
。尝试从那里启动它。The Activity's
onResume()
is always called when the Activity comes to the foreground. Try starting it in there.根据文档,您必须等到视图附加到窗口才能开始动画。因此,您应该向视图添加一个 OnAttachStateChangeListener ,该视图将在附加时执行,并从那里开始动画。
According to the documentation, you must wait until the view is attached to the window before starting animation. Therefor, you should add an
OnAttachStateChangeListener
to the view that will execute when it has been attached, and start the animation from there.