Android用動畫顯示或隱藏視圖
一、需求背景
有時候,我們需要在屏幕上顯示新的信息,同時移除舊的信息,一般情況下我們通過VISIBILITY或者GONE來對需要顯示或者隱藏的視圖進(jìn)行設(shè)置,這樣做的壞處是顯示或者隱藏的動作變化非常突兀,而且有時候變化很快導(dǎo)致用戶無法注意到這些變化。這時就可以使用動畫顯示或者隱藏視圖,通常情況下使用圓形揭露動畫,淡入淡出動畫或者卡片反轉(zhuǎn)動畫。
二、創(chuàng)建淡入淡出動畫
淡入淡出動畫會逐漸淡出一個View或者ViewGroup,同時淡入另一個。此動畫適合在應(yīng)用中切換內(nèi)容或者視圖的情況。這里使用ViewPropertyAnimator來創(chuàng)建這種動畫。
下面的動畫是從進(jìn)度指示器切換到某些內(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" />
<!--進(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è)置淡入淡出動畫
對于需要淡入的動畫,首先將其可見性設(shè)置為GONE,這一點在布局文件中已經(jīng)設(shè)置。在需要顯示淡入的View的時候,首先將其alpha設(shè)置為0,這樣可以保證View已經(jīng)顯示但是不可見。分別設(shè)置淡入的動畫和淡出的動畫,淡入的動畫將其所在的View的alpha屬性從0變化到1,淡出的動畫將其所在的View的alpha屬性從1變化到0對于淡出動畫,在動畫執(zhí)行完成后,將其的可見性設(shè)置為GONE,從而加快處理速度。
3.代碼實現(xiàn)
//開始執(zhí)行淡入淡出動畫
private fun crossFade() {
//設(shè)置需要淡入的View的alpha為0,可見性為VISIBLE
mBinding.tvContent.apply {
alpha = 0f
visibility = View.VISIBLE
//通過動畫將透明度變?yōu)?.0
animate()
.alpha(1.0f)
.setDuration(mShortAnimationDuration.toLong())
.start()
}
//設(shè)置需要淡出的動畫,將其alpha從1變?yōu)?,并通過監(jiān)聽動畫執(zhí)行事件,在動畫結(jié)束后將View的可見性設(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用動畫顯示或隱藏視圖的文章就介紹到這了,更多相關(guān)Android動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義多節(jié)點進(jìn)度條顯示的實現(xiàn)代碼(附源碼)
這篇文章主要介紹了Android自定義多節(jié)點進(jìn)度條顯示的實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問題
這篇文章主要介紹了Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問題的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06
Android開發(fā)之ViewFlipper自動播放圖片功能實現(xiàn)方法示例
這篇文章主要介紹了Android開發(fā)之ViewFlipper自動播放圖片功能實現(xiàn)方法,結(jié)合實例形式分析了Android使用ViewFlipper實現(xiàn)圖片播放的相關(guān)界面布局及功能實現(xiàn)技巧,需要的朋友可以參考下2019-03-03
詳解RecyclerView設(shè)置背景圖片長寬一樣(以GridLayoutManager為例)
這篇文章主要介紹了詳解RecyclerView設(shè)置背景圖片長寬一樣(以GridLayoutManager為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
全面解析Android的開源圖片框架Universal-Image-Loader
這篇文章主要介紹了Android的開源圖片框架Universal-Image-Loader,Universal-Image-Loader在GitHub上開源,其提供的圖片加載功能令人印象相當(dāng)深刻,需要的朋友可以參考下2016-04-04
Android ListView ImageView實現(xiàn)單選按鈕實例
這篇文章主要介紹了Android ListView ImageView實現(xiàn)單選按鈕的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android開發(fā)中實現(xiàn)應(yīng)用的前后臺切換效果
這篇文章主要介紹了Android開發(fā)中實現(xiàn)應(yīng)用的前后臺切換效果的方法,文章最后還附帶了監(jiān)聽程序是否進(jìn)入后臺的判斷方法,需要的朋友可以參考下2016-02-02
Android自定義wheelview實現(xiàn)滾動日期選擇器
這篇文章主要為大家詳細(xì)介紹了Android自定義wheelview實現(xiàn)滾動日期選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

