Android使用动画显示或隐藏视图

一、需求背景

有时候,我们需要在屏幕上显示新的信息,同时移除旧的信息,一般情况下我们通过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()
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网小熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值