一、需求背景
有时候,我们需要在屏幕上显示新的信息,同时移除旧的信息,一般情况下我们通过VISIBILITY或者GONE来对需要显示或者隐藏的视图进行设置,这样做的坏处是显示或者隐藏的动作变化非常突兀,而且有时候变化很快导致用户无法注意到这些变化。这时就可以使用动画显示或者隐藏视图,通常情况下使用圆形揭露动画,淡入淡出动画或者卡片反转动画。
二、创建淡入淡出动画
淡入淡出动画会逐渐淡出一个View或者ViewGroup,同时淡入另一个。此动画适合在应用中切换内容或者视图的情况。这里使用ViewPropertyAnimator来创建这种动画。
下面的动画是从进度指示器切换到某些内容文字的淡入淡出示例。
1.创建布局文件
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <!--淡入淡出动画-->
- <Button
- android:id="@+id/btn_use_fade_in_fade_out_animator"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginHorizontal="10dp"
- android:onClick="doClick"
- android:text="@string/use_fade_in_fade_out_animator"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="0dp"
- android:layout_height="0dp"
- app:layout_constraintDimensionRatio="w,1:1"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toBottomOf="@id/btn_use_fade_in_fade_out_animator">
-
- <TextView
- android:id="@+id/tv_content"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:padding="16dp"
- android:text="@string/test_use_fade_in_fade_out_animator_text"
- android:visibility="gone"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <!--进度条-->
- <ProgressBar
- android:id="@+id/loading_progress"
- style="?android:progressBarStyleLarge"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- </androidx.constraintlayout.widget.ConstraintLayout>
-
-
- </androidx.constraintlayout.widget.ConstraintLayout>
2.设置淡入淡出动画
对于需要淡入的动画,首先将其可见性设置为GONE,这一点在布局文件中已经设置。在需要显示淡入的View的时候,首先将其alpha设置为0,这样可以保证View已经显示但是不可见。分别设置淡入的动画和淡出的动画,淡入的动画将其所在的View的alpha属性从0变化到1,淡出的动画将其所在的View的alpha属性从1变化到0对于淡出动画,在动画执行完成后,将其的可见性设置为GONE,从而加快处理速度。
3.代码实现
- //开始执行淡入淡出动画
- private fun crossFade() {
- //设置需要淡入的View的alpha为0,可见性为VISIBLE
- mBinding.tvContent.apply {
- alpha = 0f
- visibility = View.VISIBLE
- //通过动画将透明度变为1.0
- animate()
- .alpha(1.0f)
- .setDuration(mShortAnimationDuration.toLong())
- .start()
- }
-
- //设置需要淡出的动画,将其alpha从1变为0,并通过监听动画执行事件,在动画结束后将View的可见性设置为GONE
- mBinding.loadingProgress.animate()
- .alpha(0f)
- .setDuration(mShortAnimationDuration.toLong())
- .setListener(object : AnimatorListenerAdapter() {
- override fun onAnimationEnd(animation: Animator?) {
- super.onAnimationEnd(animation)
- mBinding.loadingProgress.visibility = View.GONE
- }
- })
- .start()
- }
总结
到此这篇关于Android用动画显示或隐藏视图的文章就介绍到这了,更多相关Android动画内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!