安卓自定义对话框(弹出框)并添加显示动画

效果图

关键代码

  1. 自定义对话框
class MyDialog(context: Context) : Dialog(context) {
    init {
        setContentView(R.layout.dialog_layout)
        setCanceledOnTouchOutside(false)
        setAnimationsBottomUp()
    }

    private fun setAnimationsBottomUp() {
        window?.setWindowAnimations(R.style.dialogAnimBottomUp)
    }
}
  1. 在styles.xml中添加如下内容:
<style name="dialogAnimBottomUp" mce_bogus="1" parent="android:Animation">
    <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
    <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
</style>
  1. 在res下创建anim目录,并创建如下两个文件:
  1. dialog_exit_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%p" />
</set>
  1. dialog_enter_anim.xml
<?xml version="1.0" encoding="utf-8"?><!-- 弹出时动画 -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="100%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0%p" />
</set>

完整源代码

https://gitee.com/cxyzy1/custom_dialog

附录

还有一种实现对话框动画显示的方式,就是通过控制整个视图来进行动画显示.

简要代码:

class BottomDialogWithAnim1(context: Context) : Dialog(context, R.style.common_dialog) {
    init {
        setContentView(R.layout.dialog_bottom_anim1)
        changeDialogStyle()
        showWithMoveAnim(rootLayout)
    }

    /**
     * 动画方式显示,从底部向上显示出来。
     * 如果其他想要其他方式,则修改里面的animation实现即可。
     */
    private fun showWithMoveAnim(srcView: View) {
        val animateTime = 300L
        val animation = TranslateAnimation(0f, 0f, getWindowHeight(context) - srcView.height.toFloat(), 0f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }

    private fun getWindowHeight(context: Context): Int {
        val point = Point()
        val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = manager.defaultDisplay
        if (Build.VERSION.SDK_INT > 16) {
            display.getRealSize(point)
        } else {
            display.getSize(point)
        }

        return Point(point).y
    }


    /**
     * 设置dialog居下占满屏幕
     */
    private fun changeDialogStyle() {
        window?.let {
            val params = it.attributes
            if (params != null) {
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT
                params.width = ViewGroup.LayoutParams.MATCH_PARENT
                params.gravity = Gravity.BOTTOM
                it.attributes = params
            }
        }
    }
}

完整源代码

https://gitee.com/cxyzy1/custom_dialog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值