偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android帶你解析ScrollView–仿QQ空間標題欄漸變

移動開發(fā) Android
今天來研究的是ScrollView-滾動視圖,滾動視圖又分橫向滾動視圖(HorizontalScrollView)和縱向滾動視圖(ScrollView),今天主要研究縱向的。本篇文章主要講監(jiān)聽ScrollView的滑動實現(xiàn)仿QQ空間標題欄漸變。

[[190235]]

緒論

今天來研究的是ScrollView-滾動視圖,滾動視圖又分橫向滾動視圖(HorizontalScrollView)和縱向滾動視圖(ScrollView),今天主要研究縱向的。相信大家在開發(fā)中經(jīng)常用到,ScrollView的功能已經(jīng)很強大了,但是仍然滿足不了我們腦洞大開的UI設(shè)計師們,所以我們要自定義…本篇文章主要講監(jiān)聽ScrollView的滑動實現(xiàn)仿QQ空間標題欄漸變,先看一下效果圖: 

 

 

 

好了我們切入主題。

有可能你不知道的那些ScrollView屬性

  • android:scrollbars

設(shè)置滾動條顯示。none(隱藏),horizontal(水平),vertical(垂直)

  • android:scrollbarStyle

設(shè)置滾動條的風(fēng)格和位置。設(shè)置值:insideOverlay、insideInset、outsideOverlay、outsideInset

  • android:scrollbarThumbHorizontal

設(shè)置水平滾動條的drawable。

  • android:soundEffectsEnabled

設(shè)置點擊或觸摸時是否有聲音效果

  • android:fadingEdge

設(shè)置拉滾動條時,邊框漸變的放向。none(邊框顏色不變),horizontal(水平方向顏色變淡),vertical(垂直方向顏色變淡)。參照fadingEdgeLength的效果圖 android:fadingEdgeLength 設(shè)置邊框漸變的長度

  • android:scrollX

以像素為單位設(shè)置水平方向滾動的的偏移值,在GridView中可看的這個效果

  • android:scrollY

以像素為單位設(shè)置垂直方向滾動的的偏移值

  • android:scrollbarAlwaysDrawHorizontalTrack

設(shè)置是否始終顯示垂直滾動條

  • android:scrollbarDefaultDelayBeforeFade

設(shè)置N毫秒后開始淡化,以毫秒為單位。

以上這些屬性有興趣的可以去研究一下,這里就不詳細講了。很多屬性并不常用,下面說說我們經(jīng)常用的,怎樣監(jiān)聽ScrollView的滑動并實現(xiàn)標題欄的漸變?

ScrollView滑動監(jiān)聽:

Google并沒有給我們提供ScrollView的滑動距離、是否滑動到布局底部、頂部的方法,但是提供了一個onScrollChanged方法:

  1. @Override 
  2.     protected void onScrollChanged(int x, int y, int oldx, int oldy) { 
  3.         super.onScrollChanged(x, y, oldx, oldy); 
  4.         //todo: 
  5.         } 
  6.     }  

通過查看源碼注釋,

  1. /** 
  2.      * This is called in response to an internal scroll in this view (i.e., the 
  3.      * view scrolled its own contents). This is typically as a result of 
  4.      * {@link #scrollBy(intint)} or {@link #scrollTo(intint)} having been 
  5.      * called. 
  6.      * 
  7.      * @param l Current horizontal scroll origin. 
  8.      * @param t Current vertical scroll origin. 
  9.      * @param oldl Previous horizontal scroll origin. 
  10.      * @param oldt Previous vertical scroll origin. 
  11.      */  

我們可以知道這個方法的參數(shù)分別為:

l:當(dāng)前橫向滑動距離

t:當(dāng)前縱向滑動距離

oldl:之前橫向滑動距離

oldt:之前縱向滑動距離

但是這個方法我們不可以調(diào)用,我們可以重寫接口或者重寫ScrollView暴露該方法:

  1. package com.hankkin.gradationscroll; 
  2.  
  3. import android.content.Context; 
  4. import android.util.AttributeSet; 
  5. import android.widget.ScrollView; 
  6. /** 
  7.  * 帶滾動監(jiān)聽的scrollview 
  8.  * 
  9.  */ 
  10. public class GradationScrollView extends ScrollView { 
  11.  
  12.     public interface ScrollViewListener { 
  13.  
  14.         void onScrollChanged(GradationScrollView scrollView, int x, int y, 
  15.                              int oldx, int oldy); 
  16.  
  17.     } 
  18.  
  19.     private ScrollViewListener scrollViewListener = null
  20.  
  21.     public GradationScrollView(Context context) { 
  22.         super(context); 
  23.     } 
  24.  
  25.     public GradationScrollView(Context context, AttributeSet attrs, 
  26.                                int defStyle) { 
  27.         super(context, attrs, defStyle); 
  28.     } 
  29.  
  30.     public GradationScrollView(Context context, AttributeSet attrs) { 
  31.         super(context, attrs); 
  32.     } 
  33.  
  34.     public void setScrollViewListener(ScrollViewListener scrollViewListener) { 
  35.         this.scrollViewListener = scrollViewListener; 
  36.     } 
  37.  
  38.     @Override 
  39.     protected void onScrollChanged(int x, int y, int oldx, int oldy) { 
  40.         super.onScrollChanged(x, y, oldx, oldy); 
  41.         if (scrollViewListener != null) { 
  42.             scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); 
  43.         } 
  44.     } 
  45.  
  46.  

設(shè)置標題漸變

滾動監(jiān)聽暴露出來我們就該去設(shè)置標題欄隨著ScrollView的滑動來改變標題欄的透明度實現(xiàn)漸變:

