温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android怎么用动画显示或隐藏视图

发布时间:2022-01-17 12:03:35 来源:亿速云 阅读:123 作者:iii 栏目:开发技术

这篇文章主要介绍“Android怎么用动画显示或隐藏视图”,在日常操作中,相信很多人在Android怎么用动画显示或隐藏视图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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"
                      
                      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怎么用动画显示或隐藏视图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI