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

Activity转场动效

程序员文章站 2022-05-03 08:23:41
...

Activity转场动效

Activity原生转场的动画效果实现

xml中实现Activity的转场动效

动效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>

代码中实现Activity的转场动效

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

源码中有明确说明:@Activity.java

    /**
     * Call immediately after one of the flavors of {@link #startActivity(Intent)}
     * or {@link #finish} to specify an explicit transition animation to
     * perform next.
     *
     * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
     * to using this with starting activities is to supply the desired animation
     * information through a {@link ActivityOptions} bundle to
     * {@link #startActivity(Intent, Bundle)} or a related function.  This allows
     * you to specify a custom animation even when starting an activity from
     * outside the context of the current top activity.
     *
     * @param enterAnim A resource ID of the animation resource to use for
     * the incoming activity.  Use 0 for no animation.
     * @param exitAnim A resource ID of the animation resource to use for
     * the outgoing activity.  Use 0 for no animation.
     */
    public void overridePendingTransition(int enterAnim, int exitAnim) {
        try {
            ActivityManager.getService().overridePendingTransition(
                    mToken, getPackageName(), enterAnim, exitAnim);
        } catch (RemoteException e) {
        }
    }

示例

实现淡入淡出的效果
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动画的作用对象是View,四种动画效果:平移,缩放,旋转,透明度

Android中的View动画和属性动画