Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
一.思路:監(jiān)聽ScrollView的滑動(dòng)事件 不斷的修改Toolbar的透明度
二.注意
1.ScrollView 6.0以前沒有scrollView.setOnScrollChangeListener(l)方法 所以要自定義ScrollView 在onScrollChanged()中監(jiān)聽
2.ScrollView 6.0(23)以前沒有scrollView.setOnScrollChangeListener()方法 所以要自定義ScrollView 實(shí)現(xiàn).為了Toolbar不遮蓋ScrollView我們給ScrollView設(shè)置paddingTop
但是ScrollView 設(shè)置paddintTop以后 Toolbar透明度變?yōu)?以后還占據(jù)空間 會(huì)出現(xiàn)空白,解決方法:
為ScrollView設(shè)置兩個(gè)屬性:
1〉.
android:clipToPadding="false"
表示控件的繪制范圍是否不在padding里面 false就是表示空間的繪制可以繪制到padding中
2〉
android:clipChildren="false"
表示子控件是否不能超出padding區(qū)域(比如: false :ScrollView上滑的時(shí)候 child 可以滑出padding區(qū)域 ;true:ScrollView上滑的時(shí)候 child 不能可以滑出padding區(qū)域 )
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://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. 創(chuàng)建回調(diào)接口:
public interface TranslucentListener { /** * 透明度的回調(diào) * @param alpha */ public void onTranslucent(float alpha); }
2.自定義ScrollView 在onScrollChange方法中回調(diào)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的值得計(jì)算:
// 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改變的速率 (根據(jù)你的需要你可以自己定義)
最后MainActivity中
@Override public void onTranslucent(float alpha) { toolbar.setAlpha(alpha); }
以上所述是小編給大家介紹的Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn),小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
- 修改Android FloatingActionButton的title的文字顏色及背景顏色實(shí)例詳解
- Android實(shí)現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變
- Android設(shè)置PreferenceCategory背景顏色的方法
- Android之scrollview滑動(dòng)使標(biāo)題欄漸變背景色的實(shí)例代碼
- Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果
- Android中Fab(FloatingActionButton)實(shí)現(xiàn)上下滑動(dòng)的漸變效果
- Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼
相關(guān)文章
Android自定義View實(shí)現(xiàn)微信支付密碼輸入框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)微信支付密碼輸入框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android實(shí)戰(zhàn)教程第三篇之簡單實(shí)現(xiàn)撥打電話功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第三篇之簡單實(shí)現(xiàn)撥打電話功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android app啟動(dòng)時(shí)黑屏或者白屏的原因及解決辦法
這篇文章主要介紹了Android app啟動(dòng)時(shí)黑屏或者白屏的原因及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-09-09Android webview轉(zhuǎn)PDF的方法示例
本篇文章主要介紹了Android webview轉(zhuǎn)PDF的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式
這篇文章主要介紹了Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握Android存儲(chǔ)數(shù)據(jù)的方法,需要的朋友可以參考下2017-10-10Flutter 系統(tǒng)是如何實(shí)現(xiàn)ExpansionPanelList的示例代碼
Flutter組件有一個(gè)很大的特色,那就是很多復(fù)雜的組件都是通過一個(gè)一個(gè)小組件拼裝而成的,今天就來說說系統(tǒng)的ExpansionPanelList是如何實(shí)現(xiàn)的,需要的朋友可以參考下2020-05-05Android源碼學(xué)習(xí)之工廠方法模式應(yīng)用及優(yōu)勢介紹
工廠方法模式定義:定義一個(gè)用于創(chuàng)建對象的接口,讓子類決定實(shí)例化哪一個(gè)類。工廠方法使一個(gè)類的實(shí)例化延遲到其子類,感興趣的朋友可以了解下哦2013-01-01Android控件系列之相冊Gallery&Adapter適配器入門&控件縮放動(dòng)畫入門
本文介紹了如何使用Gallery打造簡單的相冊,并實(shí)現(xiàn)了與用戶點(diǎn)擊的互動(dòng)動(dòng)畫,并介紹了適配器的原理。您可以在此基礎(chǔ)上修改,實(shí)現(xiàn)自己的相冊,嵌入到任何程序中都會(huì)增色不少2012-11-11