網站首頁 編程語言 正文
一、需求背景
有時候,我們需要在屏幕上顯示新的信息,同時移除舊的信息,一般情況下我們通過VISIBILITY或者GONE來對需要顯示或者隱藏的視圖進行設置,這樣做的壞處是顯示或者隱藏的動作變化非常突兀,而且有時候變化很快導致用戶無法注意到這些變化。這時就可以使用動畫顯示或者隱藏視圖,通常情況下使用圓形揭露動畫,淡入淡出動畫或者卡片反轉動畫。
二、創建淡入淡出動畫
淡入淡出動畫會逐漸淡出一個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>
2.設置淡入淡出動畫
對于需要淡入的動畫,首先將其可見性設置為GONE,這一點在布局文件中已經設置。在需要顯示淡入的View的時候,首先將其alpha設置為0,這樣可以保證View已經顯示但是不可見。分別設置淡入的動畫和淡出的動畫,淡入的動畫將其所在的View的alpha屬性從0變化到1,淡出的動畫將其所在的View的alpha屬性從1變化到0對于淡出動畫,在動畫執行完成后,將其的可見性設置為GONE,從而加快處理速度。
3.代碼實現
//開始執行淡入淡出動畫 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() }
總結
原文鏈接:https://blog.csdn.net/weixin_42600398/article/details/122525505
相關推薦
- 2023-07-13 el-table實現多選及反選
- 2022-12-28 React?Server?Component混合式渲染問題詳解_React
- 2022-05-08 Python+matplotlib實現堆疊圖的繪制_python
- 2022-10-17 Python?pywin32實現word與Excel的處理_python
- 2023-05-13 python中數字列表轉化為數字字符串的實例代碼_python
- 2022-10-20 Python中使用Frozenset對象的案例詳解_python
- 2022-11-24 Clickhouse系列之整合Hive數據倉庫示例詳解_數據庫其它
- 2021-12-23 使用go?net實現簡單的redis通信協議_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支