最近需求一个问题,想要在dialog消失时增加动画,之前如上一个文章中遇到的,但是最后改了实现方式,要求在特定的地方缩放,原来的dialog高度是wrap_content的,这样是无法实现的,因此首先需要将dialog的layout文件从wrap_content,改成match_parent,同时设置最外层布局背景透明。在dialog.show()之后,重新设置窗口属性
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:clipChildren="false"
- android:layoutDirection="ltr">
-
- 此处省略内部内容
-
-
-
- </RelativeLayout>
- dialog?.show()
- dialog?.apply {
- window?.setLayout(
- WindowManager.LayoutParams.MATCH_PARENT,
- WindowManager.LayoutParams.MATCH_PARENT
- )
- }
紧接着我们就可以使用动画了,尤其是平移动画,原来dialog是wrap_content的时候,如何平移超过view,就会消失。当dialog变成match_parent后,就可以正常使用平移动画了。
思路:根据位置计算平移的距离进行平移,同时缩放
- private fun setAnim(dialog: Dialog) {
- dialog.apply {
- val animView = findViewById<View>(R.id.limited_layout)
- val height = animView?.height
- //此处是在计算相应的距离等内容
- val retainHeight = (screenHeight(context) - height!!) / 2 - DensityUtil.dp2px(
- ApplicationHelper.getInstance().context,
- 84f
- ) - getStatusBarHeight(context)
- val topHeight = DensityUtil.dp2px(
- ApplicationHelper.getInstance().context,
- 80f
- )
- val transHeight = retainHeight - topHeight - DensityUtil.dp2px(
- ApplicationHelper.getInstance().context,
- 28f
- ).toFloat()
- val scaleAnimation = if (retainHeight < topHeight
- ) {
- ScaleAnimation(
- 1f, 0f,
- 1f, 0f,
- Animation.RELATIVE_TO_SELF, 1f,
- Animation.RELATIVE_TO_SELF,
- (
- (
- topHeight + DensityUtil.dp2px(
- ApplicationHelper.getInstance().context,
- 28f
- ) - retainHeight
- ) / height.toFloat()
- )
- )
- } else {
- ScaleAnimation(
- 1f, 0f,
- 1f, 0f,
- Animation.RELATIVE_TO_SELF, 1f,
- Animation.RELATIVE_TO_SELF, 0f
- )
- }
-
- val translationAnimator = if (retainHeight > topHeight) {
- TranslateAnimation(
- 0f,
- 0f,
- 0f,
- -transHeight
- )
- } else {
- null
- }
- translationAnimator?.let {
- val animationSet = AnimationSet(true)
- scaleAnimation.duration = 300
- animationSet.addAnimation(scaleAnimation)
-
- val alphaAnimation = AlphaAnimation(1f, 0f)
- alphaAnimation.duration = 300
- it.duration = 300
- animationSet.addAnimation(it)
- animationSet.addAnimation(alphaAnimation)
- animView.startAnimation(animationSet)
- animationSet.setAnimationListener(object : Animation.AnimationListener {
- override fun onAnimationStart(animation: Animation?) {
- // 动画开始时的操作
- }
-
- override fun onAnimationEnd(animation: Animation?) {
- mLimitedDialog?.let { dismiss() }
- }
-
- override fun onAnimationRepeat(animation: Animation?) {
- // 动画重复时的操作
- }
- })
- } ?: run {
- val animationSet = AnimationSet(true)
- scaleAnimation.duration = 300
- animationSet.addAnimation(scaleAnimation)
-
- val alphaAnimation = AlphaAnimation(1f, 0f)
- alphaAnimation.duration = 300
- animationSet.addAnimation(alphaAnimation)
- animView.startAnimation(animationSet)
- animationSet.setAnimationListener(object : Animation.AnimationListener {
- override fun onAnimationStart(animation: Animation?) {
- // 动画开始时的操作
- }
-
- override fun onAnimationEnd(animation: Animation?) {
- mLimitedDialog?.let { dismiss() }
- }
-
- override fun onAnimationRepeat(animation: Animation?) {
- }
- })
- }
- }
- }