利用Theme自定义Activity间的切换动画

转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/22990643

大多Android系统默认Activity间的动画切换效果为,右边滑入,左边滑出;有时候我们的需求可能是要求所有Activity的切换为淡入淡出的效果,这时候就可能需要改变一下默认的切换风格。

下面开始实现:

首先在res文件夹下建立anim文件夹,然后在里面建立fade_in.xml和fade_out.xml两个动画资源

fade_in.xml

[html] view plain copy
 
在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="300"  
  4.     android:fromAlpha="0.0"  
  5.     android:interpolator="@android:anim/accelerate_interpolator"  
  6.     android:toAlpha="1.0" />  

fade_out.xml

[html] view plain copy
 
在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="300"  
  4.     android:fromAlpha="1.0"  
  5.     android:interpolator="@android:anim/accelerate_interpolator"  
  6.     android:toAlpha="0.0" />  

然后在values文件夹下的styles.xml中的resources标签内写:

[html] view plain copy
 
在CODE上查看代码片派生到我的代码片
  1. <style name="Anim_fade" parent="android:Theme.NoTitleBar">  
  2.        <item name="android:windowAnimationStyle">@style/fade</item>  
  3.    </style>  
  4.   
  5.    <style name="fade" parent="@android:style/Animation.Activity">  
  6.        <item name="android:activityOpenEnterAnimation">@anim/fade_in</item>  
  7.        <item name="android:activityOpenExitAnimation">@anim/fade_out</item>  
  8.        <item name="android:activityCloseEnterAnimation">@anim/fade_in</item>  
  9.        <item name="android:activityCloseExitAnimation">@anim/fade_out</item>  
  10.    </style>  


最后一步在AndroidManifest.xml中的Activity的声明上加入android:theme="@style/Anim_fade"

[html] view plain copy
 
在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.customanimationforactivity"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="10"  
  9.         android:targetSdkVersion="10" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@android:style/Theme.NoTitleBar" >  
  16.         <activity  
  17.             android:name="com.example.customanimationforactivity.MainActivity"  
  18.             android:label="@string/app_name"  
  19.             android:theme="@style/Anim_fade" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.         <activity android:name=".AppActivity" android:theme="@style/Anim_fade" >  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

贴下Splash Activity的代码:

[java] view plain copy
 
在CODE上查看代码片派生到我的代码片
  1. package com.example.customanimationforactivity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7.   
  8. public class MainActivity extends Activity  
  9. {  
  10.   
  11.     private Handler handler = new Handler();  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         handler.postDelayed(new Runnable()  
  19.         {  
  20.             @Override  
  21.             public void run()  
  22.             {  
  23.                 Intent intent = new Intent(MainActivity.this, AppActivity.class);  
  24.                 startActivity(intent);  
  25.                 finish();  
  26.             }  
  27.         }, 1000);  
  28.   
  29.     }  
  30.   
  31. }  

源码下载,点击这里

原文地址:https://www.cnblogs.com/ldq2016/p/6248906.html