How to give top to bottom animation in Android?

2019-01-22 21:03发布

问题:

I am able to give bottom to top animation when I go to next activity but now when I pressed back I am using same code for giving animation from top to bottom animation but it always goes to bottom to top so my question is how to give animation from top to bottom when i pressed back button on android device?
Please find my code below.

I use it during transitioning from one Activity to another using an Intent.

overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );

and xml is: slide_in_up.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p" android:toYDelta="0%p"
android:duration="@android:integer/config_longAnimTime"/>

and slide_out_up.xml is

 <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p" android:toYDelta="-100%p"
android:duration="@android:integer/config_longAnimTime"/>

回答1:

Just Change -100 to 100 (remove minus) in slide_out_up.xml

@Override
public void onBackPressed() {
    finish();
    overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
}


回答2:

i just implemented it with two more xml files having like

slide up, 100 to 0 and 0 to -100

slide down -100 to 0 and 0 to 100

it works perfect.



回答3:

Worked on my tablet 4.0.3.

slide_out_up.xml:

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

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0%p"
        android:toYDelta="-100%p" />

</set>

slide_in_up:xml:

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

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="-100%p"
        android:toYDelta="0%p" />

</set>

style.xml:

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

    <style name="DialogAnimationOutUpInUp">
        <item name="android:windowEnterAnimation">@anim/slide_in_up</item>
        <item name="android:windowExitAnimation">@anim/slide_out_up</item>
    </style>

</resources>


回答4:

You can override the back button press behavior and set the appropriate animation that you want, like this:

@Override
public void onBackPressed() {
    finish();
    overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
}

Change the animations at the overridePendingTransition so that it fits the ones that you want.