Android 底部弹出Activity

网上百度了很多方案,很多都是过时的,下面介绍我的方案:

第一步:新建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);
    }

最后上一下我的效果图:

这里写图片描述

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值