效果图

滑动前:

滑动中:

滑动到底部:

项目结构


ObservableScrollView
package com.jukopro.titlebarcolor;import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/*** 自定义带滚动监听的scrollview**/
public class ObservableScrollView extends ScrollView {/*** 接口回调*/private ScrollViewListener scrollViewListener = null;public interface ScrollViewListener {void onScrollChanged(ObservableScrollView scrollView, int x, int y,int oldx, int oldy);}public void setScrollViewListener(ScrollViewListener scrollViewListener) {this.scrollViewListener = scrollViewListener;}/*** 重写的onScrollChanged方法监听坐标* 这个监听在源码中没有写成回调的样子,* 只是写成了方法的样子,由于修饰这个方法的修饰符是protected,*(protected只能在本类,子类,同一包中调用),* 所以拿到ScrollView对象后在无法activity中调用此方法,* 所以只能重写后,子类中自动调用,* 所以要想在activity调用,* 就要写回调,* 上面就是我写的回调* 在Android源码中这种写法很多,在很多控件中都有*/@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);}}/***构造方法(在这里没用)*/public ObservableScrollView(Context context) {super(context);}public ObservableScrollView(Context context, AttributeSet attrs,int defStyle) {super(context, attrs, defStyle);}public ObservableScrollView(Context context, AttributeSet attrs) {super(context, attrs);}}
activity_main.xml
<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.jukopro.titlebarcolor.ObservableScrollViewandroid:id="@+id/scrollview"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:id="@+id/imageview"android:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/zuqiu" /><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="我是翟浩浩"android:gravity="center"/></LinearLayout></com.jukopro.titlebarcolor.ObservableScrollView><TextViewandroid:id="@+id/textview"android:layout_width="match_parent"android:layout_height="48dp"android:gravity="center"android:text="我是标题"android:textSize="18sp"android:textColor="#ffffff"android:background="#00000000" /></RelativeLayout>

