Animation between Activity with Android

2019-05-22 23:14发布

问题:

i need to understand animation on Android.

For example, my application starts with an activity with a button in the bottom, when the user click on the button i want that another activity appears with an animation from bottom to top and i want that the button becomes the "header" of this second activity.

How can i achieve this?

Thank You

Daniele

Thank you to DecodeGnome for the answer! It works!

But i have some problem with the animation when i want to close this activity, i create a anim_out.xml:

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

but this doesn't work (the second parameter of overridePendingTransition what is used for?).

I try to call a new overridePendingTransition in onStop() function:

public void onStop(){
   super.onStop();
   overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);

}

But when i call finish to the second activity, i still see the default animation (from left to right)!

Thank you again to who'll help me.

回答1:

1) Create a folder called anim in res folder

2) Add 2 new XML animations there (example, anim_in.xml & anim_out.xml)

3) put this line of code in the new activities onCreate:

overridePendingTransition(R.anim.anim_in, R.anim.anim_out);

Anim_in.xml example:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="0%p"
        android:fromYDelta="100%p"
        android:toXDelta="0"
        android:toYDelta="0%p"
        android:duration="300" />
</set> 

4) Place the button (header) in the top of the layout of the second activity.



回答2:

Use this code:

     public void onBackPressed() {
        super.onBackPressed();   
        overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);
}