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 。