欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android默认转场动画备忘录、动画生效时机及overridePendingTransition影响范围

程序员文章站 2022-05-03 08:58:17
...

全屏window、Activity默认动画

<resources>
    <style name="AppTheme" parent="Theme.XXXX">
        <item name="android:windowAnimationStyle">@style/Animation.Activity</item>
    </style>
    <style name="Animation.Activity">
        <item name="activityOpenEnterAnimation">@anim/activity_open_enter</item>
        <item name="activityOpenExitAnimation">@anim/activity_open_exit</item>
        <item name="activityCloseEnterAnimation">@anim/activity_close_enter</item>
        <item name="activityCloseExitAnimation">@anim/activity_close_exit</item>
        <item name="taskOpenEnterAnimation">@anim/task_open_enter</item>
        <item name="taskOpenExitAnimation">@anim/task_open_exit</item>
        <item name="launchTaskBehindTargetAnimation">@anim/launch_task_behind_target</item>
        <item name="launchTaskBehindSourceAnimation">@anim/launch_task_behind_source</item>
        <item name="taskCloseEnterAnimation">@anim/task_close_enter</item>
        <item name="taskCloseExitAnimation">@anim/task_close_exit</item>
        <item name="taskToFrontEnterAnimation">@anim/task_open_enter</item>
        <item name="taskToFrontExitAnimation">@anim/task_open_exit</item>
        <item name="taskToBackEnterAnimation">@anim/task_close_enter</item>
        <item name="taskToBackExitAnimation">@anim/task_close_exit</item>
        <item name="wallpaperOpenEnterAnimation">@anim/wallpaper_open_enter</item>
        <item name="wallpaperOpenExitAnimation">@anim/wallpaper_open_exit</item>
        <item name="wallpaperCloseEnterAnimation">@anim/wallpaper_close_enter</item>
        <item name="wallpaperCloseExitAnimation">@anim/wallpaper_close_exit</item>
        <item name="wallpaperIntraOpenEnterAnimation">@anim/wallpaper_intra_open_enter</item>
        <item name="wallpaperIntraOpenExitAnimation">@anim/wallpaper_intra_open_exit</item>
        <item name="wallpaperIntraCloseEnterAnimation">@anim/wallpaper_intra_close_enter</item>
        <item name="wallpaperIntraCloseExitAnimation">@anim/wallpaper_intra_close_exit</item>
        <item name="fragmentOpenEnterAnimation">@animator/fragment_open_enter</item>
        <item name="fragmentOpenExitAnimation">@animator/fragment_open_exit</item>
        <item name="fragmentCloseEnterAnimation">@animator/fragment_close_enter</item>
        <item name="fragmentCloseExitAnimation">@animator/fragment_close_exit</item>
        <item name="fragmentFadeEnterAnimation">@animator/fragment_fade_enter</item>
        <item name="fragmentFadeExitAnimation">@animator/fragment_fade_exit</item>
    </style>
</resources>

非全屏window、Activity默认动画

<style name="AppTheme" parent="Theme.XXXX">
    <item name="android:windowAnimationStyle">@style/Animation.Dialog</item>
</style>
<style name="Animation.Dialog">
    <item name="windowEnterAnimation">@anim/dialog_enter</item>
    <item name="windowExitAnimation">@anim/dialog_exit</item>
</style>

...等等 参考android/frameworks/base/core/res/res/values/styles.xml

Transition中默认动画

<resources>
    <style name="AppTheme" parent="Theme.XXXX">
        <item name="windowContentTransitions">false</item>
        <item name="windowActivityTransitions">true</item>
        <item name="windowEnterTransition">@transition/fade</item>
        <item name="windowExitTransition">@transition/slide_bottom</item>//这个不是默认的
        <item name="windowSharedElementEnterTransition">@transition/move</item>
        <item name="windowSharedElementExitTransition">@transition/move</item>
        <item name="windowAllowEnterTransitionOverlap">true</item>
        <item name="windowAllowReturnTransitionOverlap">true</item>
    </style>
</resources>

生效时机,及overridePendingTransition影响范围

Android7.0 (Pixel XL API24 x86_64)
1、同一Task,以下四个动画生效
       activityOpenEnterAnimation/activityOpenExitAnimation
       activityCloseEnterAnimation/activityCloseExitAnimation
   例如:
        Activity1启动Activity2
             Acivity1使用Activity2的styles.xml中声明的activityOpenExitAnimation
             Acivity2使用Activity2的styles.xml中声明的activityOpenEnterAnimation
        Activity2返回Activity1
             Acivity1使用Activity1的styles.xml中声明的activityCloseEnterAnimation
             Acivity2使用Activity1的styles.xml中声明的activityCloseExitAnimation
