Activity轉場動效

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動畫和屬性動畫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章