前言
在項目中設置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:windowEnterAnimation和overridePendingTransition的第一個參數(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文件中設置進入退出動畫時在不同機型可能會有不同的效果,記下來提醒自己。