Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實作 Toolbar 隨 ScrollView 滑動的透明度漸變效果

編輯:關於 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 的變化速率取決於分母(此處選用螢幕高度的三分之一),可依實際需求自行調整。希望這份分享能對大家有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved