android – 没有xml的Animate Fragment

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

动画片段的典型方法是将xml动画制作者与片段一起传递给FragmentTransaction.setCustomAnimations(例如参见https://*.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的问题部分.

上一篇:【插头dp】 hdu4285 找bug


下一篇:java – 具有无限旋转口吃的ObjectAnimator