Activity enters and exits animation in Android

Modify acitvity to enter and exit animation

  • Define the entry and exit animation under res/anim, as shown below:
    Insert picture description here
    • Loading animation method 1 is
      introduced in the styles.xml file, and then set the activity theme in AndroidManifest.xml, the code is as follows
//styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:activityOpenEnterAnimation">@anim/activity_open_in_anim</item>
    <item name="android:activityOpenExitAnimation">@anim/activity_open_out_anim</item>
    <item name="android:activityCloseEnterAnimation">@anim/activity_close_in_anim</item>
    <item name="android:activityCloseExitAnimation">@anim/activity_close_out_anim</item>
</style>
//AndroidManifest.xml
  <activity android:name=".MainActivity" android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivty"
            android:theme="@style/AppTheme" />

This method will not work on some phones

  • Loading animation mode 2
    • Call overridePendingTransition() in the next line of startActivity;
    • Override the finish() method and call overridePendingTransition() in it;
  • code show as below
 startActivity(new Intent(this, SecondActivty.class));
        overridePendingTransition(R.anim.activity_open_in_anim,R.anim.activity_open_out_anim);

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

Guess you like

Origin blog.csdn.net/genmenu/article/details/90056305