替换片段时圆形显示动画

时间:2016-06-20 13:32:49

标签: java android animation fragment

我想知道如何在替换片段时制作圆形透视动画。

我使用此代码替换我的片段:

        getFragmentManager()
            .beginTransaction()
            .replace(R.id.mission_detail_appointment_container, new AppointmentDeclineFragment())
            .commit();

我得到了这个代码来做圆形揭示动画:

Animator anim = ViewAnimationUtils.createCircularReveal(appointmentContainer, (int)motionEventX, (int)motionEventY, 0, finalRadius);
anim.start();

当我替换我的片段时,有没有办法启动动画师?我刚刚看到了setCustomAnimation方法,但它将int资源作为参数,我将成为Animator对象。

感谢您的帮助

1 个答案:

答案 0 :(得分:16)

我终于找到了解决方法。对于那些可能需要它的人来说,这就是我决定以片段形式实现我的圆形揭示动画的方式。

在我的片段的onCreateView方法中添加onLayoutChangeListener:

 view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                v.removeOnLayoutChangeListener(this);
                int cx = getArguments().getInt(MOTION_X_ARG);
                int cy = getArguments().getInt(MOTION_Y_ARG);
                int width = getResources().getDimensionPixelSize(R.dimen.fragment_appointment_width);
                int height = getResources().getDimensionPixelSize(R.dimen.fragment_appointment_height);

                float finalRadius = Math.max(width, height) / 2 + Math.max(width - cx, height - cy);
                Animator anim = ViewAnimationUtils.createCircularReveal(v, cx, cy, 0, finalRadius);
                anim.setDuration(500);
                anim.start();
            }
        });
相关问题