我們先看一下布局:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:tools="http://schemas.android.com/tools" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent" 
  6.     tools:context="com.hankkin.gradationtitlebar.QQSpeakActivity"
  7.  
  8.     <com.hankkin.gradationscroll.GradationScrollView 
  9.         android:id="@+id/scrollview" 
  10.         android:layout_width="match_parent" 
  11.         android:layout_height="match_parent" 
  12.         android:scrollbars="none"
  13.         <LinearLayout 
  14.             android:layout_width="match_parent" 
  15.             android:layout_height="wrap_content" 
  16.             android:orientation="vertical" > 
  17.             <ImageView 
  18.                 android:id="@+id/iv_banner" 
  19.                 android:scaleType="fitXY" 
  20.                 android:src="@drawable/banner3" 
  21.                 android:layout_width="match_parent" 
  22.                 android:layout_height="200dp" /> 
  23.             <com.hankkin.gradationscroll.NoScrollListview 
  24.                 android:id="@+id/listview" 
  25.                 android:layout_width="match_parent" 
  26.                 android:layout_height="wrap_content" > 
  27.             </com.hankkin.gradationscroll.NoScrollListview> 
  28.         </LinearLayout> 
  29.     </com.hankkin.gradationscroll.GradationScrollView> 
  30.     <TextView 
  31.         android:paddingBottom="10dp" 
  32.         android:id="@+id/textview" 
  33.         android:layout_width="match_parent" 
  34.         android:layout_height="55dp" 
  35.         android:gravity="center|bottom" 
  36.         android:text="我是標題" 
  37.         android:textSize="18sp" 
  38.         android:textColor="@color/transparent" 
  39.         android:background="#00000000" /> 
  40. </RelativeLayout>  

最外層是我們自定義的ScrollView,包裹著一張背景圖片和一個ListView(ListView重寫為不可以滑動),然后布局的上面有一個TextView當(dāng)做標題欄,你也可以用布局。 

 

 

 

然后我們需要獲取圖片的高度,并且設(shè)置滾動監(jiān)聽,隨著滾動的距離來設(shè)置標題欄的顏色透明度和字體顏色的透明度 

  1. /** 
  2.      * 獲取頂部圖片高度后,設(shè)置滾動監(jiān)聽 
  3.      */ 
  4.     private void initListeners() { 
  5.  
  6.         ViewTreeObserver vto = ivBanner.getViewTreeObserver(); 
  7.         vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
  8.             @Override 
  9.             public void onGlobalLayout() { 
  10.                 textView.getViewTreeObserver().removeGlobalOnLayoutListener( 
  11.                         this); 
  12.                 height = ivBanner.getHeight(); 
  13.  
  14.                 scrollView.setScrollViewListener(QQSpeakActivity.this); 
  15.             } 
  16.         }); 
  17.     } /** 
  18.      * 滑動監(jiān)聽 
  19.      * @param scrollView 
  20.      * @param x 
  21.      * @param y 
  22.      * @param oldx 
  23.      * @param oldy 
  24.      */ 
  25.     @Override 
  26.     public void onScrollChanged(GradationScrollView scrollView, int x, int y, 
  27.                                 int oldx, int oldy) { 
  28.         // TODO Auto-generated method stub 
  29.         if (y <= 0) {   //設(shè)置標題的背景顏色 
  30.             textView.setBackgroundColor(Color.argb((int) 0, 144,151,166)); 
  31.         } else if (y > 0 && y <= height) { //滑動距離小于banner圖的高度時,設(shè)置背景和字體顏色顏色透明度漸變 
  32.             float scale = (float) y / height; 
  33.             float alpha = (255 * scale); 
  34.             textView.setTextColor(Color.argb((int) alpha, 255,255,255)); 
  35.             textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166)); 
  36.         } else {    //滑動到banner下面設(shè)置普通顏色 
  37.             textView.setBackgroundColor(Color.argb((int) 255, 144,151,166)); 
  38.         } 
  39.     }  

OK,這就實現(xiàn)了你在最上方看到的效果了。

其實并不難,只是我們沒有親自動手去實現(xiàn),相信多動手自己親自去實現(xiàn)一下,UI想要的我們都可以實現(xiàn)。 

責(zé)任編輯:龐桂玉 來源: Android
相關(guān)推薦

2017-02-13 17:17:48

Android標題欄控件

2011-02-22 14:53:41

titlebar標題欄Android

2011-05-04 10:40:02

網(wǎng)頁加載進度標題欄lephone

2015-08-14 17:47:35

Windows 10標題欄

2015-09-09 11:08:48

qq空間可拉伸頭部

2015-08-07 15:32:19

歡迎界面仿微信仿qq空間

2009-11-03 18:05:00

VB.NET窗體標題欄

2013-12-19 14:16:46

Android ApiAndroid開發(fā)Android SDK

2023-06-26 07:21:41

標題欄鼠標標題

2022-02-13 19:05:19

微軟Windows 11

2021-09-01 13:53:19

WindowsAcrylic標題欄

2021-04-23 15:20:54

微軟瀏覽器Windows

2015-09-07 10:57:38

qq未讀消息

2015-03-31 18:13:09

swipelistvi

2022-02-21 08:31:58

Windows 11微軟云母視覺

2010-08-05 14:01:19

評測Android開發(fā)iPhone開發(fā)

2021-03-14 10:32:23

微軟Edge瀏覽器

2021-06-03 05:08:19

Edge微軟瀏覽器

2012-12-27 15:29:33

Android開發(fā)Activity

2023-05-17 10:14:37

谷歌Chrome瀏覽器
點贊
收藏

51CTO技術(shù)棧公眾號