Animation between two Activities does not work

2019-07-09 22:59发布

I am trying to do a slide animation between two Activities when then one starts the other,

public void onClick(View view) {
    Intent intent = new Intent(TestAppActivity.this, SecondActivity.class);
    startActivityForResult(intent, 1);
    TestAppActivity.this.overridePendingTransition(R.anim.animation_enter,   R.anim.animation_leave);
    finish();
}

There is no animation at all. The xmls are, for enter:

<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="3000" />

And for leave:

<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="3000" />

I can see what is gong wrong here. Using Android 2.3.3. Thanks.

2条回答
Fickle 薄情
2楼-- · 2019-07-09 23:42

Put the overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave); after finish();.

查看更多
不美不萌又怎样
3楼-- · 2019-07-09 23:48

To do an animation where the first activity go to the left and the second activity enters from the right :

slide_out_left.xml :

<?xml version="1.0" encoding="utf-8"?>

<!--
 Animation : Perform animation : Out - Direction : Left
-->

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
       android:duration="400"
       android:fromXDelta="0"
       android:toXDelta="-100%p" />

</set>

slide_in_right.xml :

<?xml version="1.0" encoding="utf-8"?>

<!--
 Animation : Perform animation : In - Direction : Right
-->

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="400"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

Note : you can change android:duration if you want.

And you have to add this code :

public void onClick(View view) {

   Intent intent = new Intent(TestAppActivity.this, SecondActivity.class);
   startActivity(intent);
   overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
   finish();
}
查看更多
登录 后发表回答