(四十一)Activity切换动画
下面介绍两种 方法设置Activity的切换动画
一、修改Activity Theme
1、在styles.xml中定义切换动画
<style name="AnimationActivity" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/activity_open_enter</item>
<item name="android:activityOpenExitAnimation">@anim/activity_open_exit</item>
<item name="android:activityCloseEnterAnimation">@anim/activity_close_enter</item>
<item name="android:activityCloseExitAnimation">@anim/activity_close_exit</item>
</style>
注意需要继承自 @android:style/Animation.Activity
。具体这4个属性什么意思呢?假设我们有 2 个 Activity,分别是 A1 和 A2:
- 当我们从 A1 启动 A2 时,A2 出现在屏幕上,这个动画叫做
android:activityOpenEnterAnimation
- 当我们从 A1 启动 A2 时,A1 从屏幕上消失,这个动画叫做
android:activityOpenExitAnimation
- 当我们从 A2 退出回到 A1 时,A1 出现在屏幕上,这个叫做
android:activityCloseEnterAnimation
- 当我们从 A2 退出回到 A1 时,A2 从屏幕上消失,这个叫做
android:activityCloseExitAnimation
2、在style.xml中定义自定义主题,使用前面定义的切换动画
<style name="CustomTheme" style="@android:style/Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@style/AnimationActivity</item>
</style>
3、在AndroidManifest.xml中在application节点下或activity节点下指定使用前面定义的主题
android:theme="@style/CustomTheme"
二、使用代码设定
通过调用overridePendingTransition() 来实现两个 Activity 切换时的动画。有两个参数:一部分是第一个activity退出时的动画;
另外一部分时第二个activity进入时的动画;。这里需要特别说明的是,关于overridePendingTransition这个函数,有两点需要注意:
1.它必需紧挨着startActivity()或者finish()函数之后调用"
2.它只在android2.0以及以上版本上适用
三、Android的Activity屏幕切换动画——左右滑动切换动画
3.1程序结构图
3.2 MainActivity.java的代码如下所示:
package com.example.testdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button bt_click; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_click = (Button) findViewById(R.id.bt_click); bt_click.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); overridePendingTransition(R.anim.in_to_right,0); } }); } }
3.3 SecondActivity.java的代码如下所示:
package com.example.testdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; public class SecondActivity extends Activity { private Button bt_click; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two_activity_main); bt_click = (Button) findViewById(R.id.bt_click); bt_click.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(SecondActivity.this, MainActivity.class); startActivity(intent); overridePendingTransition(0, R.anim.out_to_left); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { Intent intent = new Intent(SecondActivity.this, MainActivity.class); startActivity(intent); overridePendingTransition(0, R.anim.out_to_left); } return super.onKeyDown(keyCode, event); } }
3.4 in_to_right.xml的代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="500"/> </set>
3.5 out_to_left.xml的代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="0%p" android:toXDelta="-100%p" /> </set>
3.6 activity_main.xml的代码如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/bt_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="按钮" /> </RelativeLayout>
3.7 two_activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <Button android:id="@+id/bt_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="第二个界面" /> </RelativeLayout>
3.8 AndroidManifest.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:launchMode="singleTask" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" > </activity> </application> </manifest>