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

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

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

下面开始实现:

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

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0" />
fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromAlpha="1.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="0.0" />
然后在values文件夹下的styles.xml中的resources标签内写:

 <style name="Anim_fade" parent="android:Theme.NoTitleBar">
        <item name="android:windowAnimationStyle">@style/fade</item>
    </style>

    <style name="fade" parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/fade_in</item>
        <item name="android:activityOpenExitAnimation">@anim/fade_out</item>
        <item name="android:activityCloseEnterAnimation">@anim/fade_in</item>
        <item name="android:activityCloseExitAnimation">@anim/fade_out</item>
    </style>

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.customanimationforactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name="com.example.customanimationforactivity.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Anim_fade" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AppActivity" android:theme="@style/Anim_fade" >
        </activity>
    </application>

</manifest>
贴下Splash Activity的代码:

package com.example.customanimationforactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends Activity
{

	private Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				Intent intent = new Intent(MainActivity.this, AppActivity.class);
				startActivity(intent);
				finish();
			}
		}, 1000);

	}

}

源码下载,点击这里



posted @ 2014-04-05 17:31  IT专业户  阅读(175)  评论(0编辑  收藏  举报