網站首頁 編程語言 正文
通常,我們會被要求實現類似支付寶首頁的特效:隨著界面的滑動,標題欄的背景透明度漸變。
在實際開發中,常見的滑動有列表RecyclerView(ListView)滑動,NestedScrollView(ScrollView)嵌套滑動等等。
本文主要從上述兩方面來探討滑動效果。
一、RecyclerView滑動標題欄漸變
廢話不多說,直接擼代碼:
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:orientation="vertical" ? ? android:background="@color/white" ? ? tools:context=".scroll_toolbar.ScrollToolBarActivity"> ? ? <!-- title標題欄--> ? ? <android.support.v7.widget.Toolbar ? ? ? ? android:id="@+id/toolbar" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:background="@color/white"> ? ? ? ? <ImageView ? ? ? ? ? ? android:id="@+id/ivBack" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:padding="@dimen/qb_px_20" ? ? ? ? ? ? android:gravity="center_vertical" ? ? ? ? ? ? android:src="@drawable/theme_toolbar_btn_back_fg_normal0" ? ? ? ? ? ? android:textColor="#ffffff" /> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvName" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#666666" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="@dimen/qb_px_20" ? ? ? ? ? ? android:text="RecyclerView控制titleBar漸變"/> ? ? </android.support.v7.widget.Toolbar> ? ? <android.support.v7.widget.RecyclerView ? ? ? ? android:id="@+id/rvZhangjie" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignParentStart="true" ? ? ? ? android:layout_marginLeft="@dimen/qb_px_50" ? ? ? ? android:layout_marginRight="@dimen/qb_px_50" ? ? ? ? android:layout_marginTop="@dimen/qb_px_20" ? ? ? ? android:background="@color/back_ground"/> </LinearLayout>
Java代碼如下:
private void toolBarColor(){ ? ? ? ? Toolbar toolbar = findViewById(R.id.toolbar); ? ? ? ? ImageView ?ivBack = findViewById(R.id.ivBack); ? ? ? ? TextView tvName = findViewById(R.id.tvName); ? ? ? ? RecyclerView ?rvZhangjie = findViewById(R.id.rvZhangjie); ? ? ? ? List<String> stringList = dealData(); ? ? ? ? ScrollAdapter scrollAdapter = new ScrollAdapter(this, stringList); ? ? ? ? LinearLayoutManager manager = new LinearLayoutManager(this); ? ? ? ? manager.setOrientation(LinearLayoutManager.VERTICAL); ? ? ? ? rvZhangjie.setLayoutManager(manager); ? ? ? ? rvZhangjie.setAdapter(scrollAdapter); ? ? ? ? rvZhangjie.addOnScrollListener(new RecyclerView.OnScrollListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onScrolled(RecyclerView recyclerView, int dx, int dy) { ? ? ? ? ? ? ??? ?//toolbar的高度 ? ? ? ? ? ? ? ? toolbarHeight = toolbar.getBottom(); ? ? ? ? ? ? ? ? //滑動的距離 ? ? ? ? ? ? ? ? mDistanceY += dy; ? ? ? ? ? ? ? ? //當滑動的距離 <= toolbar高度的時候,改變Toolbar背景色的透明度,達到漸變的效果 ? ? ? ? ? ? ? ? if (mDistanceY <= toolbarHeight) { ? ? ? ? ? ? ? ? ? ? float scale = (float) mDistanceY / toolbarHeight; ? ? ? ? ? ? ? ? ? ? float alpha = scale * 255; ? ? ? ? ? ? ? ? ? ? toolbar.setBackgroundColor(Color.argb((int) alpha, 255, 0, 0)); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? //上述雖然判斷了滑動距離與toolbar高度相等的情況,但是實際測試時發現,標題欄的背景色 ? ? ? ? ? ? ? ? ? ? //很少能達到完全不透明的情況,所以這里又判斷了滑動距離大于toolbar高度的情況, ? ? ? ? ? ? ? ? ? ? //將標題欄的顏色設置為完全不透明狀態 ? ? ? ? ? ? ? ? ? ? toolbar.setBackgroundResource(R.color.colorPrimary); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); }
上面代碼中的 dealData()方法很簡單就是想一個String型List里面添加數據,沒什么難度。
關鍵點在于給rvZhangjie.addOnScrollListener()也就是給RecyclerView設置滑動監聽,并復寫onScrolled()方法。該方法里面3個參數:
第一個RecyclerView recyclerView,這個很明顯就是目標RecyclerView;
第二個int dx,表示RecyclerView在水平X方向的相對滑動量;
第三個int dy,表示RecyclerView在垂直Y方向的相對滑動量;
我們可以通過累加計算RecyclerView滑動的距離相對于指定距離的百分比,來計算透明度的變化量:
mDistanceY += dy; float scale = (float) mDistanceY / toolbarHeight; float alpha = scale * 255;
最后再將alpha透明度值設置給ToolBar:
?toolbar.setBackgroundColor(Color.argb((int) alpha, 255, 0, 0));
二、NestedScrollView滑動標題欄漸變
其實NestedScrollView滑動漸變和RecyclerView的滑動漸變原理是一樣的,本質上都是監聽View滑動的距離,通過距離換算成透明度值。只不過二者的滑動偏移量稍有點不同。
代碼細節我就不貼出來了,就說說關鍵的對NestedScrollView的監聽和偏移量的處理:
nsvScroolBack.setOnScrollChangeListener(new View.OnScrollChangeListener() { ? ? ? ? @Override ? ? ? ? public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { ? ? ? ? ? ? //scrollY > oldScrollY:向上滑動 ? ? ? ? ? ? //scrollY < oldScrollY:向下滑動 ? ? ? ? ? ? // scrollY:滾動過的距離。 ? ? ? ? ? ? toolbarHeight = toolbar.getBottom() * 1.5f; ? ? ? ? ? ? if (scrollY <= toolbarHeight){ ? ? ? ? ? ? ? ? float scale = (float)scrollY / toolbarHeight; ? ? ? ? ? ? ? ? float alpha =scale * 255; ? ? ? ? ? ? ? ? toolbar.setBackgroundColor(Color.argb((int) alpha, 255, 0, 0)); ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? toolbar.setBackgroundColor(Color.BLUE); ? ? ? ? ? ? } ? ? ? ? } ? ? });
通過上面的代碼,很容易發現NestedScrollView滑動漸變和RecyclerView的滑動漸變就一回事。代碼實現上差別很細微。不同的是RecyclerView的滑動漸變哪里,我們要通過對dy的累加來獲得RecyclerView在垂直方向的滑動偏移量。而在NestedScrollView的滑動漸變里面,NestedScrollView在x或者y方向的滑動偏移量,系統已經幫我們計算出來了:scrollX或者scrollY。然后進行透明度的計算即可。
原文鏈接:https://blog.csdn.net/haoyuegongzi/article/details/85053509
相關推薦
- 2022-06-04 Kubernetes中Deployment的升級與回滾_云和虛擬化
- 2021-12-28 vscode搭建go開發環境案例詳解_Golang
- 2022-08-12 Python?Pandas?中的數據結構詳解_python
- 2023-04-02 go?MethodByName()不能獲取私有方法的解決_Golang
- 2022-08-01 Python?OpenCV基于HSV的顏色分割實現示例_python
- 2022-03-22 C++計算圓形、矩形和三角形的面積_C 語言
- 2022-06-21 Android幀式布局實現自動切換顏色_Android
- 2022-07-07 Python自動化辦公之Word文檔的創建與生成_python
- 最近更新
-
- 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同步修改后的遠程分支