如何设置片段的滑出过渡

时间:2018-04-10 10:57:19

标签: android android-animation shared-element-transition

我正在应用转换集进入片段并退出我想要应用幻灯片动画,我该怎么做,这是我的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    productDetailFragment.setSharedElementEnterTransition(new DetailsTransition());
    productDetailFragment.setEnterTransition(new Fade());
    productDetailFragment.setExitTransition(new Slide(Gravity.RIGHT));
}
((MainActivity)context).getSupportFragmentManager()
                       .beginTransaction()
                       .replace(R.id.fragment, productDetailFragment)
                       .addSharedElement(viewHolder.iv, "kittenImage")
                       .addToBackStack(null)
                       .commit();

3 个答案:

答案 0 :(得分:0)

调用片段

FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate("OtpFragment", 0);
Fragment fragment = null;
Bundle b = new Bundle();

if (!fragmentPopped && fragmentManager.findFragmentByTag("OtpFragment") == null) {
    fragment = new OtpFragment();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_from_left);
    transaction.replace(R.id.frame_container, fragment, "OtpFragment")
               .addToBackStack("OtpFragment")
               .commit();
}

res 文件夹中创建动画目录: 现在为进入/退出动画创建两个单独的文件:即 R.anim.enter_from_right R.anim.exit_from_left

输入幻灯片动画:

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

退出边动画:

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

答案 1 :(得分:0)

请检查以下代码,以了解应用于片段转换的自定义动画:

ngDraggable

slide_up.xml

getSupportFragmentManager()
        .beginTransaction()
        .setCustomAnimations( R.anim.slide_up, 0, 0, R.anim.slide_down)
        .show( m_topFragment )
        .commit();

slide_down.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="translationY"
        android:valueType="floatType"
        android:valueFrom="1280"
        android:valueTo="0"
        android:duration="@android:integer/config_mediumAnimTime"/>

上面我向您展示了slide_up和down过渡,但您可以使用左右和任何自定义objectAnimator在您的片段转换时间中显示动画,并希望它可以帮助您。

答案 2 :(得分:0)

尝试这样的事情。这可能对你有用。  Fragment Transaction Example

Using Slide Example

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

Fade exitFade = new Fade();
exitFade.setDuration(FADE_DEFAULT_TIME);
previousFragment.setExitTransition(exitFade);

Transition slideTransition = new Slide(Gravity.BOTTOM);
Transition fadeTransition = new Fade();
TransitionSet set = new TransitionSet();
set.addTransition(slideTransition);
set.addTransition(fadeTransition);