How to change all the activity transitions at once in Android application?

前端 未结 5 852
日久生厌
日久生厌 2020-12-07 20:45

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

activity.overridePendingTransition(R.anim.activity_clo

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

    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
    0 讨论(0)
  • 2020-12-07 20:58

    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>
    
    0 讨论(0)
  • 2020-12-07 21:01

    Step 1: Create one base activity

    Step 2: Extend all your activity to this base activity

    Step 3: In your base activity add following code

    @Override
    protected void onStart() {
    super.onStart();
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
    
    @Override
    protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
     }
    }
    
    0 讨论(0)
  • 2020-12-07 21:03

    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>
    
    0 讨论(0)
  • 2020-12-07 21:09
    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>
    
    0 讨论(0)
提交回复
热议问题