Activity转场动效

匿名 (未验证) 提交于 2019-12-03 00:22:01

动效xml文件(frameworks/base/core/res/res/anim)

activity_close_enter.xml     activity_close_exit.xml  activity_open_enter.xml  activity_open_exit.xml    task_close_enter.xml     task_close_exit.xml  task_open_enter.xml task_open_exit.xml

对应的引用文件(frameworks/base/core/res/res/values/styles.xml)

<!-- Standard animations for a full-screen window or activity. --> <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="taskCloseEnterAnimation">@anim/task_close_enter</item>     <item name="taskCloseExitAnimation">@anim/task_close_exit</item>     ... </style>

这些动画具体的在什么情况下被使用,在源码中有解释(frameworks/base/core/res/res/values/attrs.xml)

<!--  When opening a new activity, this is the animation that is       run on the next activity (which is entering the screen). --> <!--  当打开一个新活动时,这是在下一个活动(即进入屏幕)上运行的动画  --> <attr name="activityOpenEnterAnimation" format="reference" />  <!--  When opening a new activity, this is the animation that is       run on the previous activity (which is exiting the screen). --> <!--  当打开一个新活动时,这是在先前的活动中运行的动画(它正在退出屏幕)  --> <attr name="activityOpenExitAnimation" format="reference" />  <!--  When closing the current activity, this is the animation that is       run on the next activity (which is entering the screen). --> <!--  在关闭当前活动时,这是在下一次活动(即进入屏幕)上运行的动画  --> <attr name="activityCloseEnterAnimation" format="reference" />  <!--  When closing the current activity, this is the animation that is       run on the current activity (which is exiting the screen). --> <!--  当关闭当前活动时,这是在当前活动上运行的动画(它正在退出屏幕)  --> <attr name="activityCloseExitAnimation" format="reference" />   <!--  When opening an activity in a new task, this is the animation that is       run on the activity of the new task (which is entering the screen). --> <!--  当在一个新任务中打开一个活动时,这是在新任务(进入屏幕)活动上运行的动画  --> <attr name="taskOpenEnterAnimation" format="reference" />  <!--  When opening an activity in a new task, this is the animation that is       run on the activity of the old task (which is exiting the screen). --> <!--  当在一个新任务中打开一个活动时,这是在旧任务的活动上运行的动画(它正在退出屏幕)  --> <attr name="taskOpenExitAnimation" format="reference" />  <!--  When closing the last activity of a task, this is the animation that is       run on the activity of the next task (which is entering the screen). --> <!--  当关闭任务的最后一个活动时,这是运行在下一个任务(正在进入屏幕)活动上的动画  --> <attr name="taskCloseEnterAnimation" format="reference" />  <!--  When opening an activity in a new task, this is the animation that is       run on the activity of the old task (which is exiting the screen). --> <!--  当在一个新任务中打开一个活动时,这是在旧任务的活动上运行的动画(它正在退出屏幕)  --> <attr name="taskCloseExitAnimation" format="reference" />

博客中找到更直白的解释


解释1:

android:activityOpenEnterAnimation:
一个activity创建进入的效果。

android:activityOpenExitAnimation :
一个activity还没有finish()下退出效果, 比如有俩个activity A与B 首先启动A 然后再启动B 那么A还没有finish() 这时A的退出效果。

android:activityCloseEnterAnimation:
表示上一个activity返回进入效果 比如有俩个activity A与B B在最上面,B退出(finish)后 A重新进入的效果。

android:activityCloseExitAnimation:
表示的是activity finish()之后的效果 比如有俩个activity A与B B退出后会被finish() 那么B的退出效果在这定义。

解释2:

android:activityOpenEnterAnimation:
Activity A跳转到Activity B时Activity B进入动画
android:activityOpenExitAnimation :
Activity A跳转到Activity B时Activity A退出动画
android:activityCloseEnterAnimation:
Activity B返回Activity A时Activity A的进入动画
android:activityCloseExitAnimation:
Activity B返回Activity A时ActivityB的退出动画


了解了具体的动画调用时机,通过自定义一个动画Style(复写”Animation.Activity”),加入新的动画效果,继而在Theme中引用该Style,并在应用中引用该Theme,就能达到目的。

如果不想使用动画,可以将其删除,将styles.xml中的应用动画置为null即可,如下:

<style name="custom_animation" parent="@android:style/Animation.Activity"> <item name="android:activityOpenEnterAnimation">@null</item> <item name="android:activityOpenExitAnimation">@null</item> <item name="android:activityCloseEnterAnimation">@null</item> <item name="android:activityCloseExitAnimation">@null</item> </style>

涉及到一个方法:public void overridePendingTransition (int enterAnim, int exitAnim)
调用时机:startActivity(intent)之后,和finish()之后,不然会不起作用
enterAnim:下个Activity进入的动效
exitAnim:当前Activity退出的动效

示例

实现淡入淡出的效果
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);

由左向右滑入的效果
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);


View动画的作用对象是View,四种动画效果:平移,缩放,旋转,透明度

Android中的View动画和属性动画

转载请标明出处:Activity转场动效
文章来源: Activity转场动效
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!