使用動畫顯示或隱藏視圖

本篇學習筆記徹底根據官方文檔中使用動畫顯示或隱藏視圖部分學習的,基本是照着文檔學習的,點擊連接能夠直接跳轉到官方文檔部分。android

使用動畫顯示或隱藏視圖

有時候,咱們須要在屏幕上顯示新的信息,同時移除舊的信息,通常狀況下咱們經過VISIBILITY或者GONE來對須要顯示或者隱藏的視圖進行設置,這樣作的壞處是顯示或者隱藏的動做變化很是突兀,並且有時候變化很快致使用戶沒法注意到這些變化。這時就可使用動畫顯示或者隱藏視圖,一般狀況下使用圓形揭露動畫,淡入淡出動畫或者卡片反轉動畫。markdown

經過這篇筆記的學習,能夠學習到以下內容:app

  1. 經過控制視圖的alpha建立淡入淡出的動畫
    同時也會學習到使用ViewPropertyAnimator控制動畫。ide

  2. 建立卡片翻轉動畫
    同時也會學習到設置Fragment切換時的動畫。oop

  3. 建立圓形揭露動畫
    同時也會學習到ViewAnimationUtils類中提供的createCircularReveal方法的使用。佈局

建立淡入淡出動畫、

淡入淡出動畫會逐漸淡出一個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>
複製代碼
  1. 設置淡入淡出動畫:ui

    • 對於須要淡入的動畫,首先將其可見性設置爲GONE,這一點在佈局文件中已經設置。
    • 在須要顯示淡入的View的時候,首先將其alpha設置爲0,這樣能夠保證View已經顯示可是不可見。
    • 分別設置淡入的動畫和淡出的動畫,淡入的動畫將其所在的Viewalpha屬性從0變化到1,淡出的動畫將其所在的Viewalpha屬性從1變化到0
    • 對於淡出動畫,在動畫執行完成後,將其的可見性設置爲GONE,從而加快處理速度。
  2. 實現以下:google

//開始執行淡入淡出動畫
    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()
    }
複製代碼

最終執行的效果以下:

淡入淡出動畫

建立卡片翻轉動畫

卡片翻轉經過顯示模擬卡片翻轉的動畫,在內容視圖之間添加動畫,這裏經過設置Fragment切換的動畫來演示翻轉動畫的使用。

在這裏共須要四個動畫,其中兩個用於卡片正面向左淡出以及從左側淡入的動畫,另外兩個則用於從右側淡入以及從右側淡出的動畫,動畫文件以下:

  • card_flip_left_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Before rotating, immediately set the alpha to 0. -->
        <objectAnimator
            android:valueFrom="1.0"
            android:valueTo="0.0"
            android:propertyName="alpha"
            android:duration="0" />

        <!-- Rotate. -->
        <objectAnimator
            android:valueFrom="-180"
            android:valueTo="0"
            android:propertyName="rotationY"
            android:interpolator="@android:interpolator/accelerate_decelerate"
            android:duration="@integer/card_flip_time_full" />

        <!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
        <objectAnimator
            android:valueFrom="0.0"
            android:valueTo="1.0"
            android:propertyName="alpha"
            android:startOffset="@integer/card_flip_time_half"
            android:duration="1" />
    </set>
複製代碼
  • card_flip_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Rotate. -->
        <objectAnimator
            android:valueFrom="0"
            android:valueTo="180"
            android:propertyName="rotationY"
            android:interpolator="@android:interpolator/accelerate_decelerate"
            android:duration="@integer/card_flip_time_full" />

        <!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
        <objectAnimator
            android:valueFrom="1.0"
            android:valueTo="0.0"
            android:propertyName="alpha"
            android:startOffset="@integer/card_flip_time_half"
            android:duration="1" />
    </set>
複製代碼
  • card_flip_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Before rotating, immediately set the alpha to 0. -->
        <objectAnimator
            android:valueFrom="1.0"
            android:valueTo="0.0"
            android:propertyName="alpha"
            android:duration="0" />

        <!-- Rotate. -->
        <objectAnimator
            android:valueFrom="180"
            android:valueTo="0"
            android:propertyName="rotationY"
            android:interpolator="@android:interpolator/accelerate_decelerate"
            android:duration="@integer/card_flip_time_full" />

        <!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
        <objectAnimator
            android:valueFrom="0.0"
            android:valueTo="1.0"
            android:propertyName="alpha"
            android:startOffset="@integer/card_flip_time_half"
            android:duration="1" />
    </set>
複製代碼
  • card_flip_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Rotate. -->
        <objectAnimator
            android:valueFrom="0"
            android:valueTo="-180"
            android:propertyName="rotationY"
            android:interpolator="@android:interpolator/accelerate_decelerate"
            android:duration="@integer/card_flip_time_full" />

        <!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
        <objectAnimator
            android:valueFrom="1.0"
            android:valueTo="0.0"
            android:propertyName="alpha"
            android:startOffset="@integer/card_flip_time_half"
            android:duration="1" />
    </set>
複製代碼

上面的動畫文件時直接複製的文檔中的文件內容,能夠在官方文檔中直接看到。

建立好了動畫文件,還須要建立兩個Fragment的佈局文件,分別對應正面的Fragment和背面的Fragment,下面是正面Fragment的佈局文件:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#a6c">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:text="@string/card_front"
            android:textSize="20sp"
            android:textColor="#fff"
            android:textStyle="bold"
            />
