New Activity in Android “enter from the side”

后端 未结 5 903
谎友^
谎友^ 2020-12-12 19:24

What\'s the best way to go to a new activity but make it look like the activity slides to the left and the new activity enters the screen? I am using intents to call the new

相关标签:
5条回答
  • 2020-12-12 19:50

    There is a bit of a confusion as to what the enter and exit animations are. For those who are still pondering over it, here is a template..

    overridePendingTransition(
        enterAnimationForCalledActivity, 
        exitAnimationForCallingActivity
    );
    

    This should probably clear the air a bit.

    0 讨论(0)
  • 2020-12-12 19:55

    In android OS 2.1 or later I think you can use the OverridePendingTransition() method to provide the kind of transition between activities animations you are looking for.

    Firstly, define a couple of animation resources in /res/anim/. Here is one that is named right_slide_out.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
        <translate
            android:fromXDelta="0"
            android:toXDelta="100%p"
            android:duration="500"
        />
    </set>
    

    An another called right_slide_in.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
        <translate
            android:fromXDelta="100%p"
            android:toXDelta="0"
            android:duration="700"
        />
    </set>
    

    Then, when you are starting the new activity, use the OverridePendingTransition method as in:

    startActivity(intent);
    overridePendingTransition  (R.anim.right_slide_in, R.anim.right_slide_out);
    

    That should handle the transition animations for starting the activity.

    For the reverse, when that activity finishes and youre coming back to the original one, it's a bit more foggy.

    If you have some UI control that ends that activity and calls Activity.finish(), then you can just add the overridePendingTransition() right after that.

    To handle the case where the user ends the activity by pressing the back button, use something like the following:

    @Override
    public void onBackPressed() 
    {
    
        this.finish();
        overridePendingTransition  (R.anim.right_slide_in, R.anim.right_slide_out);
    }
    
    0 讨论(0)
  • 2020-12-12 20:00

    You could use a left_slide_out.xml (just change the toXDelta in Josh's right_slide_out.xml to read -100%p), to make the old activity disappear to the left (and also have the same duration on both of the animations).

    0 讨论(0)
  • 2020-12-12 20:11

    Yes, using intents is the standard way to start another activity, and usually does the sliding thing you mention. e.g. startActivity( new Intent( this, myNextActivity.class ) ); will do it.

    0 讨论(0)
  • 2020-12-12 20:13

    Jems is correct. By default, you will get a sliding animation when launching a new intent.

    If you are looking for a more custom animation, you can use overridePendingTransition. Keep in mind it was added in API Level 5. See this API demo for sample usage.

    0 讨论(0)
提交回复
热议问题