其实设置Activity动画的根本也就是设置View的动画,因为Activity有一个根的View叫DecorView。下面介绍两种设置Activity的动画
一.【全局配置Activity显示动画】
1.修改Activity Theme
在styles.xml中输入以下代码
[html] view
plaincopy
- <style name="AnimationActivity" parent="@android:style/Animation.Activity">
- <item name="android:activityOpenEnterAnimation">@anim/slide_in_left</item>
- <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
- <item name="android:activityCloseEnterAnimation">@anim/slide_in_right</item>
- <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
- </style>
然后在themes.xml中
[html] view
plaincopy
- <style name="ThemeActivity">
- <item name="android:windowAnimationStyle">@style/AnimationActivity</item>
- <item name="android:windowNoTitle">true</item>
- </style>
在AndroidMainfest.xml中为Activity指定theme
2.使用代码设定
通过调用overridePendingTransition() 可以实时修改Activity的切换动画。但需注意的是:该函数必须在调用startActivity()或者finish()后立即调用,且只有效一次。
下面举例说明。
AndroidManifest.xml 注意application节点 的android:theme属性
[html] view
plaincopyprint?
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.activityanimationdemo"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".OneActivity"
- android:label="@string/title_activity_main" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".TwoActivity" >
- </activity>
- <activity android:name=".ThreeActivity" >
- </activity>
- </application>
- </manifest>
看这里,主要的部分:styles.xml
[java] view
plaincopyprint?
- <resources xmlns:android="http://schemas.android.com/apk/res/android">
- <style name="AppTheme" parent="@android:style/Theme">
- <!-- 设置没有标题 -->
- <item name="android:windowNoTitle">true</item>
- <!-- 设置activity切换动画 -->
- <item name="android:windowAnimationStyle">@style/activityAnimation</item>
- </style>
- <!-- animation 样式 -->
- <style name="activityAnimation" parent="@android:style/Animation">
- <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item>
- <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item>
- <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item>
- <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item>
- </style>
- </resources>
slide_left_in.xml
[html] view
plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="200"
- android:fromXDelta="-100.0%p"
- android:toXDelta="0.0" />
- </set>
slide_left_out.xml
[html] view
plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="200"
- android:fromXDelta="0.0"
- android:toXDelta="-100.0%p" />
- </set>
slide_right_in.xml
[html] view
plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="200"
- android:fromXDelta="100.0%p"
- android:toXDelta="0.0" />
- </set>
slide_right_out.xml
[html] view
plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="200"
- android:fromXDelta="0.0"
- android:toXDelta="100.0%p" />
- </set>
Activity 代码,很简单,就是startactivity而已不需要什么特别的
[java] view
plaincopyprint?
- package com.example.activityanimationdemo;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- /**
- *
- * @fileName OneActivity.java
- * @author naibo-liao
- * @CreateTime 2013-5-20 上午11:55:04
- */
- public class OneActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_one);
- findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startActivity(new Intent(OneActivity.this, TwoActivity.class));
- }
- });
- // 返回事件
- findViewById(R.id.btn_exit).setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- }
上一张工程图
二.【单独配置Activity显示动画】
参考代码:
eoe移动开发者社区的客户端源代码
下载地址:http://www.okbase.net/file/item/25706
如若感觉上面代码太复杂,我自己写了个demo,有需要的同学可以下载参考。这个demo也是Activity+Fragment框架的,或许你能用上哦!
http://download.csdn.net/detail/jdsjlzx/8448321