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