• Dialog动画相关


    最近需求一个问题,想要在dialog消失时增加动画,之前如上一个文章中遇到的,但是最后改了实现方式,要求在特定的地方缩放,原来的dialog高度是wrap_content的,这样是无法实现的,因此首先需要将dialog的layout文件从wrap_content,改成match_parent,同时设置最外层布局背景透明。在dialog.show()之后,重新设置窗口属性

    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:gravity="center"
    6. android:clipChildren="false"
    7. android:layoutDirection="ltr">
    8. 此处省略内部内容
    9. </RelativeLayout>
    1. dialog?.show()
    2. dialog?.apply {
    3. window?.setLayout(
    4. WindowManager.LayoutParams.MATCH_PARENT,
    5. WindowManager.LayoutParams.MATCH_PARENT
    6. )
    7. }

    紧接着我们就可以使用动画了,尤其是平移动画,原来dialog是wrap_content的时候,如何平移超过view,就会消失。当dialog变成match_parent后,就可以正常使用平移动画了。

    思路:根据位置计算平移的距离进行平移,同时缩放

    1. private fun setAnim(dialog: Dialog) {
    2. dialog.apply {
    3. val animView = findViewById<View>(R.id.limited_layout)
    4. val height = animView?.height
    5. //此处是在计算相应的距离等内容
    6. val retainHeight = (screenHeight(context) - height!!) / 2 - DensityUtil.dp2px(
    7. ApplicationHelper.getInstance().context,
    8. 84f
    9. ) - getStatusBarHeight(context)
    10. val topHeight = DensityUtil.dp2px(
    11. ApplicationHelper.getInstance().context,
    12. 80f
    13. )
    14. val transHeight = retainHeight - topHeight - DensityUtil.dp2px(
    15. ApplicationHelper.getInstance().context,
    16. 28f
    17. ).toFloat()
    18. val scaleAnimation = if (retainHeight < topHeight
    19. ) {
    20. ScaleAnimation(
    21. 1f, 0f,
    22. 1f, 0f,
    23. Animation.RELATIVE_TO_SELF, 1f,
    24. Animation.RELATIVE_TO_SELF,
    25. (
    26. (
    27. topHeight + DensityUtil.dp2px(
    28. ApplicationHelper.getInstance().context,
    29. 28f
    30. ) - retainHeight
    31. ) / height.toFloat()
    32. )
    33. )
    34. } else {
    35. ScaleAnimation(
    36. 1f, 0f,
    37. 1f, 0f,
    38. Animation.RELATIVE_TO_SELF, 1f,
    39. Animation.RELATIVE_TO_SELF, 0f
    40. )
    41. }
    42. val translationAnimator = if (retainHeight > topHeight) {
    43. TranslateAnimation(
    44. 0f,
    45. 0f,
    46. 0f,
    47. -transHeight
    48. )
    49. } else {
    50. null
    51. }
    52. translationAnimator?.let {
    53. val animationSet = AnimationSet(true)
    54. scaleAnimation.duration = 300
    55. animationSet.addAnimation(scaleAnimation)
    56. val alphaAnimation = AlphaAnimation(1f, 0f)
    57. alphaAnimation.duration = 300
    58. it.duration = 300
    59. animationSet.addAnimation(it)
    60. animationSet.addAnimation(alphaAnimation)
    61. animView.startAnimation(animationSet)
    62. animationSet.setAnimationListener(object : Animation.AnimationListener {
    63. override fun onAnimationStart(animation: Animation?) {
    64. // 动画开始时的操作
    65. }
    66. override fun onAnimationEnd(animation: Animation?) {
    67. mLimitedDialog?.let { dismiss() }
    68. }
    69. override fun onAnimationRepeat(animation: Animation?) {
    70. // 动画重复时的操作
    71. }
    72. })
    73. } ?: run {
    74. val animationSet = AnimationSet(true)
    75. scaleAnimation.duration = 300
    76. animationSet.addAnimation(scaleAnimation)
    77. val alphaAnimation = AlphaAnimation(1f, 0f)
    78. alphaAnimation.duration = 300
    79. animationSet.addAnimation(alphaAnimation)
    80. animView.startAnimation(animationSet)
    81. animationSet.setAnimationListener(object : Animation.AnimationListener {
    82. override fun onAnimationStart(animation: Animation?) {
    83. // 动画开始时的操作
    84. }
    85. override fun onAnimationEnd(animation: Animation?) {
    86. mLimitedDialog?.let { dismiss() }
    87. }
    88. override fun onAnimationRepeat(animation: Animation?) {
    89. }
    90. })
    91. }
    92. }
    93. }

  • 相关阅读:
    pandas.eval()/pandas.Series()/lambda/itertools.product
    信息论作业 P02014222 焦子阳
    层次分明井然有条,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang包管理机制(package)EP10
    SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
    2.4 - 网络协议 - TCP协议工作原理,报文格式,抓包实战,UDP报文,UDP检错原理
    为数据安全产业发声 | 2022数据安全技术大会获媒体高频关注
    6-JS的Fetch 跨域问题
    机器学习与深度学习
    使用C#在Windows上调用7-zip压缩文件
    Kotlin 核心语法,为什么选择Kotlin ?
  • 原文地址:https://blog.csdn.net/qq_31390999/article/details/133916903