Android App页面滑动标题栏颜色渐变详解
作者:任缥缈 发布时间:2023-10-14 20:08:17
通常,我们会被要求实现类似支付宝首页的特效:随着界面的滑动,标题栏的背景透明度渐变。
在实际开发中,常见的滑动有列表RecyclerView(ListView)滑动,NestedScrollView(ScrollView)嵌套滑动等等。
本文主要从上述两方面来探讨滑动效果。
一、RecyclerView滑动标题栏渐变
废话不多说,直接撸代码:
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
tools:context=".scroll_toolbar.ScrollToolBarActivity">
<!-- title标题栏-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<ImageView
android:id="@+id/ivBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/qb_px_20"
android:gravity="center_vertical"
android:src="@drawable/theme_toolbar_btn_back_fg_normal0"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:textSize="16sp"
android:padding="@dimen/qb_px_20"
android:text="RecyclerView控制titleBar渐变"/>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvZhangjie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="@dimen/qb_px_50"
android:layout_marginRight="@dimen/qb_px_50"
android:layout_marginTop="@dimen/qb_px_20"
android:background="@color/back_ground"/>
</LinearLayout>
Java代码如下:
private void toolBarColor(){
Toolbar toolbar = findViewById(R.id.toolbar);
ImageView ivBack = findViewById(R.id.ivBack);
TextView tvName = findViewById(R.id.tvName);
RecyclerView rvZhangjie = findViewById(R.id.rvZhangjie);
List<String> stringList = dealData();
ScrollAdapter scrollAdapter = new ScrollAdapter(this, stringList);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
rvZhangjie.setLayoutManager(manager);
rvZhangjie.setAdapter(scrollAdapter);
rvZhangjie.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//toolbar的高度
toolbarHeight = toolbar.getBottom();
//滑动的距离
mDistanceY += dy;
//当滑动的距离 <= toolbar高度的时候,改变Toolbar背景色的透明度,达到渐变的效果
if (mDistanceY <= toolbarHeight) {
float scale = (float) mDistanceY / toolbarHeight;
float alpha = scale * 255;
toolbar.setBackgroundColor(Color.argb((int) alpha, 255, 0, 0));
} else {
//上述虽然判断了滑动距离与toolbar高度相等的情况,但是实际测试时发现,标题栏的背景色
//很少能达到完全不透明的情况,所以这里又判断了滑动距离大于toolbar高度的情况,
//将标题栏的颜色设置为完全不透明状态
toolbar.setBackgroundResource(R.color.colorPrimary);
}
}
});
}
上面代码中的 dealData()方法很简单就是想一个String型List里面添加数据,没什么难度。
关键点在于给rvZhangjie.addOnScrollListener()也就是给RecyclerView设置滑动监听,并复写onScrolled()方法。该方法里面3个参数:
第一个RecyclerView recyclerView,这个很明显就是目标RecyclerView;
第二个int dx,表示RecyclerView在水平X方向的相对滑动量;
第三个int dy,表示RecyclerView在垂直Y方向的相对滑动量;
我们可以通过累加计算RecyclerView滑动的距离相对于指定距离的百分比,来计算透明度的变化量:
mDistanceY += dy;
float scale = (float) mDistanceY / toolbarHeight;
float alpha = scale * 255;
最后再将alpha透明度值设置给ToolBar:
toolbar.setBackgroundColor(Color.argb((int) alpha, 255, 0, 0));
二、NestedScrollView滑动标题栏渐变
其实NestedScrollView滑动渐变和RecyclerView的滑动渐变原理是一样的,本质上都是监听View滑动的距离,通过距离换算成透明度值。只不过二者的滑动偏移量稍有点不同。
代码细节我就不贴出来了,就说说关键的对NestedScrollView的监听和偏移量的处理:
nsvScroolBack.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//scrollY > oldScrollY:向上滑动
//scrollY < oldScrollY:向下滑动
// scrollY:滚动过的距离。
toolbarHeight = toolbar.getBottom() * 1.5f;
if (scrollY <= toolbarHeight){
float scale = (float)scrollY / toolbarHeight;
float alpha =scale * 255;
toolbar.setBackgroundColor(Color.argb((int) alpha, 255, 0, 0));
}else {
toolbar.setBackgroundColor(Color.BLUE);
}
}
});
通过上面的代码,很容易发现NestedScrollView滑动渐变和RecyclerView的滑动渐变就一回事。代码实现上差别很细微。不同的是RecyclerView的滑动渐变哪里,我们要通过对dy的累加来获得RecyclerView在垂直方向的滑动偏移量。而在NestedScrollView的滑动渐变里面,NestedScrollView在x或者y方向的滑动偏移量,系统已经帮我们计算出来了:scrollX或者scrollY。然后进行透明度的计算即可。
来源:https://blog.csdn.net/haoyuegongzi/article/details/85053509