</androidx.constraintlayout.widget.ConstraintLayout>
複製代碼

背面的Fragment則只顯示一張圖片:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:contentDescription="@null"
            android:src="@mipmap/ic_default_mine_banner"
            android:scaleType="centerCrop"
            />

</androidx.constraintlayout.widget.ConstraintLayout>
複製代碼

而後就能夠建立Fragment而後使用了:

//卡片切換的Fragment
    class CardFrontFragment : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_card_front, container, false)
        }
    }

    class CardBackFragment : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_card_back, container, false)
        }
    }
複製代碼

Activity中,當切換Fragment的時候,經過向事務中設置自定義動畫資源來設置Fragment切換時的動畫:

//顯示卡片背面
    private fun showCardBack() {
        if (mShowingBack) {
            //supportFragmentManager.popBackStack()
            return
        }
        mShowingBack = true
        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                R.animator.card_flip_right_in,
                R.animator.card_flip_right_out,
                R.animator.card_flip_left_in,
                R.animator.card_flip_left_out
            )
            replace(R.id.fl_content, mBackFragment)
            //addToBackStack(null)
            commit()
        }
    }

    //顯示卡片正面
    private fun showCardFront(){
        if(!mShowingBack){
            //supportFragmentManager.popBackStack()
            return
        }
        mShowingBack = false
        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                R.animator.card_flip_right_in,
                R.animator.card_flip_right_out,
                R.animator.card_flip_left_in,
                R.animator.card_flip_left_out
            )
            replace(R.id.fl_content, mFrontFragment)
            //addToBackStack(null)
            commit()
        }
    }
複製代碼

這裏的切換方式和文檔裏面的略有不一樣,可是沒有太大區別。

以後運行上面的代碼就能夠看到Fragment在切換時的動畫了:

Fragment切換時的動畫效果

建立圓形揭露動畫

當須要顯示或者隱藏一組界面元素時,可使用圓形揭露動畫。ViewAnimationUtils.createCircleReveal()方法能夠建立圓形揭露動畫,此動畫在ViewAnimationUtils類中提供,適用於5.0及更高版本。

下面的代碼演示了經過圓形揭露動畫顯示以前不可見的視圖:

首先建立須要顯示和隱藏的佈局:

<Button
                        android:id="@+id/btn_show_view_with_circle_animator"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toBottomOf="@id/fl_content"
                        app:layout_constraintLeft_toLeftOf="parent"
                        android:text="@string/show_view_with_circle_animator"
                        app:layout_constraintRight_toLeftOf="@id/btn_hide_view_with_circle_animator"
                        android:layout_marginStart="10dp"
                        android:onClick="doClick"
                        />

                <Button
                        android:id="@+id/btn_hide_view_with_circle_animator"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintBaseline_toBaselineOf="@id/btn_show_view_with_circle_animator"
                        app:layout_constraintLeft_toRightOf="@id/btn_show_view_with_circle_animator"
                        app:layout_constraintRight_toRightOf="parent"
                        android:text="@string/hide_view_with_circle_animator"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:onClick="doClick"
                        />


                <TextView
                        android:id="@+id/tv_view"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        app:layout_constraintTop_toBottomOf="@id/btn_show_view_with_circle_animator"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintDimensionRatio="w,1:1"
                        android:background="@color/color_f54949"
                        android:layout_margin="10dp"
                        />
複製代碼

下面是經過圓形揭露動畫隱藏最下面的TextView

//使用圓形揭露動畫隱藏視圖
    private fun hideViewWithCircleAnimator() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val cx = mBinding.tvView.width / 2
            val cy = mBinding.tvView.height / 2
            val anim =
                ViewAnimationUtils.createCircularReveal(mBinding.tvView, cx, cy, cx.toFloat(), 0f)
            anim.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    super.onAnimationEnd(animation)
                    //動畫運行結束後將View設置爲GONE
                    mBinding.tvView.visibility = View.GONE
                }
            })
            anim.start()
        } else {
            mBinding.tvView.visibility = View.GONE
        }
    }
複製代碼

下面的代碼演示使用圓形揭露動畫顯示最下面的TextView

//使用圓形揭露動畫顯示視圖
    private fun showViewWithCircleAnimator() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val cx = mBinding.tvView.width / 2
            val cy = mBinding.tvView.height / 2
            val anim =
                ViewAnimationUtils.createCircularReveal(mBinding.tvView, cx, cy, 0f, cx.toFloat())
            mBinding.tvView.visibility = View.VISIBLE
            anim.start()
        } else {
            mBinding.tvView.visibility = View.VISIBLE
        }
    }
複製代碼

運行效果以下:

使用圓形揭露動畫顯示或隱藏視圖

實現上面的圓形揭露動畫主要就是依賴於ViewAnimationUtils類中提供的createCircularReveal方法,這個方法接收5個參數分別是:

  1. 須要對哪一個View實現動畫,傳入相應的View便可
  2. 圓形揭露動畫的中心點的x座標
  3. 圓形揭露動畫的中心點的y座標
  4. 開始時的半徑
  5. 結束時的半徑

經過佈局文件能夠看到,上面我要對tv_view實現圓形揭露動畫,這個View的長寬時相等的,因此我這邊設置的中心點座標就是寬度和高度的一半,同時在隱藏的時候,設置的起始半徑就是寬度的一半,結束半徑就是0

相關文章
相關標籤/搜索