在 Android 中启动 AnimationDrawable

发布于 2024-09-10 15:58:53 字数 539 浏览 2 评论 0原文

我应该在哪里启动一个在显示活动时需要动画的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦初启 2024-09-17 15:58:53

我知道这个问题有点老了,但这可能对像我一样遇到这个问题的人有帮助。我启动 AnimationDrawable 的一种方法是创建一个新的 Runnable 并使用 ImageView 中的 post 方法。

你可以这样做:

ImageView spinner = (ImageView) findViewById(R.id.my_imageView);
spinner.setBackgroundResource(R.drawable.spinner);
spinner.post(new Runnable() {
    public void run() {
        AnimationDrawable anim = (AnimationDrawable) spinner.getBackground();
        anim.start();
    }
});

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:

ImageView spinner = (ImageView) findViewById(R.id.my_imageView);
spinner.setBackgroundResource(R.drawable.spinner);
spinner.post(new Runnable() {
    public void run() {
        AnimationDrawable anim = (AnimationDrawable) spinner.getBackground();
        anim.start();
    }
});
眼眸里的快感 2024-09-17 15:58:53

并行线程方法似乎是最流行的一种,但它确实提出了 2 个主要问题:

  • 根据文档,所有 UI 相关代码都应该在主(又名“GUI”)线程上运行。虽然在 AnimationDrawable 上调用 .start() 可能不会被视为纯 UI 操作,但我仍然认为它应该遵循该规则。
  • 您永远无法确切知道动画何时开始。我见过带有“神奇”延迟长度值的代码,这些值应该可以解决这个问题。你应该知道,每当程序员采取这种方法时,上帝就会杀死一只小猫宝宝。

因此,我建议使用名称非常恰当的 runOnUiThread() 方法。在 onResume() 中调用它将确保您的动画代码将在主线程上运行,它将在附加窗口后运行,您会知道消息将在哪里被处理,并且小猫不需要丢失它们的信息生活:

@Override
protected void onResume()
{
    super.onResume();
    runOnUiThread(new Runnable() 
    {
        @Override
        public void run() 
        {
            animation.start();
        }
    });
}

The parallel thread approach seems to be the most popular one, but it does raise 2 major issues:

  • According to the docs, all UI related code should run on the main (a.k.a "GUI") thread. While calling .start() on an AnimationDrawable might not be considered a pure UI operation, I still feel that it should follow that rule.
  • You can never know exactly when will your animation start. I've seen code with "magic" delay length values that were supposed to fix that. You should know tht God kills a baby kitten every time a programmer takes that approach.

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:

@Override
protected void onResume()
{
    super.onResume();
    runOnUiThread(new Runnable() 
    {
        @Override
        public void run() 
        {
            animation.start();
        }
    });
}
孤寂小茶 2024-09-17 15:58:53

当 Activity 进入前台时,始终会调用 Activity 的 onResume()。尝试从那里启动它。

The Activity's onResume() is always called when the Activity comes to the foreground. Try starting it in there.

小ぇ时光︴ 2024-09-17 15:58:53

根据文档,您必须等到视图附加到窗口才能开始动画。因此,您应该向视图添加一个 OnAttachStateChangeListener ,该视图将在附加时执行,并从那里开始动画。

ImageView loadingImg = (ImageView)v.findViewById(R.id.image);
loadingImg.setBackgroundResource(R.drawable.progressdialog);
loadingImg.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
    AnimationDrawable loadingAnimation = (AnimationDrawable) v.getBackground();
    loadingAnimation.start();
  }

  @Override
  public void onViewDetachedFromWindow(View v) {
  }
});

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.

ImageView loadingImg = (ImageView)v.findViewById(R.id.image);
loadingImg.setBackgroundResource(R.drawable.progressdialog);
loadingImg.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
    AnimationDrawable loadingAnimation = (AnimationDrawable) v.getBackground();
    loadingAnimation.start();
  }

  @Override
  public void onViewDetachedFromWindow(View v) {
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文