天天看點

Activity跳轉動畫和局部控件動畫

Activity跳轉動畫有兩種實作方式。

第一種,如Activity A → Activity B, 在跳轉startActivity()之後,調用

overridePendingTransition(enterAnim, exitAnim);
           

第一個參數為B的進入動畫,第二個參數為A的退出動畫。動畫檔案一般在res/anim目錄下,下面是示例:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">  
	<!--移動動畫,X、Y軸上的位移效果 -->
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="300"/>  
	<!--類似由遠拉近放大的效果 -->
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />  
</set>  
           

第二種,在清單檔案中配置Activity的theme屬性。

android:theme="@style/main_app_pull_anim_style"
           

如果是配置在<application/>中,則整個程式的所有Activity都會顯示這個動畫。在styles.xml中:

<style name="main_app_pull_anim_style" parent="@android:style/Theme">
        <item name="android:windowAnimationStyle">@style/main_app_pull_anim</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">true</item>
    </style>
    
    <style name="main_app_pull_anim" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/main_app_pull_in_animation</item>
        <item name="android:windowExitAnimation">@anim/main_app_pull_out_animation</item>
    </style>
           

anim/main_app_pull_in_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="1000"/> 
</set>  
           

anim/main_app_pull_out_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="1000"/> 
</set>  
           

單獨控件的動畫實作:

private Runnable mPullViewRunnable = new Runnable() {
		public void run() {
			final android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) mAppPullView.getLayoutParams();
					int j = -params.topMargin;
				TranslateAnimation localTranslateAnimation = new TranslateAnimation(0.0F, 0.0F, 0.0F, j);
				localTranslateAnimation.setDuration(700L);
				localTranslateAnimation.setAnimationListener(new AnimationListener() {
							public void onAnimationEnd(Animation paramAnimation) {
//								Intent intentApp = new Intent(MainPageActivity.this,RecommendAppActivity.class);
//								startActivity(intentApp);
							}

							public void onAnimationRepeat(Animation paramAnimation) {
							}

							public void onAnimationStart(Animation paramAnimation) {
							}
						});
				mAppPullView.startAnimation(localTranslateAnimation);
		}
	};
           

mAppPullView為顯示動畫的控件,觸發時可以用mPullViewRunnable.run() 或者 用Handler 。