Android带你解析ScrollView–仿QQ空间标题栏渐变

简介:

Android带你解析ScrollView仿QQ空间标题栏渐变

绪论

今天来研究的是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想要的我们都可以实现。





本文作者:佚名
来源:51CTO
目录
相关文章
|
17天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
49 1
|
3月前
|
算法 C++ 容器
C++ STL:空间配置器源码解析
C++ STL:空间配置器源码解析
|
3月前
|
存储 算法 安全
AVB数据解析:Android verified boot 2.0 vbmeta 数据结构解析
AVB数据解析:Android verified boot 2.0 vbmeta 数据结构解析
124 0
|
1月前
|
编译器 开发工具 Android开发
Android 12 新特性深度解析
【2月更文挑战第15天】 随着移动操作系统的不断进化,Android 12带来了一系列创新功能与性能提升。本文将深入剖析Android 12的核心新特性,包括隐私仪表盘、通知管理、设备控制以及性能优化等方面,为开发者和用户提供全面的更新指南。
|
2月前
|
存储 弹性计算 固态存储
阿里云服务器租用费用1t空间多少钱?全面解析
阿里云服务器租用费用1t空间多少钱?1T空间如果是系统盘SSD云盘价格是3686元一年、ESSD云盘1t空间是5222元一年,ESSD Entry云盘1024G存储空间价格是2580元一年。阿里云百科整理几款不同的云盘1t空间价格
|
3月前
|
JSON Java Android开发
Android网络和数据交互: 请解释Android中的JSON解析库,如Gson。
Android网络和数据交互: 请解释Android中的JSON解析库,如Gson。
24 0
|
4月前
|
Android开发 容器
[Android]View的事件分发机制(源码解析)
[Android]View的事件分发机制(源码解析)
36 0
|
4月前
|
SQL 数据库 数据安全/隐私保护
Android Studio App开发中数据库SQLite的解析及实战使用(包括创建数据库,增删改查,记住密码等 附源码必看)
Android Studio App开发中数据库SQLite的解析及实战使用(包括创建数据库,增删改查,记住密码等 附源码必看)
58 0
|
21天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0

推荐镜像

更多