編輯:關於 Android 程式設計
本文將說明如何實作 Toolbar 隨著 ScrollView 滑動而透明度漸變的效果。
一、思路:監聽 ScrollView 的滑動事件,並不斷修改 Toolbar 的透明度。
二、注意事項
1. ScrollView 在 6.0(API 23)以前並沒有 scrollView.setOnScrollChangeListener(l) 方法,因此需要自訂 ScrollView,在 onScrollChanged() 中進行監聽。
2. 為了讓 Toolbar 不遮蓋 ScrollView,我們會為 ScrollView 設定 paddingTop。但設定 paddingTop 之後,當 Toolbar 透明度變為 0 時仍會佔據空間,導致出現空白。解決辦法是為 ScrollView 設定以下兩個屬性:
1〉
android:clipToPadding="false"
表示控制項的繪製範圍是否不受 padding 限制,設為 false 即代表內容可以繪製到 padding 區域中。
2〉
android:clipChildren="false"
表示子控制項是否可以超出 padding 區域(例如:設為 false 時,ScrollView 上滑時 child 可以滑出 padding 區域;設為 true 時則不能滑出)。
版面檔如下:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.dice.md.toolbar.transperent.TranslucentScrollView android:id="@+id/scrollview" android:clipToPadding="false" android:clipChildren="true" android:paddingTop="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_blue_dark" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_orange_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_blue_dark" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_orange_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_blue_dark" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_orange_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_blue_dark" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:background="@android:color/holo_orange_light" /> </LinearLayout> </com.dice.md.toolbar.transperent.TranslucentScrollView> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:background="?attr/colorPrimary" android:layout_height="?attr/actionBarSize" > </android.support.v7.widget.Toolbar> </RelativeLayout>
三、實作步驟
1. 建立回呼介面:
public interface TranslucentListener {
/** 透明度的回調
* @param alpha */
public void onTranslucent(float alpha);
}
2. 自訂 ScrollView,在 onScrollChanged 方法中回呼 TranslucentListener 介面的方法,並回傳 alpha 值:
@Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (translucentListener!=null) {
//translucentListener.onTranslucent(alpha);
}
}
3. alpha 值的計算:
// alpha = 滑出去的高度/(screenHeight/3); float heightPixels = getContext().getResources().getDisplayMetrics().heightPixels; float scrollY = getScrollY();//該值 大於0 float alpha = 1-scrollY/(heightPixels/3);// 0~1 透明度是1~0 //這裡選擇的screenHeight的1/3 是alpha改變的速率 (根據你的需要你可以自己定義)
最後在 MainActivity 中:
@Override public void onTranslucent(float alpha) {
toolbar.setAlpha(alpha);
}
透過上述做法,Toolbar 便會隨著 ScrollView 的上滑而逐漸變得透明,營造出絲滑的漸變視覺效果。其中 alpha 的變化速率取決於分母(此處選用螢幕高度的三分之一),可依實際需求自行調整。希望這份分享能對大家有所幫助。
Android 滑動組件懸浮固定頂部效果實作
下圖展示滑動至頂部後,目標按鈕區會固定在頂部的效果。 實作概念 這個效果通常會準備兩組相同的控制項:一組放在頂部,一組放在內容中間。當內容中的控制項即將滑到頂部時,就切
安卓移動開發學習筆記(二):神奇的 Web API
本次從兩個大方向來講解 Web API:一、如何在 Web 端實現 Web API?二、Android 端如何調用 Web API?至於「Web API 是什麼」「有什麼
Android 生成 keystore 的兩種方式:Eclipse 圖形化與命令行
發布前必須準備好簽名文件。這裡整理兩條路徑:一條是在 Eclipse 裡用向導點完就好,另一條是用 keytool 命令直接生成。 一、在 Eclipse 中生成 先打開
ListView 常用擴展實戰:下拉刷新、分頁加載與彈性滾動
一、實現 ListView 下拉刷新 第一步:加入頂部下拉加載介面(Header) 1、建立 head_layout 版面配置檔;2、建立一個自訂 ListView 類別