如何禁用/避免屏幕旋转后,碎片自定义动画 [英] How to disable/avoid Fragment custom animations after screen rotation

查看:223
本文介绍了如何禁用/避免屏幕旋转后,碎片自定义动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才已经想通了,它可以作为预期片段我setRetainInstance每次(真)(片段数据将被保留),而这是造​​成屏幕旋转后再次执行片段的自定义动画。

I have just figured out that every time I setRetainInstance(true) on a Fragment it works as expected (Fragment data is retained), but this is causing the fragment's custom animation to be executed again after screen rotation.

有没有办法来避免/屏幕旋转禁用这些动画?

Is there a way to avoid/disable those animations on screen rotation?

使用以下动画创建的片段

The fragment is created using the following animations:

setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left,R.anim.slide_in_left,
                    R.anim.slide_out_right);

setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);

所以,我不希望这些滑动动画再次在屏幕上旋转执行。

So, I don't want those "sliding animations" to be executed again on screen rotation.

推荐答案

这是我如何处理它。

private boolean viewsHaveBeenDestroyed;

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    // This stops animation on rotation as we have a retained instance.
    boolean shouldNotAnimate = enter && viewsHaveBeenDestroyed;
    viewsHaveBeenDestroyed = false;
    return shouldNotAnimate ? AnimationUtils.loadAnimation(getActivity(), R.anim.none)
            : super.onCreateAnimation(transit, enter, nextAnim);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    viewsHaveBeenDestroyed = true;
}

在哪里R.anim.none就是什么也不做动画。祝你好运。

Where R.anim.none is just an animation that does nothing. Good luck.

这篇关于如何禁用/避免屏幕旋转后,碎片自定义动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