QQ控件顶部的工具条在整体的ScrollView从顶部滑动时会从透明逐渐变为solid纯色;虽然有人实现过 ActionBar随ScorllView上下拖动而透明度渐变效果 以及 仿QQ空间滚动ActionBar透明度变化Demo ,但是两者存在以下问题:

1、前者的适用范围是ActionBar+ScrollView,但是实际中顶部的布局可能是任何view类型,即适用性不够宽;

2、后者的实现是通过重写ScrollView的onTouchEvent来实现的,但是这样有弊端:因为是监听的触摸事件,所以在我们进行“用力滑一下靠惯性就让ScrollView自己滚动到底部或顶部”的操作时(其实就是FLING啦),触摸事件是监听不到的,就会出现透明度显示错误的bug。

综上,我实现的方式是:

重写ScrollView的onScrollChanged方法,自定义一个公共的OnScrollChangedListener接口(因为Activity里面是无法直接使用OnScrollChangedListener的),并暴露一个setOnScrollChangedListener用来让Activity设置并监听滚动事件,在接口实现当中就可以放入我们的透明度渐变效果啦。

透明度渐变其实很简单,使用Drawable的setAlpha方法,之后再对任何类型的顶部布局(view或viewgroup都行)调用setBackgroundDrawable方法就好了。

效果图:                                 

关键代码:

