Activty界面切换动画

在介绍切换动画效果前,先介绍下将使用到的Android SDK提供的工具类。

AlphaAnimation:控制动画对象的透明度,淡入淡出效果实现。

TranslateAnimation:控制动画对象的位置,实现对象位置的移动动画。

Animation:动画抽象类。

AnimationUtils:提供了动画的一些常用方法。

通过XML方式定义动画的形式。

更多的动画说明文档请看:http://android.toolib.net/guide/topics/resources/animation-resource.html

 

 

一、淡入淡出方式切换

1、建立Activity淡入动画的XML描述enter_alpha.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <alpha
4         android:fromAlpha="1.0" //1表示完全不透明, 0表示完全透明。这里设置起始透明度
5         android:duration="5000" //动画时间,5s
6         android:toAlpha="0" //设置结束透明度 />
7 </set>

2、建立Activity淡出动画的XML描述out_alpha.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <alpha
4         android:fromAlpha="0"
5         android:duration="5000"
6         android:toAlpha="1.0" />
7 </set>

上述的xml文件存放路径,在res路径下新建文件夹anim,存放在此文件夹下。

 

在JAVA中调用动画资源方式:R.anmi.文件名

在XML中:@[package:]anim/文件名

 

3、设计主Activity界面main.xml

原型图效果:

界面XML描述:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7     
 8 <Button
 9     android:layout_width="wrap_content"
10     android:layout_height="wrap_content"
11     android:onClick="change"
12     android:text="淡入淡出Activity" 
13     />
14 
15 <Button
16     android:layout_width="wrap_content"
17     android:layout_height="wrap_content"
18     android:onClick="change2"
19     android:text="滚动切换Activity"
20     />
21 </LinearLayout>

打开MainActivity定义“淡入淡出Activity”按钮的change事件:

    public void change(View v){
        Intent intent = new Intent(this, OtherActivity.class);
        
        startActivity(intent);
        
        overridePendingTransition(R.anim.out_alpha, R.anim.enter_alpha);
    }

4、设计第二个Activity界面other.xml,并添加Activity信息到AndroidManifest.xml

原型图效果:

 

创建第二个Activity界面OtherActivity类:

 1 package mr.jin.activity;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 public class OtherActivity extends Activity {
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.other);
11     }
12 }

添加Activity信息:

<activity android:name=".OtherActivity" android:label="otherActivity">

界面XML描述:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:background="#0000ff"
 7     >
 8 <TextView  
 9     android:layout_width="fill_parent" 
10     android:layout_height="wrap_content" 
11     android:text="这是第二个Activity界面"
12     />
13 </LinearLayout>

到这里,淡入淡出切换Activity已经完成。

 

二、滚动方式切换

在实现淡入淡出时,界面已经设计完成,这里只需要实现动画部分。

1、Activity滚入XML动画描述lefttoright.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android">
 3     <translate
 4         android:fromXDelta="-100%p"//动画对象的起始X坐标
 5         android:toXDelta="0"//动画对象的结束X坐标
 6         android:fromYDelta="0"//这里是横向移动,所以Y坐标无需改变,始终是0
 7         android:toYDelta="0"
 8         android:duration="5000"//动画时间5s
 9          />
10 </set>

2、Activity滚出XML动画描述righttoleft.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android">
 3     <translate
 4         android:fromXDelta="0"
 5         android:toXDelta="100%p"
 6         android:fromYDelta="0"
 7         android:toYDelta="0"
 8         android:duration="5000"
 9          />
10 </set>

3、MainActivity中定义“滚动切换Activity”按钮事件

    public void change2(View v){
        Intent intent = new Intent(this, OtherActivity.class);
        
        startActivity(intent);
        
        overridePendingTransition(R.anim.lefttoright, R.anim.righttoleft);
    }

 

posted @ 2013-01-12 15:07  ggzjj  阅读(286)  评论(0编辑  收藏  举报