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
猜你喜欢
- 前言: 有时候我们需要实现长按选择文字功能,比如阅读器一般都有这个功能,有时候某个自定义控件上可能就有这种需求,如何实现呢?正好最近还算闲,
- 在intellij中忽略提交文件,分两种情况,文件没有纳入版本管理第一种方法文件还没有纳入版本管理,这种通过 svn的ignore配置ver
- 打注解@SpringBootTest的时候不会出现提示但是又导入了 <dependency> &nb
- 1. 通过设置采样率压缩res资源图片压缩 decodeResource public Bitmap decodeSampled
- 一、概述1、事务ACID特性事务将一系列的工作视为一个工作单元,它具有 ACID 特性:A:Atomicity 不可分性 也就是说
- 这几天用winform做了一个设置壁纸的小工具, 为图片添加当月的日历并设为壁纸,可以手动设置壁纸,也可以定时设置壁纸,最主要的特点是在图片
- 一、File流概念 JAVA中针对文件的读写操作设置了一系列的流,其
- 1. 出故障了没办法,干it这一行,就得天天面对故障,大家就是传说中的消防员,到处救火。不过,这次的故障范围有点大,宿主机都打不开了。好在监
- 目录一、内部类1.什么是内部类?1.类作为形参和返回值:2.内部类的几种形式二、Lambda表达式(强调做什么,而不是以什么形式去做)1.L
- 本文实例为大家分享了android实现动态显示隐藏进度条的具体代码,供大家参考,具体内容如下调用ProgressUtil.startProg
- 本文介绍了Spring @Async异步线程池用法总结,分享给大家,希望对大家有帮助1. TaskExecutorspring异步线程池的接
- C#调用c++dll文件是一件很麻烦的事情,首先面临的是数据类型转换的问题,相信经常做c#开发的都和我一样把学校的那点c++底子都忘光了吧(
- 知识点回顾封装封装(有时称为数据隐藏)是与对象有关的一个重要概念。从形式上来看,封装不过是将数据和行为组合在一个包中,并对对象的使用者隐藏了
- 本文实例讲述了dotNet中的反射用法。分享给大家供大家参考,具体如下:参考MSDN:ms-help://MS.VSCC.2003/MS.M
- 新建一个类MyPageInterceptor.java(注意在springboot中要添加注解@Component)package com.
- 1、首先,找到 Android SDK 在本机中的位置,如果不知道,可以通过在 Android Studio 找到,如下:2、其次,通过 c
- 1. 背景Java Persistence with Hibernate 在12.2.1小节使用如下例子描述 n+1查询问题:List<
- idea中的Maven导包失败问题解决总结1.先确定idea和Maven 的配置文件settings 没有问题找到我们本地的maven仓库,
- 前言:在日常的代码开发中,此处相信每个开发人员对代码质量都是高要求,有自己的一套代码规范,但是我们不是单独作战,往往大家都是团队作战,人是最
- 本文将介绍使用Spring Boot集成Mybatis并实现主从库分离的实现(同样适用于多数据源)。延续之前的Spring Boot 集成M