以后我的Api demo源码学习笔记会按照Api demo 的Apk目录顺序进行记录。比如这篇文章的标题App/Activity/Animation 就指的是根目录下的App选项,然后选择Activity,然后选择Animation。与Apk应用的标题栏文字也是一一对应的,方便查找比对,以后都会如此办理。
这个页面要实现的是Activity切换的两种不同动画形式,一种为Fade in,一种为Zoom in。具体效果直接运行Api demo看看就好,比较好理解。
关键代码就一句:
overridePendingTransition(R.anim.fade, R.anim.hold);
或者:
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
在StartActivity后紧跟以上的方法overridePendingTransition函数就可以重写前后两个Activity出现和消失的动画。第一个参数是enterAnim,第二个参数是exitAnim
ActivityAnimationActivity.java
View Code
1 public class ActivityAnimationActivity extends Activity {
2
3 Button button_fade = null;
4 Button button_zoom = null;
5 /** Called when the activity is first created. */
6 @Override
7 public void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.main);
10
11 button_fade = (Button) findViewById(R.id.fade_animation);
12 button_zoom = (Button) findViewById(R.id.zoom_animation);
13
14 button_fade.setOnClickListener(new OnClickListener() {
15
16 @Override
17 public void onClick(View v) {
18 startActivity(new Intent(ActivityAnimationActivity.this, JumptoActivity.class));
19 //api demo源代码中有注释,这里之所以要为旧的activity也设置一个退出动画,是避免它消失得太快,
20 //让新activity透明度渐变的效果缺乏衬托,显现不出来
21 overridePendingTransition(R.anim.fade, R.anim.hold);
22 }
23 });
24
25 button_zoom.setOnClickListener(new OnClickListener() {
26
27 @Override
28 public void onClick(View v) {
29 startActivity(new Intent(ActivityAnimationActivity.this, JumptoActivity.class));
30 overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
31
32 }
33 });
34
35 }
36 }
跳转后的Activity很简单:
View Code
1 public class JumptoActivity extends Activity {
2 public void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.jumpto);
5 }
6
7 }
所设置的动画效果配置文件放在res/anim文件夹中。
fade.xml:
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <alpha xmlns:android="http://schemas.android.com/apk/res/android"
3 android:interpolator="@android:anim/accelerate_interpolator"
4 android:fromAlpha="0.0" android:toAlpha="1.0"
5 android:duration="@android:integer/config_longAnimTime" />
hold.xml
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <translate xmlns:android="http://schemas.android.com/apk/res/android"
3 android:interpolator="@android:anim/accelerate_interpolator"
4 android:fromXDelta="0" android:toXDelta="0"
5 android:duration="@android:integer/config_longAnimTime" />
zoom_enter.xml
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3 android:interpolator="@android:anim/decelerate_interpolator">
4 <scale android:fromXScale="2.0" android:toXScale="1.0"
5 android:fromYScale="2.0" android:toYScale="1.0"
6 android:pivotX="50%p" android:pivotY="50%p"
7 android:duration="@android:integer/config_mediumAnimTime" />
8 </set>
zoom_exit.xml
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3 android:interpolator="@android:anim/decelerate_interpolator"
4 android:zAdjustment="top">
5 <scale android:fromXScale="1.0" android:toXScale=".5"
6 android:fromYScale="1.0" android:toYScale=".5"
7 android:pivotX="50%p" android:pivotY="50%p"
8 android:duration="@android:integer/config_mediumAnimTime" />
9 <alpha android:fromAlpha="1.0" android:toAlpha="0"
10 android:duration="@android:integer/config_mediumAnimTime"/>
11 </set>
另外,注意需要在AndroidManifest.xml中对跳转后的Activity进行注册。
<activity android:name="JumptoActivity"></activity>