網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、需求背景
有時(shí)候,我們需要在屏幕上顯示新的信息,同時(shí)移除舊的信息,一般情況下我們通過VISIBILITY或者GONE來(lái)對(duì)需要顯示或者隱藏的視圖進(jìn)行設(shè)置,這樣做的壞處是顯示或者隱藏的動(dòng)作變化非常突兀,而且有時(shí)候變化很快導(dǎo)致用戶無(wú)法注意到這些變化。這時(shí)就可以使用動(dòng)畫顯示或者隱藏視圖,通常情況下使用圓形揭露動(dòng)畫,淡入淡出動(dòng)畫或者卡片反轉(zhuǎn)動(dòng)畫。
二、創(chuàng)建淡入淡出動(dòng)畫
淡入淡出動(dòng)畫會(huì)逐漸淡出一個(gè)View或者ViewGroup,同時(shí)淡入另一個(gè)。此動(dòng)畫適合在應(yīng)用中切換內(nèi)容或者視圖的情況。這里使用ViewPropertyAnimator來(lái)創(chuàng)建這種動(dòng)畫。
下面的動(dòng)畫是從進(jìn)度指示器切換到某些內(nèi)容文字的淡入淡出示例。
1.創(chuàng)建布局文件
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!--淡入淡出動(dòng)畫--> <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)畫
對(duì)于需要淡入的動(dòng)畫,首先將其可見性設(shè)置為GONE,這一點(diǎn)在布局文件中已經(jīng)設(shè)置。在需要顯示淡入的View的時(shí)候,首先將其alpha設(shè)置為0,這樣可以保證View已經(jīng)顯示但是不可見。分別設(shè)置淡入的動(dòng)畫和淡出的動(dòng)畫,淡入的動(dòng)畫將其所在的View的alpha屬性從0變化到1,淡出的動(dòng)畫將其所在的View的alpha屬性從1變化到0對(duì)于淡出動(dòng)畫,在動(dòng)畫執(zhí)行完成后,將其的可見性設(shè)置為GONE,從而加快處理速度。
3.代碼實(shí)現(xiàn)
//開始執(zhí)行淡入淡出動(dòng)畫 private fun crossFade() { //設(shè)置需要淡入的View的alpha為0,可見性為VISIBLE mBinding.tvContent.apply { alpha = 0f visibility = View.VISIBLE //通過動(dòng)畫將透明度變?yōu)?.0 animate() .alpha(1.0f) .setDuration(mShortAnimationDuration.toLong()) .start() } //設(shè)置需要淡出的動(dòng)畫,將其alpha從1變?yōu)?,并通過監(jiān)聽動(dòng)畫執(zhí)行事件,在動(dòng)畫結(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é)
原文鏈接:https://blog.csdn.net/weixin_42600398/article/details/122525505
相關(guān)推薦
- 2023-07-31 el-input自動(dòng)獲取焦點(diǎn)并選中值
- 2022-11-09 Android?使用maven?publish插件發(fā)布產(chǎn)物(aar)流程實(shí)踐_Android
- 2022-10-26 一文解析?Golang?sync.Once?用法及原理_Golang
- 2022-07-25 C++細(xì)講深淺拷貝與初始化列表如何操作_C 語(yǔ)言
- 2023-03-02 C++回溯算法之深度優(yōu)先搜索詳細(xì)介紹_C 語(yǔ)言
- 2022-06-22 python?實(shí)現(xiàn)?mp3Play?音頻播放_(tái)python
- 2022-05-28 Entity?Framework?Core生成數(shù)據(jù)庫(kù)表_實(shí)用技巧
- 2022-11-14 C++中4種管理數(shù)據(jù)內(nèi)存的方式總結(jié)_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支