前言
在項目中設置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
文件中設置進入退出動畫時在不同機型可能會有不同的效果鬼悠,記下來提醒自己删性。