旋转后framgent中的动画不会重新启动

发布于 2024-12-18 13:30:53 字数 749 浏览 3 评论 0原文

我有一个在第一次显示片段时正确启动的动画。但是,方向改变后,它不会重新启动。动画是设置为 ImageView 背景的动画列表资源。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View root = inflater.inflate(R.layout.fragment_lead_manual,
            container, false);
    final ImageView badgeEntryView = (ImageView) root
            .findViewById(R.id.manual_image);
    mAnimation = (AnimationDrawable) badgeEntryView.getBackground();
    return root;
}

@Override
public void onResume() {
    super.onResume();
    mAnimation.start();
}

@Override
public void onPause() {
    super.onPause();
    mAnimation.stop();
}

编辑:我忘记添加动画位于选项卡内,这使事情变得更加困难。不过,我已经找到了问题所在,并将在下面添加答案。

I have an animation which starts correctly the first time the fragment is displayed. However after an orientation change, it won't restart. The animation is an animation-list resource set as the background of an ImageView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View root = inflater.inflate(R.layout.fragment_lead_manual,
            container, false);
    final ImageView badgeEntryView = (ImageView) root
            .findViewById(R.id.manual_image);
    mAnimation = (AnimationDrawable) badgeEntryView.getBackground();
    return root;
}

@Override
public void onResume() {
    super.onResume();
    mAnimation.start();
}

@Override
public void onPause() {
    super.onPause();
    mAnimation.stop();
}

EDIT: I forgot to add that the animation is inside a tab, which makes things more difficult. However, I've figured out the problem and will add the answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱,才寂寞 2024-12-25 13:30:53

根据创建选项卡的时间,需要解决两种情况:

  1. 首先创建选项卡,然后将其活动附加到窗口
  2. 首先将活动附加到窗口,然后创建选项卡

情况一如果选项卡是第一个显示的选项卡或在旋转期间发生。当用户切换到该选项卡时会出现第二种情况,因为它不是第一个选项卡。让我们分别处理每种情况:

情况 1:a) 创建选项卡 b) 附加到窗口

在附加到窗口之前(即在 onCreate() 或 onResume() 内部)调用 AnimationDrawable.start() 会中断动画。如 Android 文档 中所述:

需要注意的是,在 Activity 的 onCreate() 方法期间无法调用对 AnimationDrawable 调用的 start() 方法,因为 AnimationDrawable 尚未完全附加到窗口。如果您想立即播放动画而不需要交互,那么您可能需要从 Activity 中的 onWindowFocusChanged() 方法调用它,当 Android 将您的窗口置于焦点时,该方法将被调用。

Fragments 的难度更大,但基本上是一样的。我们重写 Activity 中的方法,然后调用 Fragment:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        final FragmentManager fm = getFragmentManager();
        ManualLeadFragment manualFragment = (ManualLeadFragment) fm
                .findFragmentByTag(TAG_MANUAL);
        if (manualFragment != null) {
            manualFragment.startAnimation();
        }
    }
}

然后在 Fragment 中,实现 startAnimation():

void startAnimation() {
    mAnimation.start();
}

情况 2:a) 附加到窗口 b) 创建选项卡

在这种情况下,对 onWindowFocusChanged() 的调用已经发生,因此动画不会启动。因此,我们仍然需要在 onResume() 期间启动它,但略有不同:

@Override
public void onResume() {
    super.onResume();
    if (isVisible()) {
        startAnimation();
    }
}

这会调用与情况 1 相同的 startAnimation() 方法,但由于 Fragment 已经附加到 Window,因此可以在 onResume() 期间调用它。

摘要

AnimationDrawable.start() 只能在 Fragment 可见时调用。有时它在 onResume() 期间可见,并且可以在此时开始动画。其他时候它还不可见,然后当它变得可见时调用重写的 onWindowFocusChanged() 方法,然后开始动画。

There are two cases that need to be solved, based on when the tab is created:

  1. FIRST the tab is created, and SECOND its Activity is attached to the Window
  2. FIRST the Activity is attached to the Window, and SECOND the tab is created

Case one occurs if the tab is the first one displayed or during rotation. Case two occurs when the user switches to that tab because it's not the first. Let's handle each case separately:

Case 1: a) Create tab b) Attach to window

Calling AnimationDrawable.start() before it is attached to the window (i.e. inside onCreate() or onResume()) breaks the animation. As stated in the Android docs :

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.

It's more difficult with Fragments, but basically the same. We override the method in the Activity and then call over to the Fragment:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        final FragmentManager fm = getFragmentManager();
        ManualLeadFragment manualFragment = (ManualLeadFragment) fm
                .findFragmentByTag(TAG_MANUAL);
        if (manualFragment != null) {
            manualFragment.startAnimation();
        }
    }
}

And then in the Fragment, implement startAnimation():

void startAnimation() {
    mAnimation.start();
}

Case 2: a) Attach to window b) Create tab

In this case, the call to onWindowFocusChanged() has already occurred and so the animation won't start. So we still need to start it during onResume(), but slightly differently:

@Override
public void onResume() {
    super.onResume();
    if (isVisible()) {
        startAnimation();
    }
}

This calls into the same startAnimation() method as in Case 1, but because the Fragment is already attached to the Window, it can be called during onResume().

Summary

AnimationDrawable.start() can only be called when the Fragment is visible. Sometimes it is visible during onResume(), and the animation can be started at that point. Other times it is not yet visible at that time, and then the overridden onWindowFocusChanged() method is called when when it becomes visible, and the animation is started then.

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