Android用動畫顯示或隱藏視圖
一、需求背景
有時候,我們需要在屏幕上顯示新的信息,同時移除舊的信息,一般情況下我們通過VISIBILITY或者GONE來對需要顯示或者隱藏的視圖進行設置,這樣做的壞處是顯示或者隱藏的動作變化非常突兀,而且有時候變化很快導致用戶無法注意到這些變化。這時就可以使用動畫顯示或者隱藏視圖,通常情況下使用圓形揭露動畫,淡入淡出動畫或者卡片反轉(zhuǎn)動畫。
二、創(chuàng)建淡入淡出動畫
淡入淡出動畫會逐漸淡出一個View或者ViewGroup,同時淡入另一個。此動畫適合在應用中切換內(nèi)容或者視圖的情況。這里使用ViewPropertyAnimator來創(chuàng)建這種動畫。
下面的動畫是從進度指示器切換到某些內(nèi)容文字的淡入淡出示例。
1.創(chuàng)建布局文件
<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,這一點在布局文件中已經(jīng)設置。在需要顯示淡入的View的時候,首先將其alpha設置為0,這樣可以保證View已經(jīng)顯示但是不可見。分別設置淡入的動畫和淡出的動畫,淡入的動畫將其所在的View的alpha屬性從0變化到1,淡出的動畫將其所在的View的alpha屬性從1變化到0對于淡出動畫,在動畫執(zhí)行完成后,將其的可見性設置為GONE,從而加快處理速度。
3.代碼實現(xiàn)
//開始執(zhí)行淡入淡出動畫 private fun crossFade() { //設置需要淡入的View的alpha為0,可見性為VISIBLE mBinding.tvContent.apply { alpha = 0f visibility = View.VISIBLE //通過動畫將透明度變?yōu)?.0 animate() .alpha(1.0f) .setDuration(mShortAnimationDuration.toLong()) .start() } //設置需要淡出的動畫,將其alpha從1變?yōu)?,并通過監(jiān)聽動畫執(zhí)行事件,在動畫結束后將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動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android ListView 實現(xiàn)上拉加載的示例代碼
這篇文章主要介紹了Android ListView 實現(xiàn)上拉加載的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07解析:ClickOnce通過URL傳遞參數(shù) XXX.application?a=1
本篇文章是對ClickOnce通過URL傳遞參數(shù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06Android仿美團網(wǎng)、大眾點評購買框懸浮效果修改版
這篇文章主要為大家詳細介紹了Android仿美團網(wǎng)、大眾點評購買框懸浮效果的修改版,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android實戰(zhàn)教程第二篇之簡單實現(xiàn)兩種進度條效果
這篇文章主要為大家詳細介紹了Android實戰(zhàn)教程第二篇,簡單實現(xiàn)兩種進度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11