动画移除片段

Posted

技术标签:

【中文标题】动画移除片段【英文标题】:Animate removal of an Fragment 【发布时间】:2016-02-06 15:59:35 【问题描述】:

在一个应用程序中,我尝试为片段的移除制作动画。

transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
transaction.commit();

框架完全忽略了这一点。片段本身被移除,但视觉效果并不好。任何FragmentTransaction#replace 都适用于这些动画。我正在使用SupportLibrary v23.1.

感谢您帮助我:)

【问题讨论】:

【参考方案1】:

必须在 transaction.remove()/add()/replace() 之前调用 transaction.setCustomAnimation(),否则动画将永远不会运行。

所以你的代码看起来像:

transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
transaction.commit();

【讨论】:

嗯,好的。似乎在***.com/questions/13982240/… 之前已经提出过这个问题,并且当动画在传入视图上运行时,您实际上无法为片段的移除设置动画。我链接到的问题有一些关于如何解决这个问题的想法。【参考方案2】:

这是我曾经面临的一个很常见的问题。我在决定使用 Fragments 时非常小心,因为它们对于 UI 动画非常不灵活。您可能会在这里找到答案:

http://daniel-codes.blogspot.com/2013/09/smoothing-performance-on-fragment.html

您还可以为您的片段设置动画并在完成动画后执行移除事务,以适当的顺序分别执行这两项操作。

//Pseudo Code

Animation anim = ... //this animates your fragment view
anim.setAnimationListener(new AnimationListener() 

    void onAnimFinish() 
        transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
        transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
        transaction.commit()
        getFragmentManager().executePendingTransactions();
    

)

【讨论】:

以上是关于动画移除片段的主要内容,如果未能解决你的问题,请参考以下文章

移除 Sprite 的动画

使用嵌套片段和动画对象

动画移除注释

移除 Item RecyclerView 时添加自定义动画

移除 ViewPager2 Overscroll 动画

在vue中beforeDestroy()中移除requestAnimationFrame方法启动的动画