网上百度了很多方案,很多都是过时的,下面介绍我的方案:
第一步:新建3个anim文件
1 bottom_in.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="800"
/>
</set>
2 bottom_out.xml (底部弹出动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="25"
android:toYDelta="100%p"
android:duration="800"
/>
</set>
3 bottom_silent.xml (静止动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="0"
android:duration="1500" (注意这里的时间要比上面两个的长,否则会出现黑色背景)
/>
</set>
第二步: 要启动Activity的地方加上下面的代码
startActivity(new Intent(ActionBarActivity.this,DialogActivity.class));
overridePendingTransition(R.anim.bottom_in,R.anim.bottom_silent);
注意:很多博客都建议大家把后一参数设置为0,其实这样是无效的
* @param enterAnim A resource ID of the animation resource to use for
* the incoming activity. Use 0 for no animation.
* @param exitAnim A resource ID of the animation resource to use for
* the outgoing activity. Use 0 for no animation.
源码的注释上可以看出,第一个参数是被启动的Activity进入的动画,而第二个是当前Activity退出的动画,如果设置为0,走的是默认动画,会出现黑色背景,非常影响UI
第二步: 被启动的Activity的finish方法加上下面的代码
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.bottom_silent,R.anim.bottom_out);
}
最后上一下我的效果图: