本文共 1937 字,大约阅读时间需要 6 分钟。
class MyDialog(context: Context) : Dialog(context) { init { setContentView(R.layout.dialog_layout) setCanceledOnTouchOutside(false) setAnimationsBottomUp() } private fun setAnimationsBottomUp() { window?.setWindowAnimations(R.style.dialogAnimBottomUp) }}
还有一种实现对话框动画显示的方式,就是通过控制整个视图来进行动画显示.
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 } } }}
转载地址:http://dzodf.baihongyu.com/