旋转后framgent中的动画不会重新启动
我有一个在第一次显示片段时正确启动的动画。但是,方向改变后,它不会重新启动。动画是设置为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据创建选项卡的时间,需要解决两种情况:
情况一如果选项卡是第一个显示的选项卡或在旋转期间发生。当用户切换到该选项卡时会出现第二种情况,因为它不是第一个选项卡。让我们分别处理每种情况:
情况 1:a) 创建选项卡 b) 附加到窗口
在附加到窗口之前(即在 onCreate() 或 onResume() 内部)调用 AnimationDrawable.start() 会中断动画。如 Android 文档 中所述:
Fragments 的难度更大,但基本上是一样的。我们重写 Activity 中的方法,然后调用 Fragment:
然后在 Fragment 中,实现 startAnimation():
情况 2:a) 附加到窗口 b) 创建选项卡
在这种情况下,对 onWindowFocusChanged() 的调用已经发生,因此动画不会启动。因此,我们仍然需要在 onResume() 期间启动它,但略有不同:
这会调用与情况 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:
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 more difficult with Fragments, but basically the same. We override the method in the Activity and then call over to the Fragment:
And then in the Fragment, implement startAnimation():
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:
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.