Android實現Activity頁面跳轉切換動畫特效


了解Android程序設計的人應該知道,在Android 2.0之后有了overridePendingTransition(),其中里面兩個參數,一個是前一個activity的退出,另一個activity的進入。

如下代碼:

@Override  
public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.SplashScreen);  
   new Handler().postDelayed(new Runnable() {  
    @Override  
    public void run() {  
    Intent mainIntent = new Intent(SplashScreen.this,   AndroidNews.class);  
    SplashScreen.this.startActivity(mainIntent);  
    SplashScreen.this.finish();  
    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);  
    }  
   }, 3000);  
} 

首先要在res文件夾下建立anim文件夾,然后把動畫效果xml文件放到里面去

zoomin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android"
    Android:interpolator="@android:anim/decelerate_interpolator">
    <scale
        Android:duration="@android:integer/config_mediumAnimTime"
        Android:fromXScale="2.0"
        Android:fromYScale="2.0"
        Android:pivotX="50%p"
        Android:pivotY="50%p"
        Android:toXScale="1.0"
        Android:toYScale="1.0" />
</set>

zoomout.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android"
    Android:interpolator="@android:anim/decelerate_interpolator"
    Android:zAdjustment="top">
    <scale
        Android:duration="@android:integer/config_mediumAnimTime"
        Android:fromXScale="1.0"
        Android:fromYScale="1.0"
        Android:pivotX="50%p"
        Android:pivotY="50%p"
        Android:toXScale=".5"
        Android:toYScale=".5" />
    <alpha
        Android:duration="@android:integer/config_mediumAnimTime"
        Android:fromAlpha="1.0"
        Android:toAlpha="0" />
</set>

 從左向右切換的效果:

Activity的位置定義,如下圖:

從上圖可以看出,以手機屏幕下面邊未X軸,屏幕左邊為Y軸,當Activity在X軸值為-100%p時,剛好在屏幕的左邊(位置1),當X軸值為0%p時,剛好再屏幕內(位置2),當X=100%p時剛好在屏幕右邊(位置3)。

清楚了位置后,我們就可以實現左右滑動的切換效果,首先讓要退出的Activity從位置2移動到位置1,同時讓進入的Activity從位置3移動位置2,這樣就能實現從左右切換效果。

out_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:duration="260"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p"></translate>
</set>

in_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:duration="260"
        android:fromXDelta="100%p"
        android:toXDelta="0%p"></translate>
</set>

 

 

關於overridePendingTransition這個函數,有兩點需要主意
1.它必需緊挨着startActivity()或者finish()函數之后調用"
2.它只在android2.0以及以上版本上適用  

overridePendingTransition 這個函數會不起作用,總結下,大概是以下三個方面的原因:

 1、android系統版本2.0以下,這個沒辦法,想其他辦法解決切換動畫吧。
 2、在ActivityGroup等的嵌入式Activity中,這個比較容易解決,用如下方法就可以了:
    this.getParent().overridePendingTransition
就可以解決。
 3、在一個Activity的內部類中,或者匿名類中,這時候只好用handler來解決了。
 4、手機的顯示動畫效果被人為或者其他方式給關閉了 現在打開即可 設置->顯示->顯示動畫效果

 

參考:

Android的Activity屏幕切換動畫(一)-左右滑動切換

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM