FragmentTransaction ft = getFragmentManager().beginTransaction(); //設置進入退出動畫 ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); DetailsFragment newFragment = DetailsFragment.newInstance(); ft.replace(R.id.details_fragment_container, newFragment, "detailFragment"); // Start the animated transition. ft.commit();
下面是anim目錄下的動畫android
enter_from_left.xml <translate android:fromXDelta="-100%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700"/>
enter_from_right.xmlide
<translate android:fromXDelta="100%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700" />
exit_to_left.xml動畫
<translate android:fromXDelta="0%" android:toXDelta="-100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700"/>
exit_to_right.xmlthis
<translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700" />
Intent intent = new Intent(TestActivities.this,TestActivityFirst.class); startActivity(intent); // transaction animation overridePendingTransition(R.anim.slide_in_bottom,R.anim.slide_out_bottom);
Activity的轉場動畫是經過overridePendingTransition(int enterAnim, int exitAnim)實現的。code
這個方法在startActivity(Intent) or finish()以後被調用,指定接下來的這個轉場動畫。xml
1.enterAnim,是新的Activity的進入動畫的resource ID;對象
2.exitAnim,是舊的Activity(當前的Activity)離開動畫的resource ID。blog
因此這兩個參數的對象是兩個Activity。 資源
若是上面兩個參數沒有動畫要設置,則用0做爲參數。get
動畫的資源文件放在res\anim\目錄下