成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[190235]]

緒論

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

 

 

 

好了我們切入主題。

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

  • android:scrollbars

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

  • android:scrollbarStyle

設置滾動條的風格和位置。設置值:insideOverlay、insideInset、outsideOverlay、outsideInset

  • android:scrollbarThumbHorizontal

設置水平滾動條的drawable。

  • android:soundEffectsEnabled

設置點擊或觸摸時是否有聲音效果

  • android:fadingEdge

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

  • android:scrollX

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

  • android:scrollY

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

  • android:scrollbarAlwaysDrawHorizontalTrack

設置是否始終顯示垂直滾動條

  • android:scrollbarDefaultDelayBeforeFade

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

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

ScrollView滑動監聽:

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.      */  

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

l:當前橫向滑動距離

t:當前縱向滑動距離

oldl:之前橫向滑動距離

oldt:之前縱向滑動距離

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

  1. package com.hankkin.gradationscroll; 
  2.  
  3. import android.content.Context; 
  4. import android.util.AttributeSet; 
  5. import android.widget.ScrollView; 
  6. /** 
  7.  * 帶滾動監聽的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.  

設置標題漸變

滾動監聽暴露出來我們就該去設置標題欄隨著ScrollView的滑動來改變標題欄的透明度實現漸變:

我們先看一下布局:

  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當做標題欄,你也可以用布局。 

 

 

 

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

  1. /** 
  2.      * 獲取頂部圖片高度后,設置滾動監聽 
  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.      * 滑動監聽 
  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) {   //設置標題的背景顏色 
  30.             textView.setBackgroundColor(Color.argb((int) 0, 144,151,166)); 
  31.         } else if (y > 0 && y <= height) { //滑動距離小于banner圖的高度時,設置背景和字體顏色顏色透明度漸變 
  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下面設置普通顏色 
  37.             textView.setBackgroundColor(Color.argb((int) 255, 144,151,166)); 
  38.         } 
  39.     }  

OK,這就實現了你在最上方看到的效果了。

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

責任編輯:龐桂玉 來源: Android
相關推薦

2017-02-13 17:17:48

Android標題欄控件

2011-02-22 14:53:41

titlebar標題欄Android

2011-05-04 10:40:02

網頁加載進度標題欄lephone

2015-08-14 17:47:35

Windows 10標題欄

2015-08-07 15:32:19

歡迎界面仿微信仿qq空間

2015-09-09 11:08:48

qq空間可拉伸頭部

2009-11-03 18:05:00

VB.NET窗體標題欄

2013-12-19 14:16:46

Android ApiAndroid開發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開發iPhone開發

2021-03-14 10:32:23

微軟Edge瀏覽器

2012-12-27 15:29:33

Android開發Activity

2021-06-03 05:08:19

Edge微軟瀏覽器

2023-05-17 14:59:08

Windows 11Chrome 瀏覽
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久99精品久久久久 | 欧美一区二区三区在线看 | 黄色欧美视频 | 2020国产在线 | 夜夜撸av | 国内自拍偷拍 | 久久精彩视频 | 欧美在线视频一区 | 日韩精品久久一区二区三区 | 日韩一区二区av | 亚洲视频在线观看 | 国产精品久久久亚洲 | 自拍偷拍中文字幕 | 一道本视频 | 欧美午夜一区 | 毛片区 | 色综合一区二区三区 | 在线色网 | 国产成人av在线播放 | 久久99这里只有精品 | av一级久久| 1000部精品久久久久久久久 | 亚洲 欧美 日韩 在线 | 麻豆精品一区二区三区在线观看 | 色综合久久88色综合天天 | 婷婷丁香在线视频 | 久久99精品久久久 | 亚洲美女视频 | av电影一区| 欧美成年黄网站色视频 | 国产精品我不卡 | 精品国产乱码久久久久久蜜柚 | 日韩三区在线观看 | 国产欧美日韩综合精品一区二区 | 国产精品资源在线 | 欧美性jizz18性欧美 | 亚洲欧美在线免费观看 | 久久久国产精品一区 | 一级毛片免费 | 日本一二区视频 | 91天堂网 |