如何使用customAnimation

时间:2018-09-01 21:54:37

标签: android animation fragment

我一直在互联网上搜索,每次得到相同的结果时,我都想为片段设置自定义动画,请检查以下代码:      FragmentManagerfragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.slide_in_right);
    SpeedMatchFragment speedMatchFragment = new SpeedMatchFragment();
    fragmentTransaction.replace(R.id.fragment_container,speedMatchFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

但此处 fragmentTransaction.setCustomAnimations(R.anim.slide_in_right); 我遇到无法解决错误!请注意,我已经将anim文件夹设置为xml ...,所有工作都需要完成! 这是xml代码:

<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-100%"
    android:toXDelta="0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:duration="@android:integer/config_mediumAnimTime"/>

1 个答案:

答案 0 :(得分:0)

据我所知,您必须设置进入和退出动画,然后错误就会变得像魔术一样。...

 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
     fragmentTransaction.setCustomAnimations(R.anim.pop_enter, R.anim.pop_exit);
        SpeedMatchFragment speedMatchFragment = new SpeedMatchFragment();
        fragmentTransaction.replace(R.id.fragment_container,speedMatchFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();