猜你喜欢
- 承蒙各位厚爱,我们一起每天进步一点点!(鼠标选中空白处查看答案)1、现有如下代码段:x = 2;while(x<n/2){x = 2*
- —学习并使用mybatis-plus的一些高级功能的用法例如: AR模式、 乐观锁 、逻辑删除 、自动填充、数据保护等功能为了方便演示,咱们
- 第1部分 TreeSet介绍TreeSet简介TreeSet 是一个有序的集合,它的作用是提供有序的Set集合。它继承于AbstractSe
- 1. Maven简介相对于传统的项目,Maven 下管理和构建的项目真的非常好用和简单,所以这里也强调下,尽量使用此类工具进行项目构建, 它
- 目录1.项目gitthub地址链接: https://github.com/baisul/generateCode.git切换到master
- 汉诺塔的规则是:一共三根柱子,一根柱子从上到
- 多态是同一个行为具有多个不同表现形式或形态的能力。多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功
- spring @Autowired注解无法注入问题简述在使用spring框架的过程中,常会遇到这种两情况:1、在扫描的包以外使用需要使用ma
- 本文实例讲述了Android实现捕获TextView超链接的方法。分享给大家供大家参考,具体如下:这里分享一篇捕获TextView超链接的文
- 幂等概述幂等性原本是数学上的概念,即使公式:f(x)=f(f(x)) 能够成立的数学性质。用在编程领域,则意为对同一个系统,使用同样的条件,
- 查询文档 & 基本操作为了方便学习, 本节中所有示例沿用上节的索引按照ID单个GET class_1/_doc/1查询结果:{ &n
- C++ 中二分查找递归非递归实现并分析二分查找在有序数列的查找过程中算法复杂度低,并且效率很高。因此较为受我们追捧。其实二分查找算法,是一个
- 本文实例为大家分享了android实现倒计时功能的具体代码,供大家参考,具体内容如下【思路】:通过 timer 执行周期延时的任务,hand
- 懒加载 ,也称为嵌套查询 需要查询关联信息时,使用 Mybatis 懒加载特性可有效的减
- 本文实例讲述了Android编程实现自定义ProgressBar样式。分享给大家供大家参考,具体如下:效果图如下,本例中设置了第一级进度条和
- 最近在做一个需求是从数据库里面取出图片,但是图片都有一个白色的背景,于是项目组希望可以将图片的白色的背景去掉。本文为大家分享了java去除图
- C# 获取某个时间的0点0分和23点59分59秒,具体代码如下所示:C#获取当月第一天和最后一天当月第一天0时0分0秒:DateTime.N
- 根据poi接收controller层的excel文件导入 可使用后缀
- @Autowired和static的关系一、发生的场景好几次有个同事因为把static用到Spring的@Autowired上,导致注入的对
- 开篇Druid号称是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。作为日常使用较多的数据库连接组件,纯粹个人兴趣研究下