overridePendingTransition使用時遇到的一個問題

前言

在項目中設置Activity的進入進出動畫時,發(fā)現(xiàn)當前頁面從上到下退出時會往上彈一下再退出,導致頁面底部會閃動一下,所以又到了填坑時刻。

1.簡要介紹overridePendingTransition

當Activity進入和退出時的需要設置動畫時,overridePendingTransition是一個不錯的選擇,先看一下這個方法的調用實際和需要傳入的參數(shù):

/**
     * 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) {
        }
    }

上面的注釋中可以看到,這個方法需要在startActivity()或者finish()方法之后立即被調用。
第一個參數(shù)enterAnim:設置下一個即將到來的Activity的進入動畫;
第二個參數(shù)exitAnim:設置當前即將退出的這個Activity的退出動畫。
當設置為0時表示沒有動畫。
例如:

                ...
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
                ...

點擊按鈕時,finish掉當前頁面,并設置動畫。R.anim.push_left_in 動畫在res/anim中定義。例如從下往上滑動出現(xiàn):

<?xml version="1.0" encoding="utf-8"?><!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0.0"
        android:duration="420"
        />
</set>

2.問題出現(xiàn)及解決方式

當然,也可以直接在AndroidManifest.xml中定義當前Activity的主題,在主題里面設置動畫:

        <activity
            android:name="com.webclient.MainActivity"
            android:theme="@style/MyMainStyle" />

styles.xml中定義MyMainStyle

    <style name="MyMainStyle" >
        <item name="android:windowAnimationStyle">@style/AnimBottom</item>
    </style>

    <style name="AnimBottom" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>

當然要注意一下這里面的android:windowEnterAnimationoverridePendingTransition的第一個參數(shù)不一樣。android:windowEnterAnimation是指被設置該樣式的Activity進入屏幕時的動畫效果,而overridePendingTransition的第一個參數(shù)enterAnim是設置下一個即將進入屏幕的Activity的進入動畫。
然而這樣設置在有底部虛擬導航欄的情況下就出現(xiàn)了問題:當前頁面從上至下退出時會回彈一下再提出,導致屏幕底部會閃動一下。由于項目原因沒有在調用startActivity()開啟這個activity的時候調用overridePendingTransition來設置這個頁面的進入方式。因此只能在AndroidManifest.xml中設置,即上述方式中設置<item name="android:windowEnterAnimation">@anim/push_bottom_in</item>,由于是退出時發(fā)生閃動問題,此時去掉在xml中設置的退出動畫:<item name="android:windowExitAnimation">@anim/push_bottom_out</item>,在此Activity中重寫finish()方法,在里面調用overridePendingTransition

  @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.push_bottom_out);
    }

再次運行后bug消失,問題解決。因此直接在AndroidManifest.xml文件中設置進入退出動畫時在不同機型可能會有不同的效果,記下來提醒自己。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 一、橫豎屏切換與狀態(tài)保存的問題 前面的文章說到了App橫豎屏切換的時候會銷毀當前的Activity然后重新創(chuàng)建一個...
    常思行閱讀 1,032評論 0 0
  • 【Android 動畫】 動畫分類補間動畫(Tween動畫)幀動畫(Frame 動畫)屬性動畫(Property ...
    Rtia閱讀 6,390評論 1 38
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,112評論 25 709
  • 學習收集,非原創(chuàng) 一、 簡介 在Android開發(fā)過程中,經常會碰到Activity之間的切換效果的問題,下面介紹...
    yyg閱讀 16,588評論 4 39
  • 第一集 P城行星偵探學院的學生 P城行星偵探學院位于P城的行星學校里,院長是一個沉穩(wěn)的中年男子,名叫劉德...
    wyk2007閱讀 344評論 0 2

友情鏈接更多精彩內容