经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android用动画显示或隐藏视图
来源:jb51  时间:2022/1/17 12:17:20  对本文有异议

一、需求背景

有时候,我们需要在屏幕上显示新的信息,同时移除旧的信息,一般情况下我们通过VISIBILITY或者GONE来对需要显示或者隐藏的视图进行设置,这样做的坏处是显示或者隐藏的动作变化非常突兀,而且有时候变化很快导致用户无法注意到这些变化。这时就可以使用动画显示或者隐藏视图,通常情况下使用圆形揭露动画,淡入淡出动画或者卡片反转动画。

二、创建淡入淡出动画

淡入淡出动画会逐渐淡出一个View或者ViewGroup,同时淡入另一个。此动画适合在应用中切换内容或者视图的情况。这里使用ViewPropertyAnimator来创建这种动画。

下面的动画是从进度指示器切换到某些内容文字的淡入淡出示例。

1.创建布局文件

  1. <androidx.constraintlayout.widget.ConstraintLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4.  
  5. <!--淡入淡出动画-->
  6. <Button
  7. android:id="@+id/btn_use_fade_in_fade_out_animator"
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. android:layout_marginHorizontal="10dp"
  11. android:onClick="doClick"
  12. android:text="@string/use_fade_in_fade_out_animator"
  13. app:layout_constraintLeft_toLeftOf="parent"
  14. app:layout_constraintRight_toRightOf="parent"
  15. app:layout_constraintTop_toTopOf="parent" />
  16.  
  17. <androidx.constraintlayout.widget.ConstraintLayout
  18. android:layout_width="0dp"
  19. android:layout_height="0dp"
  20. app:layout_constraintDimensionRatio="w,1:1"
  21. app:layout_constraintLeft_toLeftOf="parent"
  22. app:layout_constraintRight_toRightOf="parent"
  23. app:layout_constraintTop_toBottomOf="@id/btn_use_fade_in_fade_out_animator">
  24.  
  25. <TextView
  26. android:id="@+id/tv_content"
  27. android:layout_width="0dp"
  28. android:layout_height="0dp"
  29. android:padding="16dp"
  30. android:text="@string/test_use_fade_in_fade_out_animator_text"
  31. android:visibility="gone"
  32. app:layout_constraintBottom_toBottomOf="parent"
  33. app:layout_constraintLeft_toLeftOf="parent"
  34. app:layout_constraintRight_toRightOf="parent"
  35. app:layout_constraintTop_toTopOf="parent" />
  36.  
  37. <!--进度条-->
  38. <ProgressBar
  39. android:id="@+id/loading_progress"
  40. style="?android:progressBarStyleLarge"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. app:layout_constraintBottom_toBottomOf="parent"
  44. app:layout_constraintLeft_toLeftOf="parent"
  45. app:layout_constraintRight_toRightOf="parent"
  46. app:layout_constraintTop_toTopOf="parent" />
  47.  
  48. </androidx.constraintlayout.widget.ConstraintLayout>
  49.  
  50.  
  51. </androidx.constraintlayout.widget.ConstraintLayout>

2.设置淡入淡出动画

对于需要淡入的动画,首先将其可见性设置为GONE,这一点在布局文件中已经设置。在需要显示淡入的View的时候,首先将其alpha设置为0,这样可以保证View已经显示但是不可见。分别设置淡入的动画和淡出的动画,淡入的动画将其所在的View的alpha属性从0变化到1,淡出的动画将其所在的View的alpha属性从1变化到0对于淡出动画,在动画执行完成后,将其的可见性设置为GONE,从而加快处理速度。

3.代码实现

  1. //开始执行淡入淡出动画
  2. private fun crossFade() {
  3. //设置需要淡入的View的alpha为0,可见性为VISIBLE
  4. mBinding.tvContent.apply {
  5. alpha = 0f
  6. visibility = View.VISIBLE
  7. //通过动画将透明度变为1.0
  8. animate()
  9. .alpha(1.0f)
  10. .setDuration(mShortAnimationDuration.toLong())
  11. .start()
  12. }
  13.  
  14. //设置需要淡出的动画,将其alpha从1变为0,并通过监听动画执行事件,在动画结束后将View的可见性设置为GONE
  15. mBinding.loadingProgress.animate()
  16. .alpha(0f)
  17. .setDuration(mShortAnimationDuration.toLong())
  18. .setListener(object : AnimatorListenerAdapter() {
  19. override fun onAnimationEnd(animation: Animator?) {
  20. super.onAnimationEnd(animation)
  21. mBinding.loadingProgress.visibility = View.GONE
  22. }
  23. })
  24. .start()
  25. }

总结

到此这篇关于Android用动画显示或隐藏视图的文章就介绍到这了,更多相关Android动画内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号