歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之過渡動畫

Android之過渡動畫

日期:2017/3/1 11:17:12   编辑:Linux編程

在Android中,兩個activiyt的切換總是自左向右抽動的效果

在Activity中提供了overridePendingTransition函數,用在startActivity(Intent) orfinish之後,

overridePendingTransition有兩個參數,都是int類型的,意味著這裡要傳入一個資源,

在sdk中是這樣定義的、

enterAnim A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnim A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
一個是進入的動畫,一個是退出的動畫,如果連個值都設置成0,則表示不添加動畫

例如 在startActivity開啟一個intent之後,添加如下代碼

  1. overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
這樣在啟動下一個窗口的時候出現淡入淡出的效果
  1. //實現從左向右滑動效果
  2. overridePendingTransition(android.R.anim.slide_in_left,
  3. android.R.anim.slide_out_right);

另外,還可以通過在資源文件夾中anim中添加自定義的配置文件,來實現自定義過度動畫

例如在程序中添加這樣的 效果來實現和iphone一樣的效果

  1. overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
自定義的zoomin.xml文件,該文件設置了新的activity進入時的效果
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:interpolator="@android:anim/decelerate_interpolator">
  5. <scale
  6. android:fromXScale="2.0" android:toXScale="1.0"
  7. android:fromYScale="2.0" android:toYScale="1.0"
  8. android:pivotX="50%p" android:pivotY="50%p"
  9. android:duration="@android:integer/config_mediumAnimTime" />
  10. </set>
自定義的zoomout.xml文件,該文件設置了原來的activity退出是的效果
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:interpolator="@android:anim/decelerate_interpolator"
  5. android:zAdjustment="top">
  6. <scale
  7. android:fromXScale="2.0" android:toXScale="0.5"
  8. android:fromYScale="2.0" android:toYScale="0.5"
  9. android:pivotX="50%p" android:pivotY="50%p"
  10. android:duration="@android:integer/config_mediumAnimTime" />
  11. <alpha
  12. android:fromAlpha="1.0"
  13. android:toAlpha="0"
  14. android:duration="@android:integer/config_mediumAnimTime" />
  15. </set>
Copyright © Linux教程網 All Rights Reserved