How to change all the activity transitions at once

2020-02-02 06:23发布

问题:

I know I can change activity transition using the following code right after startActivity() or finish()

activity.overridePendingTransition(R.anim.activity_close_enter, R.anim.activity_close_exit);

But if I have ten activities in my app, I have to do that ten times; and it is quite hard to modify. So I'm wondering if there is a way to set transition for all activities within the application at once. Is there any corresponding configuration in AndroidManifest.xml?

Thanks!

回答1:

You want to first create a <style> in res/styles.xml, like this:

    <style name="YourAnimation.Activity" parent="@android:style/Animation.Activity"> 
       <item name="android:windowEnterAnimation">@anim/your_in_down</item>
       <item name="android:windowExitAnimation">@anim/your_out_down</item>
    </style>

Then you can apply the style to a theme, in the same file:

    <style name="YourTheme" parent="android:Theme.Translucent">
       ...
       <item name="android:windowAnimationStyle">@style/YourAnimation.Activity</item>
    </style>

And finally apply the theme to your activities in the manifest:

    <activity
        android:name=".YourActivity"
        android:theme="@style/YourTheme" />

Look at these links for reference:

  • Android Reference - Apply A Theme
  • Android Reference - WindowEnterAnimation


回答2:

I know this has been answered but here is what I did in mine. We still support API 14 so there are some animations missing that I had to pull into the project from API 22( slide_in_right, slide_out_left). What this does is to slide in the screens when you open a new activity and slides the closing one out to the left. When you press back it will then do the opposite, sliding from the left the previous screen and closing out to the right the current screen.

<style name="YourTheme" parent="android:Theme.Translucent">
   ...
    <item name="android:windowAnimationStyle">@style/YourAnimation.Activity</item>
</style>

<style name="YourAnimation.Activity" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@android:anim/slide_in_left</item>
    <item name="android:activityCloseExitAnimation">@android:anim/slide_out_right</item>
</style>


回答3:

My solution is mostly like JPM answer. But here is some additional file that you may require.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>

</style>

<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
</style>

Create anim folder under res folder and then create this four animation files:

slide_in_right.xml

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

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

slide_out_left.xml

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

slide_in_left.xml

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

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

slide_out_right.xml

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

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


回答4:

My solution is mostly like of others...
 <style name="YourAnimation.Activity" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@anim/slidefromright</item>
        <item name="android:windowExitAnimation">@anim/slidetoright</item>
    </style>
<resources>

    <!-- Base application theme. -->
    <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:windowAnimationStyle">@style/YourAnimation.Activity </item>
    </style>


</resources>