微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 没有xml的Animate Fragment

我正在寻找一种方法来动画片段的过渡而不使用xml.

动画片段的典型方法是将xml动画制作者与片段一起传递给FragmentTransaction.setCustomAnimations(例如参见https://stackoverflow.com/a/4936159/1290264)

相反,我想发送setCustomAnimations一个ObjectAnimator来应用于片段,但遗憾的是这不是一个选项.

关于如何实现这一点的任何想法?

解决方法:

我找到了添加Animator的方法.

它是通过在将片段的onCreateAnimator方法添加到fragmentManager之前重写它来实现的.例如,要在转换期间滑入和滑出动画,您可以执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tutorial);
    if (savedInstanceState == null) {
        Fragment fragment = new MyFragment(){
            @Override
            public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
            {
                display display = getActivity().getwindowManager().getDefaultdisplay();
                Point size = new Point();
                display.getSize(size);

                Animator animator = null;
                if(enter){
                   animator = 
                     ObjectAnimator.ofFloat(this, "translationX", (float) size.x, 0);
                } else {
                   animator = 
                     ObjectAnimator.ofFloat(this, "translationX", 0, (float) size.x);
                }

                animator.setDuration(500);
                return animator;
            }
        }

        getFragmentManager().beginTransaction()
                .add(R.id.container, fragment)
                .commit();
    }
}

附:这个答案归功于this forum的问题部分.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。