如何添加Activity的进出场动画
必须在 StartActivity() 或 finish() 之后立即调用overridePendingTransition
方法
public static void actionStart(Activity activity, int id, int category) {
Intent intent = new Intent(activity, QLibraryDetialActivity.class);
intent.putExtra("id", id);
intent.putExtra("category", category);
activity.startActivity(intent);
// 设置切换动画,从右边进入,左边退出;
// 两个activity切换,第一个activity从0位左移退出,第二个activity从100%位左移进入(下面参数1是第二个activity的动画)
activity.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
动画xml slide_in_right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0"/>
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="1.0"/>
</set>
动画xml slide_out_left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p"/>
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="1.0"/>
</set>
用在finish()后:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
myFinish();
break;
}
return super.onKeyDown(keyCode, event);
}
private void myFinish() {
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
动画slide_in_left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="-100%p"
android:toXDelta="0"/>
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="1.0"/>
</set>
动画slide_out_right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p"/>
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="1.0"/>
</set>