Android中Toolbar随着ScrollView滑动透明度渐变效果实现
作者:Luyifei666 发布时间:2023-10-08 21:06:50
Android中Toolbar随着ScrollView滑动透明度渐变效果实现
一.思路:监听ScrollView的滑动事件 不断的修改Toolbar的透明度
二.注意
1.ScrollView 6.0以前没有scrollView.setOnScrollChangeListener(l)方法 所以要自定义ScrollView 在onScrollChanged()中监听
2.ScrollView 6.0(23)以前没有scrollView.setOnScrollChangeListener()方法 所以要自定义ScrollView 实现.为了Toolbar不遮盖ScrollView我们给ScrollView设置paddingTop
但是ScrollView 设置paddintTop以后 Toolbar透明度变为0以后还占据空间 会出现空白,解决方法:
为ScrollView设置两个属性:
1〉.
android:clipToPadding="false"
表示控件的绘制范围是否不在padding里面 false就是表示空间的绘制可以绘制到padding中
2〉
android:clipChildren="false"
表示子控件是否不能超出padding区域(比如: false :ScrollView上滑的时候 child 可以滑出padding区域 ;true:ScrollView上滑的时候 child 不能可以滑出padding区域 )
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.dice.md.toolbar.transperent.TranslucentScrollView
android:id="@+id/scrollview"
android:clipToPadding="false"
android:clipChildren="true"
android:paddingTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
</LinearLayout>
</com.dice.md.toolbar.transperent.TranslucentScrollView>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_height="?attr/actionBarSize" >
</android.support.v7.widget.Toolbar>
</RelativeLayout>
三.步骤
1. 创建回调接口:
public interface TranslucentListener {
/**
* 透明度的回调
* @param alpha
*/
public void onTranslucent(float alpha);
}
2.自定义ScrollView 在onScrollChange方法中回调TranslucentListener接口的方法 并且回传alpha的值:
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (translucentListener!=null) {
//translucentListener.onTranslucent(alpha);
}
}
3.alpha的值得计算:
// alpha = 滑出去的高度/(screenHeight/3);
float heightPixels = getContext().getResources().getDisplayMetrics().heightPixels;
float scrollY = getScrollY();//该值 大于0
float alpha = 1-scrollY/(heightPixels/3);// 0~1 透明度是1~0
//这里选择的screenHeight的1/3 是alpha改变的速率 (根据你的需要你可以自己定义)
最后MainActivity中
@Override
public void onTranslucent(float alpha) {
toolbar.setAlpha(alpha);
}
以上所述是小编给大家介绍的Android中Toolbar随着ScrollView滑动透明度渐变效果实现,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
来源:http://blog.csdn.net/qq_24531461/article/details/54667026


猜你喜欢
- 前言前面一篇我们介绍了使用 shared_preferences实现简单的键值对存储,然而我们还会面临更为复杂的本地存储。比如资讯类 App
- 工厂方法模式的定义工厂方法(Factory Method)模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂
- Android开发笔记:关于SeekBar在刷新使用中的一些问题问题今天在用Navigation 在两个Fragment之间导航时发现了从第
- 简评:Android Support Library 26 中终于实现了一个等待已久的功能: RecyclerView 的快速滚动 。And
- 在windows环境下,命令行程序为cmd.exe,是一个32位的命令行程序,微软Windows系统基于Windows上的命令解释程序,类似
- 在C#中,用于存储的结构较多,如:DataTable,DataSet,List,Dictionary,Stack等
- 背景朋友想从XX超市app购买一些物美价廉的东西,但是因为人多货少经常都是缺货的状态,订阅了到货通知也没什么效果,每次收到短信通知进入app
- 本文实例为大家分享了SpringBoot使用POI进行Excel下载的具体代码,供大家参考,具体内容如下使用poi处理Excel特别方便,此
- java中不定长参数的使用方法不定长参数方法的语法如下:返回值 方法名(参数类型...参数名称)在参数列表中使用“...”形式定义不定长参数
- 本文实例为大家分享了C++实现图书馆管理系统的具体代码,供大家参考,具体内容如下总体思想用C++开发图书馆管理系统需要对学生和图书分别建立c
- 我们知道,(1)如果是整百的年份,能被400整除的,是闰年;(2)如果不是整百的年份,能被4整除的,也是闰年。每400年,有97个闰年。鉴于
- 概念final 具有“不可改变的”的含义,可以修饰 非抽象类、非抽象成员方法和变量。用 final 修饰的类不能被继承,没有子类。用 fin
- 首先我们看看为什么需要对象复制?为什么需要对象复制如上,是我们平时开发中最常见的三层MVC架构模型,编辑操作时Controller层接收到前
- 用了多年的Visual Studio,今天才发现这个编码技巧,真是惭愧,分享出来,算是抛砖引玉吧!开发环境: vs2010+C#1、代码重构
- 环境: VS2019 , OpencvSharp4 4.5.5.20211231 , .NET Framework 4.8界面设计:图像显示
- 零碎记事距离上次发博客已经有一年半了,转眼间我也是从做图像研究到了做游戏开发,说起来看看前面的博文,本来就有前兆的东西呢(笑)......因
- Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:创建一个Java类,添加一个无参无返回值
- Spring数据源的配置数据源(连接池)的作用数据源(连接池)是提高程序性能如出现的事先实例化数据源,初始化部分连接资源使用连接资源时从数据
- Maven本地仓库有对应的jar包但是报找不到问题原因第一,你本地仓库对应的包文件夹下有_remote.repositories这个文件;第
- 在Android Studio中,你可以很快速的使用Parcelable插件进行实体类的序列化的实现,使用该插件后,你的实体类可以快速的实现