Android XML动画无法正常工作

时间:2018-09-04 14:04:33

标签: android animation fragment

我有一个带有 BottomNavigationView 的Activity。它有2个项目(片段)。第一个片段必须从左进入并向左退出。类似地,第二个片段必须从左进入向右然后向右退出。这两个片段都可以正确输入,但以相反的方式退出。在这里看看:

enter_from_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:shareInterpolator="false">
<translate
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="-100%"
    android:toXDelta="0%"
    >
</translate>
</set>

exit_to_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:shareInterpolator="false"
    >
<translate
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
   android:fromXDelta="0%"
    android:toXDelta="-100%"
    >
</translate>
</set>

enter_from_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:shareInterpolator="false">
<translate
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="100%"
    android:toXDelta="0%"
    >

</translate>
</set>

exit_to_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:shareInterpolator="false">
<translate
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
     android:fromXDelta="0%"
    android:toXDelta="100%">

</translate>
</set>

代码:(两个片段的退出动画效果相反

 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.sendSms:
                    FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                    transaction.setCustomAnimations(R.anim.enter_from_left,R.anim.exit_to_left);
                    transaction.replace(R.id.fragcontainer,new SendSmsFragment());
                    transaction.commit();
                    return true;
                case R.id.receiveSms:
                    FragmentTransaction transaction1=getSupportFragmentManager().beginTransaction();
                    transaction1.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_right);
                    transaction1.replace(R.id.fragcontainer,new ReceiveSmsFragment());
                    transaction1.commit();
                    return true;

            }
            return false;
        }
    };

1 个答案:

答案 0 :(得分:0)

尝试exit_to_right:transaction.setCustomAnimations(R.anim.enter_from_left,R.anim.exit_to_right);