绪论

今天来研究的是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方法:

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

我们可以知道这个方法的参数分别为:
l:当前横向滑动距离
t:当前纵向滑动距离
oldl:之前横向滑动距离
oldt:之前纵向滑动距离

但是这个方法我们不可以调用,我们可以重写接口或者重写ScrollView暴露该方法:

package com.hankkin.gradationscroll;import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/*** 带滚动监听的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;}@Overrideprotected 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);}}}

设置标题渐变

滚动监听暴露出来我们就该去设置标题栏随着ScrollView的滑动来改变标题栏的透明度实现渐变:
我们先看一下布局:

<?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="我是标题"android:textSize="18sp"android:textColor="@color/transparent"android:background="#00000000" />
</RelativeLayout>

最外层是我们自定义的ScrollView,包裹着一张背景图片和一个ListView(ListView重写为不可以滑动),然后布局的上面有一个TextView当做标题栏,你也可以用布局。

然后我们需要获取图片的高度,并且设置滚动监听,随着滚动的距离来设置标题栏的颜色透明度和字体颜色的透明度

/*** 获取顶部图片高度后,设置滚动监听*/private void initListeners() {ViewTreeObserver vto = ivBanner.getViewTreeObserver();vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);height = ivBanner.getHeight();scrollView.setScrollViewListener(QQSpeakActivity.this);}});}
 /*** 滑动监听* @param scrollView* @param x* @param y* @param oldx* @param oldy*/@Overridepublic void onScrollChanged(GradationScrollView scrollView, int x, int y,int oldx, int oldy) {// TODO Auto-generated method stubif (y <= 0) {   //设置标题的背景颜色textView.setBackgroundColor(Color.argb((int) 0, 144,151,166));} else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变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 {    //滑动到banner下面设置普通颜色textView.setBackgroundColor(Color.argb((int) 255, 144,151,166));}}

OK,这就实现了你在最上方看到的效果了。
其实并不难,只是我们没有亲自动手去实现,相信多动手自己亲自去实现一下,UI想要的我们都可以实现。
源码地址:欢迎Star,fork,有问题多多指正。
https://github.com/Hankkin/GradationTitleBar
项目里面我还添加了一个带banner的,原理是一样的。

Android带你解析ScrollView--仿QQ空间标题栏渐变相关推荐

  1. Android源码解析--SwipeMenuListView仿QQ聊天左滑

    版权声明:本文为博主原创文章,转载请标明出处. https://blog.csdn.net/lyhhj/article/details/50612714 绪论: 好久没写博客了,最近比较懒,不想写博客 ...

  2. 仿QQ空间标题栏显示隐藏

    在QQ空间中我们经常会看到一种效果:标题栏或者状态栏在下拉或者上拉时的"渐变显示隐藏功能" ,今天我们就看看他是怎么实现的. 先看下效果图: 一:那么我们就先来说下"带B ...

  3. Android图文混排(仿QQ空间评论)

    你知道TextView中可以"塞入"图片么? 之前项目需求,做一个类似QQ的点赞评论功能,效果图如下: 本文主要讲解评论这一块,至于点赞和表情帖,后续再说. 评论功能的介绍:评论内 ...

  4. android写qq动态界面,Android_Android仿QQ空间主页面的实现,今天模仿安卓QQ空间,效果如 - phpStudy...

    Android仿QQ空间主页面的实现 今天模仿安卓QQ空间,效果如下: 打开程序的启动画面和导航页面我就不做了,大家可以模仿微信的那个做一下,很简单.这次主要做一下主页面的实现,下面是主页面的布局: ...

  5. 【Android UI设计与开发】第09期:底部菜单栏(四)Fragment+PopupWindow仿QQ空间最新版底部菜单栏

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9023451          在今天的这篇文章当中,我依然会以实战加理论结合 ...

  6. android弹窗使用总结,高仿QQ空间操作弹窗

    android弹窗一共有两种方式,一种是dialog及其子类,另一种是popupwindow:Dialog及其子类尤其AlertDialog是最常用的,也是最自由的一种. **Popupwindow与 ...

  7. 高仿QQ空间项目实战开发(带服务器端程序)

    大家好,今天我在这里分享一个小程序.高仿QQ空间的APP,这里给大家分享一个安卓APP和PHP写的服务器程序.想提高安卓开发能力或安卓和后台服务器一起做的初学者值得一看,老鸟跳过. 接下来我们看看效果 ...

  8. Android仿QQ空间底栏

    继上一篇仿新浪微博底栏,我们在写个仿QQ空间底栏的效果. 先看主布局文件: <RelativeLayout xmlns:android="http://schemas.android. ...

  9. Android仿QQ空间底部菜单

    之前曾经在网上看到Android仿QQ空间底部菜单的Demo,发现这个Demo有很多Bug,布局用了很多神秘数字.于是研究了一下QQ空间底部菜单的实现,自己写了一个,供大家参考.效果如下图所示:  点 ...

最新文章

  1. [洛谷P4722]【模板】最大流 加强版 / 预流推进
  2. android控件跟随手势滑动改变位置
  3. 图形处理(十三)基于可变形模板的三维人脸重建-学习笔记
  4. boost::mpl::string相关的测试程序
  5. P3085 [USACO13OPEN]Yin and Yang G 点分治
  6. 在Spring Boot中使用切面统一处理自定义的异常
  7. git日常提交使用的命令行
  8. 程序员的算法课(9)-常见字符串算法
  9. python 100题_python 100题
  10. 【BZOJ-1324】Exca王者之剑 最小割
  11. 新创建虚拟机如何配置ip地址
  12. AI——六(图层、蒙版)
  13. 隐函数求导(一元和二元)
  14. CSDN 第六期编程竞赛做题记录
  15. linux 之 Deamon进程
  16. The server encountered an internal error () that prevented it from fulfilling this requet异常解决
  17. missingno错误不显示图片,pycharm
  18. Anthony计量经济学导论-学习笔记+R语言
  19. 常用正则表达式—身份证号(JavaScript,Regex)
  20. 第二章:真分数理论(信度系数的估计)(三)

热门文章

  1. matlab 绘制星形球面,一种星形套球面相对内孔的跳动检具
  2. Mac怎么设置wifi热点
  3. 灰狼优化(GWO)算法(含MATLAB代码)
  4. Java利用jxl.jar操作Excel文件的方法——把两列相同的数据扫描输出到新的Excel文件中...
  5. [讨论] ROS1 为何不是可靠的系统
  6. Python处理字节(bytes bytearray)
  7. 添加机构organizations模块
  8. 计算5的阶乘,利用c++代码实现
  9. C语言用递归求1到5的阶乘
  10. 曾国藩:一生所悟,句句珠玑,与君共勉