您当前的位置: 首页 >  动画

蓝不蓝编程

暂无认证

  • 2浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

蓝不蓝编程 发布时间:2020-07-30 08:54:13 ,浏览量:2

效果图

关键代码
  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中添加如下内容:

    @anim/dialog_enter_anim
    @anim/dialog_exit_anim

  1. 在res下创建anim目录,并创建如下两个文件:
  1. dialog_exit_anim.xml


    

  1. dialog_enter_anim.xml


    

完整源代码

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

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0447s