效果图
- 自定义对话框
class MyDialog(context: Context) : Dialog(context) {
init {
setContentView(R.layout.dialog_layout)
setCanceledOnTouchOutside(false)
setAnimationsBottomUp()
}
private fun setAnimationsBottomUp() {
window?.setWindowAnimations(R.style.dialogAnimBottomUp)
}
}
- 在styles.xml中添加如下内容:
@anim/dialog_enter_anim
@anim/dialog_exit_anim
- 在res下创建anim目录,并创建如下两个文件:
- dialog_exit_anim.xml
- 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