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屏幕切换动画(一)-左右滑动切换

 

原文地址:https://www.cnblogs.com/liaojie970/p/5830357.html