本文以实例形式展示了Android实现Activity界面切换添加动画特效的方法,对于Android程序设计人员来说有很好的参考借鉴价值。具体方法如下:
了解Android程序设计的人应该知道,在Android 2.0之后有了overridePendingTransition(),其中里面两个参数,一个是前一个activity的退出,另一个activity的进入。
现看看下面这段示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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.mainfadein, R.anim.splashfadeout); } }, 3000 ); } |
上面的代码只是闪屏的一部分。
1
|
getWindow().setWindowAnimations( int ); |
这可没有上个好但是也可以 。
实现淡入淡出的效果:
1
|
overridePendingTransition(Android.R.anim.fade_in,android.R.anim.fade_out); |
由左向右滑入的效果:
1
|
overridePendingTransition(Android.R.anim.slide_in_left,android.R.anim.slide_out_right); |
实现zoomin和zoomout,即类似iphone的进入和退出时的效果:
1
|
overridePendingTransition(R.anim.zoomin, R.anim.zoomout); |
新建zoomin.xml文件:
1
2
3
4
5
6
7
8
|
<? 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:fromXScale = "2.0" android:toXScale = "1.0" Android:fromYScale = "2.0" android:toYScale = "1.0" Android:pivotX = "50%p" android:pivotY = "50%p" Android:duration = "@android:integer/config_mediumAnimTime" /> </ set > |
新建zoomout.xml文件:
1
2
3
4
5
6
7
8
9
10
11
|
<? 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:fromXScale = "1.0" android:toXScale = ".5" Android:fromYScale = "1.0" android:toYScale = ".5" Android:pivotX = "50%p" android:pivotY = "50%p" Android:duration = "@android:integer/config_mediumAnimTime" /> < alpha Android:fromAlpha = "1.0" android:toAlpha = "0" Android:duration = "@android:integer/config_mediumAnimTime" /> </ set > |
相信本文所述示例对大家的Android程序设计有一定的借鉴价值。