转载请注明出处: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" />
- <?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" />
- <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>
- 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);
- }
- }
源码下载,点击这里
关于Dialog和Activity的style的深入理解
结合该事例的下载地址:http://www.cnblogs.com/Jaylong/archive/2012/08/30/activity.html
<!-- 微博 -->
<style name=
"weibosdk_ContentOverlay"
parent=
"android:style/Theme.Dialog"
>
<item name=
"android:windowNoTitle"
>
true
</item> <span style=
"background-color: #00ff00;"
>没有标题
</span> <item name=
"android:windowIsTranslucent"
>
true
</item> <span style=
"background-color: #00ff00;"
>透明
</span> <item name=
"android:windowBackground"
>
@color
/weibosdk_transparent</item> <span style=
"background-color: #00ff00;"
>窗口背景色
</span> <item name=
"android:windowAnimationStyle"
>
@android
:style/Animation.Translucent</item> <span style=
"background-color: #00ff00;"
>重要,Dialog进出的显示动画
</span> <item name=
"android:windowContentOverlay"
>
@null
</item> <span style=
"background-color: #00ff00;"
>是否有覆盖
</span> <item name=
"android:backgroundDimEnabled"
>
false
</item> <span style=
"background-color: #00ff00;"
>dialog的背景是否昏暗
</span> </style>
|
以上是自定义Dialgog可以使用自定义样式:
关于<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> 重要,Dialog进出的显示动画要特别注意,可以自定义Dialog进出的动画,但是item的名字必须和下面的一样,以确定Android系统能够认识出来。
<style name=
"Animation.Translucent"
>
<item name=
"windowEnterAnimation"
>
@anim
/translucent_enter</item>
<item name=
"windowExitAnimation"
>
@anim
/translucent_exit</item>
</style>
|
----------------------------------------------
Activity 自定义样式:
<style name=
"ThemeActivity"
>
<item name=
"android:windowAnimationStyle"
>
@style
/AnimationActivity</item>
<item name=
"android:windowNoTitle"
>
true
</item>
<item name=
"android:windowFullscreen"
>
true
</item>
<item name=
"android:windowContentOverlay"
>
@null
</item>
<item name=
"android:windowIsTranslucent"
>
true
</item>
</style>
|
同样的每个item对应的name不能变,以保证Android系统能够辨认出来。
<style name=
"AnimationActivity"
parent=
"@android:style/Animation.Activity"
>
<item name=
"android:activityOpenEnterAnimation"
>
@anim
/slideinright</item> <span style=
"background-color: #00ff00;"
>第一次创建新Activity是调用的动画
</span> <item name=
"android:activityCloseExitAnimation"
>
@anim
/slideoutright</item> <span style=
"background-color: #00ff00;"
>调用finish关闭该Activity是调用的动画
</span> <item name=
"android:activityOpenExitAnimation"
>
@anim
/slideoutleft</item> <span style=
"background-color: #00ff00;"
>activity还没有finish()下退出效果</span>
<item name=
"android:activityCloseEnterAnimation"
>
@anim
/slideinleft</item> <span style=
"background-color: #00ff00;"
> 上一个activity返回进入效果
</span><br> </style>
|