Android:两个场景之间的过渡

时间:2015-07-28 20:38:03

标签: android transition

我将制作一个应用程序,可以在屏幕上向上滑动,从一个场景转换到另一个场景。这些场景中的每一个都执行不同的功能,但是我希望过渡能够平滑地进行动画处理,就好像连接了两个场景一样。

制作两个活动并在它们之间添加转换或使用片段进行活动会更好吗?

1 个答案:

答案 0 :(得分:1)

这些方法应该为片段制作动画(比这更复杂但这个例子应该让你开始:)) -

private void animateSwipe(int layoutContainerID, Fragment fragment, String fragmentTag) {
    FragmentTransaction fragmentTransaction =  getFragmentTransactionWithAnimation(true);
    fragmentTransaction.replace(layoutContainerID, fragment, fragmentTag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    fragmentTransaction = null; 
}


private FragmentTransaction getFragmentTransactionWithAnimation(boolean swipeLeft) {
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

    if(swipeLeft)
        fragmentTransaction.setCustomAnimations(R.animator.animate_in, R.animator.animate_out);
    else
        fragmentTransaction.setCustomAnimations(R.animator.animate_in_from_right, R.animator.animate_out_to_left);

    return fragmentTransaction;
}

这是XML动画文件(放在res文件夹中名为animator的文件夹中 -

animate_in.xml -

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="xFraction" 
android:valueType="floatType"
android:valueFrom="-1"
android:valueTo="0" 
android:duration="1000"/>

animate_out.xml -

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="xFraction" 
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1" 
android:duration="1000"/>

animate_out_to_left.xml -

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="xFraction" 
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="-1" 
android:duration="1000"/>

animate_in_from_right.xml -

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="xFraction" 
android:valueType="floatType"
android:valueFrom="1"
android:valueTo="0" 
android:duration="1000"/>