2、不同Task(设置了SingleInstance或两个应用),以下四个动画生效
        taskOpenEnterAnimation/taskOpenExitAnimation
        taskCloseEnterAnimation/taskCloseExitAnimation
   例如:
        应用1的Activity1启动应用2的Activity2
             Acivity1使用Activity2的styles.xml中声明的taskOpenExitAnimation
             Acivity2使用Activity2的styles.xml中声明的taskOpenEnterAnimation
        应用2的Activity2返回应用1的Activity1
             Acivity1使用Activity1的styles.xml中声明的taskCloseEnterAnimation
             Acivity2使用Activity1的styles.xml中声明的taskCloseExitAnimation
3、不同Task,当Task被move到顶层或底层时{例如:重写finish(),在finish()方法中调moveTaskToBack(true)
   },以下四个动画生效
        taskToFrontEnterAnimation/taskToFrontExitAnimation
        taskToBackEnterAnimation/taskToBackExitAnimation
   例如:
        Activity2设置了启动模式为SingleInstance
        Activity1启动Activity2
             Acivity1使用Activity2的styles.xml中声明的taskToFrontExitAnimation
             Acivity2使用Activity2的styles.xml中声明的taskToFrontEnterAnimation
        Activity2返回Activity1
             Acivity1使用Activity1的styles.xml中声明的taskToBackEnterAnimation
             Acivity2使用Activity1的styles.xml中声明的taskToBackExitAnimation
4、当Activity设置了<item name="android:windowShowWallpaper">true</item>
   (123)中的动画将不生效,以下八个动画生效
        wallpaperOpenEnterAnimation/wallpaperOpenExitAnimation
        wallpaperCloseEnterAnimation/wallpaperCloseExitAnimation
        wallpaperIntraOpenEnterAnimation/wallpaperIntraOpenEnterAnimation
        wallpaperIntraCloseEnterAnimation/wallpaperIntraCloseExitAnimation
   例如:
        情景1:Activity1设置了<item name="android:windowShowWallpaper">true</item>
        Activity1启动Activity2
             Acivity1使用Activity2的styles.xml中声明的wallpaperCloseExitAnimation
             Acivity2使用Activity2的styles.xml中声明的wallpaperCloseEnterAnimation
        Activity2返回Activity1
             Acivity1使用Activity1的styles.xml中声明的wallpaperOpenEnterAnimation
             Acivity2使用Activity1的styles.xml中声明的wallpaperOpenExitAnimation

        情景2:Activity2设置了<item name="android:windowShowWallpaper">true</item>
        Activity1启动Activity2
             Acivity1使用Activity2的styles.xml中声明的wallpaperOpenExitAnimation
             Acivity2使用Activity2的styles.xml中声明的wallpaperOpenEnterAnimation
        Activity2返回Activity1
             Acivity1使用Activity1的styles.xml中声明的wallpaperCloseEnterAnimation
             Acivity2使用Activity1的styles.xml中声明的wallpaperCloseExitAnimation

        情景3:Activity1、Activity2同时设置了<item name="android:windowShowWallpaper">true</item>
        Activity1启动Activity2
             Acivity1使用Activity2的styles.xml中声明的wallpaperIntraOpenExitAnimation
             Acivity2使用Activity2的styles.xml中声明的wallpaperIntraOpenEnterAnimation
        Activity2返回Activity1
             Acivity1使用Activity1的styles.xml中声明的wallpaperIntraCloseEnterAnimation
             Acivity2使用Activity1的styles.xml中声明的wallpaperIntraCloseExitAnimation

5、overridePendingTransition(int enterAnim, int exitAnim);
   会覆盖掉以上(1234)通过windowAnimationStyle声明的动画,
   但无法覆盖5.0后的Transition{startActivity(intent, options.toBundle())};
6、遗留问题:FallbackHome启动launcher时,launcher的动画wallpaperCloseEnterAnimation去不掉
   ,只能用overridePendingTransition(0, 0);原因不明

附录 SingleTask与SingleInstance

SingleTask:标注的Activity在被自身的app调用时,不新建task。当已存在的SingleTask界面被切换到前台时,
SingleTask界面之后开启的其他界面会被关闭
SingleInstance:新建task