overridePendingTransition for sliding activities in and out smoothly

為{幸葍}努か 提交于 2019-11-28 19:25:17

Instead of overriding the animation in both startActivity() and the new activities onCreate(), you only need to override the animation just after the startActivity() call.

The two ints you provide for overridePendingTransition(int enterAnim, int exitAnim) correspond to the two animations - removing the old Activity and adding the new one.

For your second question, I believe you have the fromXDelta set wrong, -100% should be all the way off the left-hand side of the screen, not the right, so changing this to 100% should fix it.

Jagie

look at my gist, it works perfectly:

1.Override CommonActivity's startActivity and finish

 @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.from_left_in, R.anim.from_right_out);
    }

2.from_left_in.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p"
               android:toXDelta="0"
               android:duration="300"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
   </set>

3.from_right_in.xml

   <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p"
               android:toXDelta="0"              android:interpolator="@android:interpolator/accelerate_decelerate"
               android:duration="300"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

4.from_left_out.xml

   <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0"
               android:toXDelta="-100%p"
               android:duration="300"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
</set>

5.from_right_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0"
               android:toXDelta="100%p"
               android:duration="300"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
</set>

gist link: https://gist.github.com/JagieChen/f5cc44bf663f3722bd19097be47ccf9b

There's an error not only in the enter_from_right animation, that should have a fromXDelta of 100% instead of -100%, but even in the enter_from_left animation, that should fave a fromXDelta of -100% instead of 100%.

Cheers,

syedjibharat

Change fromXDelta to -100% from enter_from_left and fromXDelta to 100% from enter_from_right in your code, this will give you a correct sliding animation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!