activity的切换方式

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

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

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

Animation:动画抽象类。

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

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

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

 

 

一、淡入淡出方式切换

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

 

[html]  view plain copy print ?
  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>  
[html]  view plain copy print ?
  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

 

 

[html]  view plain copy print ?
  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>  
[html]  view plain copy print ?
  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描述:

 

[html]  view plain copy print ?
  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>  
[html]  view plain copy print ?
  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事件:

 

[java]  view plain copy print ?
  1. public void change(View v){  
  2.     Intent intent = new Intent(this, OtherActivity.class);  
  3.       
  4.     startActivity(intent);  
  5.       
  6.     overridePendingTransition(R.anim.out_alpha, R.anim.enter_alpha);  
  7. }  
[java]  view plain copy print ?
  1. public void change(View v){  
  2.     Intent intent = new Intent(this, OtherActivity.class);  
  3.       
  4.     startActivity(intent);  
  5.       
  6.     overridePendingTransition(R.anim.out_alpha, R.anim.enter_alpha);  
  7. }  



 

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

原型图效果:

 

创建第二个Activity界面OtherActivity类:

 

[java]  view plain copy print ?
  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. }  
[java]  view plain copy print ?
  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描述:

 

[html]  view plain copy print ?
  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>  
[html]  view plain copy print ?
  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:

 

[html]  view plain copy print ?
  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>  
[html]  view plain copy print ?
  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:

 

 

[html]  view plain copy print ?
  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>  
[html]  view plain copy print ?
  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”按钮事件

[java]  view plain copy print ?
  1. public void change2(View v){  
  2.     Intent intent = new Intent(this, OtherActivity.class);  
  3.       
  4.     startActivity(intent);  
  5.       
  6.     overridePendingTransition(R.anim.lefttoright, R.anim.righttoleft);  
  7. }  

 

 

Activity 切换效果通常可以使用 Android 系统提供的四种动画效果来实现,分别是:overridePendingTransition、ActivityOptions、ViewAnimationUtils 和 Transition。具体来说: 1. overridePendingTransition:这是一种简单的 Activity 切换动画效果,可以在 startActivity 或者 finish 方法中调用,用于在当前 Activity 退出或者新的 Activity 进入时添加动画效果。该方法需要传入两个参数,分别是进入和退出时的动画效果。 2. ActivityOptions:这是一种比较灵活的 Activity 切换效果,可以实现一些特殊的切换动画效果,如共享元素动画等。该方法需要传入一个 Bundle 对象,用于指定要添加的动画效果。 3. ViewAnimationUtils:这是一种可以实现圆形揭示动画的效果,适用于 Android 5.0 及以上版本的系统。该方法需要传入一个 View 对象、开始和结束的圆形半径以及动画的持续时间。 4. Transition:这是一种比较复杂的 Activity 切换效果,可以实现多种动画效果,如淡入淡出、滑动、缩放等。该方法需要使用 Android 系统提供的 Transition 类实现,并且需要在 XML 文件中定义动画效果。使用时需要在 Activity 中调用 getWindow().setEnterTransition() 和 getWindow().setExitTransition() 方法来指定进入和退出时的动画效果。 以上四种方法都可以用于实现 Activity 切换效果,具体选择哪种方法可以根据实际需求和个人喜好来决定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值