package com.example.alphatop;import com.example.alphatop.myScrollView.OnScrollChangedListener;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;public class MainActivity extends Activity {private LinearLayout titleLayout; //顶部渐变布局private myScrollView scrollView;  //整体ScrollViewprivate static final int START_ALPHA = 0;private static final int END_ALPHA = 255;private int fadingHeight = 600;   //当ScrollView滑动到什么位置时渐变消失(根据需要进行调整)private Drawable drawable;        //顶部渐变布局需设置的Drawable@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);titleLayout = (LinearLayout) findViewById(R.id.linear);scrollView = (myScrollView) findViewById(R.id.scrollview);drawable = getResources().getDrawable(R.drawable.color_exam_grey);drawable.setAlpha(START_ALPHA);titleLayout.setBackgroundDrawable(drawable);scrollView.setOnScrollChangedListener(scrollChangedListener);}/*** 公共接口:ScrollView的滚动监听*/private OnScrollChangedListener scrollChangedListener = new OnScrollChangedListener() {@Overridepublic void onScrollChanged(ScrollView who, int x, int y, int oldx, int oldy) {if (y > fadingHeight) {y = fadingHeight;   //当滑动到指定位置之后设置颜色为纯色,之前的话要渐变---实现下面的公式即可}drawable.setAlpha(y * (END_ALPHA - START_ALPHA) / fadingHeight + START_ALPHA);}};
}
package com.example.alphatop;import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;public class myScrollView extends ScrollView {public myScrollView(Context context) {  super(context);  }  public myScrollView(Context context, AttributeSet attrs) {  super(context, attrs);  }  public myScrollView(Context context, AttributeSet attrs, int defStyle) {  super(context, attrs, defStyle);  }  /*** 公共接口:ScrollView滚动监听*/public interface OnScrollChangedListener {  void onScrollChanged(ScrollView who, int x, int y, int oldx, int oldy);  }  private OnScrollChangedListener mOnScrollChangedListener;  @Override  protected void onScrollChanged(int x, int y, int oldx, int oldy) {  super.onScrollChanged(x, y, oldx, oldy);  if (mOnScrollChangedListener != null) {  //使用公共接口触发滚动信息的onScrollChanged方法,将滚动位置信息暴露给外部mOnScrollChangedListener.onScrollChanged(this, x, y, oldx, oldy);  }  }  /*** 暴露给外部的方法:设置滚动监听* @param listener*/public void setOnScrollChangedListener(OnScrollChangedListener listener) {  mOnScrollChangedListener = listener;  }
}
<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="${relativePackage}.${activityClass}" ><com.example.alphatop.myScrollView android:id="@+id/scrollview"android:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@color/color_pink"android:contentDescription="@null" /><ImageViewandroid:layout_width="match_parent"android:layout_height="1000dp"android:background="@color/color_blue"android:contentDescription="@null" /></LinearLayout></com.example.alphatop.myScrollView><LinearLayoutandroid:id="@+id/linear"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:gravity="center_horizontal"android:text="title bar" /><ImageView android:layout_width="match_parent"android:layout_height="2dp"android:src="@color/color_grey"android:contentDescription="@null"/></LinearLayout></RelativeLayout>

demo下载地址

顶部布局随ScrollView滑动透明度渐变(QQ空间效果)相关推荐

  1. Android透明到白色滑动渐变,Android中Toolbar随着ScrollView滑动透明度渐变效果实现...

    Android中Toolbar随着ScrollView滑动透明度渐变效果实现 一.思路:监听ScrollView的滑动事件 不断的修改Toolbar的透明度 二.注意 1.ScrollView 6.0 ...

  2. Android开发之QQ空间效果(QQ空间下拉图片放大,松手后回弹)

    Android开发之QQ空间效果(QQ空间下拉图片放大,松手后回弹) 腾讯QQ空间的下拉图片放大,松手后回弹的效果带来的视觉差异效果让许多移动开发者心动不已,经本人一段时间的研究,终于实现了该视差效果 ...

  3. android qq空间效果,更新UI设计 Android手机QQ空间1.2详测

    近日,腾讯发布了Android手机QQ空间1.2版.经过一周的试用,小编认为该版QQ空间的多项创新,大大地发挥了Android平台的价值. 从测试结果来看,Android手机QQ空间1.2版在原有便捷 ...

  4. android 滑动模糊渐变,Android UI效果实现 滑动模糊渐变效果实现

    大家应该都看到过iOS7解锁屏幕的滑动模糊渐变效果,好了,现在可以把手纸收起来了,今天黄老师就给大家讲一下如何在Android平台上 实现类似的滑动模糊渐变效果,其实方式远比你想像的简单. 目标效果展 ...

  5. RecyclerView实现QQ空间和微信朋友圈头部刷新效果

    RecyclerView实现QQ空间和微信朋友圈头部刷新效果 老规矩先上图 本篇主要讲RecyclerView实现QQ空间和微信朋友圈头部刷新效果,如果想了解ListView如何实现,请查看上篇:Li ...

  6. 【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )

    文章目录 前言 一.移除顶部状态栏空白 二.帧布局组件 三.透明度组件 四.监听滚动事件 五.完整代码示例 六.相关资源 前言 在上一篇博客 [Flutter]Banner 轮播组件 ( flutte ...

  7. 滑动listview隐藏和显示顶部布局

    需求: 1.listview向下滑动时,隐藏顶部布局 2.listview向上滑动到最上面,显示顶部布局 3.顶部布局的隐藏和显示有过渡效果 4.第一次加载listview时,顶部不要隐藏 布局: 注 ...

  8. 滑动到底部或顶部响应的ScrollView实现

    转:http://www.trinea.cn/android/滑动到底部或顶部响应的scrollview/ 两种实现方式的主要不同点在于判断滑动位置的地方,第一种方式在onScrollChanged函 ...

  9. Android实现ScrollView顶部布局上滑缩小,下滑恢复

    废话不多说,先上效果图: 代码实现也比较简易,我写了一个类继承了ScrollView,并重写onTouchEvent以支持头部布局的变化. 当然,这么做需要手动关联一下 头布局.图片. 上核心代码: ...

最新文章

  1. matlab方波假频现象分析,基于MATLAB 的信号时域采样及频率混叠现象分析
  2. Logstash(二)input、codec插件详解
  3. 企业IM-1功能需求
  4. 正则表达式截取URL参数值
  5. Exchange 2013如何自建管理员角色
  6. div css入门教程,更简洁CSS清理浮动方式:clearfix
  7. webpack4导入全局sass文件
  8. Python 2 寿命即将终结,​是时候将 3500 万行代码迁移到 Python 3 了!
  9. clipboardjs 基本使用方式之一
  10. bat运行exe 自动确认_无powershell运行powershell方法总结
  11. pythoncss50使用教程_Python学习(二十) —— 前端之CSS
  12. python获取文件列表失败_python – Pytesseract没有这样的文件或目录错误
  13. 通讯录管理系统的设计与实现
  14. Mac 电脑锁屏快捷方法
  15. android模拟anr,Android ANR
  16. 京东单号中转延误如何查询出的方法
  17. android开发学习:打电话和发短信
  18. 使用 CNN 进行面部表情检测
  19. JAVA出现警告无法读取 AppletViewer 属性文件的解决方法
  20. android学习笔记之——获取SD卡及手机ROM容量

热门文章

  1. 学习笔记|强化学习(Reinforcement Learning, RL)——让AlphaGo进化得比人类更强
  2. windows无法停止 服务 错误1053 服务没有及时响应
  3. 【毕业设计源码】基于uniapp、vue、node的校园闲置二手商城小程序(包远程运行成功)
  4. java非侵入式接口实现_No-intrusive, 非侵入式接口设计
  5. ofo北京总部人去楼空,是资本寒冬还是冬眠?
  6. Makefile经典教程(最牛X的教程)
  7. 第 12 章 基于块匹配的全景图像拼接--Matlab深度学习实战图像处理应用
  8. J2EE基础集合框架之Set
  9. linx版本微信安装方式
  10. 大白话解释TCP三次握手