MainActivity
package com.jukopro.titlebarcolor;import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;import com.jukopro.titlebarcolor.ObservableScrollView.ScrollViewListener;public class MainActivity extends Activity {private ObservableScrollView scrollView;private ImageView imageView;private TextView textView;private int imageHeight;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);scrollView = (ObservableScrollView) findViewById(R.id.scrollview);imageView = (ImageView) findViewById(R.id.imageview);textView = (TextView) findViewById(R.id.textview);initListeners();}private void initListeners() {// 获取顶部图片高度后,设置滚动监听ViewTreeObserver vto = imageView.getViewTreeObserver();vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);imageHeight = imageView.getHeight();scrollView.setScrollViewListener(new ScrollViewListener() {@Overridepublic void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {// TODO Auto-generated method stub// Log.i("TAG", "y--->" + y + "    height-->" + height);if (y <= 0) {
//                          设置文字背景颜色,白色textView.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));//AGB由相关工具获得,或者美工提供
//                          设置文字颜色,黑色textView.setTextColor(Color.argb((int) 0, 255, 255, 255));Log.e("111","y <= 0");} else if (y > 0 && y <= imageHeight) {float scale = (float) y / imageHeight;float alpha = (255 * scale);// 只是layout背景透明(仿知乎滑动效果)白色透明textView.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));//                          设置文字颜色,黑色,加透明度textView.setTextColor(Color.argb((int) alpha, 0, 0, 0));Log.e("111","y > 0 && y <= imageHeight");} else {
//                          白色不透明textView.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));//                          设置文字颜色//黑色textView.setTextColor(Color.argb((int) 255, 0, 0, 0));Log.e("111","else");}}});}});}}

源码下载:http://download.csdn.net/detail/zhaihaohao1/9786526

Android中自定义ScrollView的滑动监听事件,并在滑动时渐变标题栏背景颜色相关推荐

  1. Android中Preference的使用以及监听事件分析

                                                                                                        ...

  2. ROS中 Python/C++ 键盘按键监听事件

    ROS中 Python/C++ 键盘按键监听事件 这几天在肝全国智能驾驶大赛,真就挺累的:抓狂思考,手搓代码,疯狂编译,要命运行.在这趟火车上,被各种各样的问题卡住甚至卡死,就很难受好吧.至今,我深深 ...

  3. [Android]Fragment自定义动画、动画监听以及兼容性包使用

    Fragment是Android在API 11之后加入的一个组件,对提高Android开发中的布局合理性和布局效率都有很大作用,尤其是在Android平板等大屏幕设备的开发中,Fragment的引入能 ...

  4. Android的Preference的使用和监听事件

    学习了Preference的用法,这里记录下来供大家参考. 继承结构图 1.基本组成的使用 1.1组织方式(组合控件) PreferenceScreen:最平白和基础的方式 PreferenceCat ...

  5. vue中select选择框的监听事件

    template模板: <el-select v-model="valueClass" placeholder="请选择学生" class="s ...

  6. Android开发之解决NestedScrollView滑动监听兼容低版本的方法

    NestedScrollView的滑动监听目前仅限api23及以上,为了兼容低版本如下自定义方法 可以自定义NestedScrollView即可如下: package cn.net.gfan.worl ...

  7. Android RecyclerView(九)滑动监听综述

    Android RecyclerView(九)滑动监听 1 RecyclerView 的滑动监听 1.1 RecyclerView 设置滑动监听 mRecyclerView.setOnScrollLi ...

  8. Android实现来电和去电的监听

    写个实例实现Android中来电和去电的监听,来电可以使用PhoneStateListener对电话状态的改变进行监听,去电需要动态或者静态去注册广播接收器,对去电进行监听: 来电: 来电所对应的三种 ...

  9. Android中手势滑动监听简单实现

    文章目录 背景 实现 手势监听器的声明和创建 对重写的相关方法 onScroll() 说明 手势监听器接管View的触屏事件 验证结果 背景 项目中要实现控件的滑动监听其实还是挺常见的,这里就简单的做 ...

最新文章

  1. 使用Java合并图片、修改DPI
  2. iOS自动布局(AutoLayout)之 NSLayoutAnchor
  3. Java的最大优势还是跨平台么?
  4. 什么是基金净值、单位净值、累计净值
  5. matlab the installer cannot read,MATLAB安装 The installer cannot read the mwinstall.dll… | 学步园...
  6. 一个空间绑定多个域名实现自动跳转的几种方法!
  7. lua的table库中的常用函数总结
  8. tween.js 中文使用指南
  9. web项目01-----项目需求分析,需求文档
  10. 2小时部署实时反欺诈深度学习模型 —— IBM主机机器学习平台社区版简介
  11. 谈一谈企业部署erp系统的三大时间段
  12. 全球与中国量身定制生产线市场深度研究分析报告
  13. 【详细】【转】CentOS 7部署ASP.NET Core应用程序
  14. python到底有多少个库_11个你可能不知道的Python库
  15. 为什么业务中很少用到设计模式
  16. 【数字信号】基于matlab GUI DTMF双音多频信号仿真演示系统(戈泽尔算法)【含Matlab源码 016期】
  17. CV-2022:CodeFormer【最强的AI 视频、图片修复】【南洋理工大学 S-Lab】
  18. 点线联合优化估计相机姿态(IROS 2022)
  19. 【控制control】机械臂运动学、动力学模型
  20. Input elements should have autocomplete attributes (suggested: new-password)

热门文章

  1. Linux 运行级别
  2. 被业界追捧的3D传感技术 奥比中光比拼苹果结果出人意料
  3. 为什么工厂管理那么难?
  4. Linux多线程网络编程要义丨epoll与reactor原理
  5. 用cocos2dx做一个简单的单机捕鱼达人游戏(1)
  6. SketchUp pro(草图大师) 2017中文版
  7. RocketMQ自动创建topic原理-TBW102
  8. DDR5 SDRAM简要前瞻以及DRAM DIMM和DCPMM配置指南
  9. python appium自动化测试框架unittest_appium+python+unittest自动化测试
  10. PDF页码顺序如何调整