Android ScrollView滑動(dòng)實(shí)現(xiàn)仿QQ空間標(biāo)題欄漸變
今天來研究的是ScrollView-滾動(dòng)視圖,滾動(dòng)視圖又分橫向滾動(dòng)視圖(HorizontalScrollView)和縱向滾動(dòng)視圖(ScrollView),今天主要研究縱向的。相信大家在開發(fā)中經(jīng)常用到,ScrollView的功能已經(jīng)很強(qiáng)大了,但是仍然滿足不了我們腦洞大開的UI設(shè)計(jì)師們,所以我們要自定義…本篇文章主要講監(jiān)聽ScrollView的滑動(dòng)實(shí)現(xiàn)仿QQ空間標(biāo)題欄漸變,先看一下效果圖:
好了我們切入主題。
有可能你不知道的那些ScrollView屬性
•android:scrollbars
設(shè)置滾動(dòng)條顯示。none(隱藏),horizontal(水平),vertical(垂直)
•android:scrollbarStyle
設(shè)置滾動(dòng)條的風(fēng)格和位置。設(shè)置值:insideOverlay、insideInset、outsideOverlay、outsideInset
•android:scrollbarThumbHorizontal
設(shè)置水平滾動(dòng)條的drawable。
•android:soundEffectsEnabled
設(shè)置點(diǎn)擊或觸摸時(shí)是否有聲音效果
•android:fadingEdge
設(shè)置拉滾動(dòng)條時(shí),邊框漸變的放向。none(邊框顏色不變),horizontal(水平方向顏色變淡),vertical(垂直方向顏色變淡)。參照fadingEdgeLength的效果圖 android:fadingEdgeLength 設(shè)置邊框漸變的長(zhǎng)度
•android:scrollX
以像素為單位設(shè)置水平方向滾動(dòng)的的偏移值,在GridView中可看的這個(gè)效果
•android:scrollY
以像素為單位設(shè)置垂直方向滾動(dòng)的的偏移值
•android:scrollbarAlwaysDrawHorizontalTrack
設(shè)置是否始終顯示垂直滾動(dòng)條
•android:scrollbarDefaultDelayBeforeFade
設(shè)置N毫秒后開始淡化,以毫秒為單位。
以上這些屬性有興趣的可以去研究一下,這里就不詳細(xì)講了。很多屬性并不常用,下面說說我們經(jīng)常用的,怎樣監(jiān)聽ScrollView的滑動(dòng)并實(shí)現(xiàn)標(biāo)題欄的漸變?
ScrollView滑動(dòng)監(jiān)聽:
Google并沒有給我們提供ScrollView的滑動(dòng)距離、是否滑動(dòng)到布局底部、頂部的方法,但是提供了一個(gè)onScrollChanged方法:
@Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); //todo: } }
通過查看源碼注釋,
/**
* This is called in response to an internal scroll in this view (i.e., the
* view scrolled its own contents). This is typically as a result of
* {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
* called.
*
* @param l Current horizontal scroll origin.
* @param t Current vertical scroll origin.
* @param oldl Previous horizontal scroll origin.
* @param oldt Previous vertical scroll origin.
*/
我們可以知道這個(gè)方法的參數(shù)分別為:
l:當(dāng)前橫向滑動(dòng)距離
t:當(dāng)前縱向滑動(dòng)距離
oldl:之前橫向滑動(dòng)距離
oldt:之前縱向滑動(dòng)距離
但是這個(gè)方法我們不可以調(diào)用,我們可以重寫接口或者重寫ScrollView暴露該方法:
package com.hankkin.gradationscroll; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; /** * 帶滾動(dòng)監(jiān)聽的scrollview * */ public class GradationScrollView extends ScrollView { public interface ScrollViewListener { void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy); } private ScrollViewListener scrollViewListener = null; public GradationScrollView(Context context) { super(context); } public GradationScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public GradationScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } } }
設(shè)置標(biāo)題漸變
滾動(dòng)監(jiān)聽暴露出來我們就該去設(shè)置標(biāo)題欄隨著ScrollView的滑動(dòng)來改變標(biāo)題欄的透明度實(shí)現(xiàn)漸變:
我們先看一下布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hankkin.gradationtitlebar.QQSpeakActivity"> <com.hankkin.gradationscroll.GradationScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iv_banner" android:scaleType="fitXY" android:src="@drawable/banner3" android:layout_width="match_parent" android:layout_height="200dp" /> <com.hankkin.gradationscroll.NoScrollListview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.hankkin.gradationscroll.NoScrollListview> </LinearLayout> </com.hankkin.gradationscroll.GradationScrollView> <TextView android:paddingBottom="10dp" android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center|bottom" android:text="我是標(biāo)題" android:textSize="18sp" android:textColor="@color/transparent" android:background="#00000000" /> </RelativeLayout>
最外層是我們自定義的ScrollView,包裹著一張背景圖片和一個(gè)ListView(ListView重寫為不可以滑動(dòng)),然后布局的上面有一個(gè)TextView當(dāng)做標(biāo)題欄,你也可以用布局。
然后我們需要獲取圖片的高度,并且設(shè)置滾動(dòng)監(jiān)聽,隨著滾動(dòng)的距離來設(shè)置標(biāo)題欄的顏色透明度和字體顏色的透明度
/** * 獲取頂部圖片高度后,設(shè)置滾動(dòng)監(jiān)聽 */ private void initListeners() { ViewTreeObserver vto = ivBanner.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { textView.getViewTreeObserver().removeGlobalOnLayoutListener( this); height = ivBanner.getHeight(); scrollView.setScrollViewListener(QQSpeakActivity.this); } }); }
/** * 滑動(dòng)監(jiān)聽 * @param scrollView * @param x * @param y * @param oldx * @param oldy */ @Override public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) { // TODO Auto-generated method stub if (y <= 0) { //設(shè)置標(biāo)題的背景顏色 textView.setBackgroundColor(Color.argb((int) 0, 144,151,166)); } else if (y > 0 && y <= height) { //滑動(dòng)距離小于banner圖的高度時(shí),設(shè)置背景和字體顏色顏色透明度漸變 float scale = (float) y / height; float alpha = (255 * scale); textView.setTextColor(Color.argb((int) alpha, 255,255,255)); textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166)); } else { //滑動(dòng)到banner下面設(shè)置普通顏色 textView.setBackgroundColor(Color.argb((int) 255, 144,151,166)); } }
OK,這就實(shí)現(xiàn)了你在最上方看到的效果了。
其實(shí)并不難,只是我們沒有親自動(dòng)手去實(shí)現(xiàn),相信多動(dòng)手自己親自去實(shí)現(xiàn)一下,UI想要的我們都可以實(shí)現(xiàn)。
源碼地址:https://github.com/Hankkin/GradationTitleBar
項(xiàng)目里面我還添加了一個(gè)帶banner的,原理是一樣的。
更多關(guān)于滑動(dòng)功能的文章,請(qǐng)點(diǎn)擊專題: 《Android滑動(dòng)功能》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中自定義標(biāo)題欄樣式的兩種方法
- 3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法
- Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
- Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果
- Android中去掉標(biāo)題欄的幾種方法(三種)
- Android 全屏無標(biāo)題欄的三種實(shí)現(xiàn)方法
- Android中隱藏標(biāo)題欄和狀態(tài)欄的方法
- Android 使用CoordinatorLayout實(shí)現(xiàn)滾動(dòng)標(biāo)題欄效果的實(shí)例
- Android中隱藏狀態(tài)欄和標(biāo)題欄的方法匯總(隱藏狀態(tài)欄、標(biāo)題欄的五種方法)
- Android實(shí)現(xiàn)可折疊式標(biāo)題欄
相關(guān)文章
Android布局加載之LayoutInflater示例詳解
這篇文章主要介紹了Android布局加載之LayoutInflater的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考借鑒價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03Android游戲開發(fā)學(xué)習(xí)之引擎用法實(shí)例詳解
這篇文章主要介紹了Android游戲開發(fā)學(xué)習(xí)之引擎用法,較為詳細(xì)的分析了Android游戲開發(fā)中所常用的JBox2D引擎功能及相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android開發(fā)簽名知識(shí)梳理總結(jié)
這篇文章主要介紹了Android開發(fā)簽名知識(shí)梳理總結(jié),Android?系統(tǒng)要求所有?APK?必須先使用證書進(jìn)行數(shù)字簽名,然后才能安裝到設(shè)備上進(jìn)行更新2022-06-06Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽、圓角圖片)
這篇文章主要為大家詳細(xì)介紹了Android關(guān)于Glide的使用,內(nèi)容豐富,高斯模糊、加載監(jiān)聽、圓角圖片希望大家可以掌握,感興趣的小伙伴們可以參考一下2016-11-11android實(shí)現(xiàn)系統(tǒng)信息推送
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)系統(tǒng)信息推送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼
這篇文章主要介紹了Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼,本文直接給出核心實(shí)現(xiàn)代碼,代碼中包含注釋,需要的朋友可以參考下2015-04-04Android 照相機(jī)的實(shí)例應(yīng)用
這篇文章主要介紹了Android 照相機(jī)的實(shí)例應(yīng)用的相關(guān)資料,希望通過此文能掌握Android照相機(jī)的使用方法,需要的朋友可以參考下2017-08-08