片段在移除和动画结束后闪烁

Fragment flashes after removal and animation ends

我一直在尝试使用循环显示动画从我的 activity 中插入和删除片段。我发现这个 demo repo here 正是这样做的。我所做的唯一更改是在单击按钮时删除片段,而不是片段触摸侦听器。

问题是,使用按钮移除后,当动画结束时,我看到片段闪烁了一秒钟。如果我使用 onTouch 侦听器删除片段,则不会发生闪光:YouTube link

几点观察:
- 如果我 运行 我的移动设备上的代码,那么只有当我使用按钮删除片段时才会出现闪光。调用onFragmentTouch时没有出现flash。
- 如果我在模拟器上 运行ning 这个,那么我可以看到它在按钮按下和片段触摸上都闪烁。

为了简化事情,我的 onFragmentTouch 侦听器只是调用一个函数 remove 来实际删除片段。这个相同的函数也被按钮调用,在它的 XML 中。 我不明白为什么会这样。参考代码。

添加片段,在主activity:

public void addFragment(View v)
    {
        int randomColor =
                Color.argb(255, (int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
        mfragment = CircularRevealingFragment.newInstance(20, 20, randomColor);
        getFragmentManager().beginTransaction().add(R.id.container, mfragment).commit();
    }

删除片段的函数,也在主函数中 activity:

public void remove(View v){
        Animator unreveal = mfragment.prepareUnrevealAnimator(0, 0);
        unreveal.addListener(new Animator.AnimatorListener()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                getFragmentManager().beginTransaction().remove(mfragment).commit();
            }
            //hiding other overridden function.
        });
        unreveal.start();
    }

CircularRevealingFragment 非常简单,几乎所有内容都与 github 项目中的相同。不露声色的动画师:

public Animator prepareUnrevealAnimator(float cx, float cy)
    {
        int radius = getEnclosingCircleRadius(getView(), (int)cx, (int)cy);
        Animator anim = ViewAnimationUtils.createCircularReveal(getView(), (int) cx, (int) cy, radius, 0);
        anim.setInterpolator(new AccelerateInterpolator(2f));
        anim.setDuration(1000);
        return anim;
    }

谁能解释视频中显示的行为?

我认为主要问题是提交的 FragmentTransaction 异步执行。如果你想立即执行使用 getFragmentManager().executePendingTransactions().