Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)
作者:Rexqqqqq 发布时间:2023-11-21 23:56:29
标签:Android,滑动Scrollview,标题栏渐变
Scrollview标题栏滑动渐变
仿京东样式(上滑显示下滑渐变消失)
/**
* @ClassName MyScrollView
* @Author Rex
* @Date 2021/1/27 17:38
*/
public class MyScrollView extends ScrollView {
private TranslucentListener mTranslucentListener;
public void setTranslucentListener(TranslucentListener translucentListener) {
this.mTranslucentListener = translucentListener;
}
public MyScrollView(Context context) {
this(context, null);
}
public MyScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mTranslucentListener != null) {
//ScrollView滑出高度
int scrollY = getScrollY();
//屏幕高度
int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
//有效滑动距离为屏幕2分之一
// alpha = 滑动高度/(screenHeight/3f)
if (scrollY <= screenHeight / 2f) {
Log.d(">>>>>>>>>", "ScrollView划出高度:" + scrollY);
Log.d(">>>>>>>>>", "屏幕高度:" + screenHeight);
Log.d(">>>>>>>>>", "渐变值:" + (0 + scrollY / (screenHeight / 4f)));
// 渐变的过程 1~0
mTranslucentListener.onTranslucent(0 + scrollY / (screenHeight /4f));
}
}
}
}
Activity 设置
public class ToolbarActivity extends AppCompatActivity implements TranslucentListener {
private Toolbar mToolBar;
private MyScrollView mScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toobar);
mToolBar = findViewById(R.id.id_toolbar);
mScrollView = findViewById(R.id.id_scrollView);
//初始化渐变为0
mToolBar.setAlpha(0);
//设置渐变回调
mScrollView.setTranslucentListener(this);
}
@Override
public void onTranslucent(float alpha) {
mToolBar.setAlpha(alpha);
}
}
渐变回调接口
/**
* @ClassName TranslucentListener
* @Author rex
* @Date 2021/1/27 17:38
*/
public interface TranslucentListener {
/**
* 透明度的回调监听
*
* @param alpha 0~1 透明度
*/
public void onTranslucent(float alpha);
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.rex.rxhttpdemo.MyScrollView
android:id="@+id/id_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button0" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button2" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button3" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button4" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</com.rex.rxhttpdemo.MyScrollView>
<androidx.appcompat.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
app:title="title"
/>
</RelativeLayout>
下滑显示上滑渐变消失
/**
* @ClassName MyScrollView
* @Author Rex
* @Date 2021/1/27 17:38
*/
public class MyScrollView extends ScrollView {
private TranslucentListener mTranslucentListener;
public void setTranslucentListener(TranslucentListener translucentListener) {
this.mTranslucentListener = translucentListener;
}
public MyScrollView(Context context) {
this(context, null);
}
public MyScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mTranslucentListener != null) {
//ScrollView滑出高度
int scrollY = getScrollY();
//屏幕高度
int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
//有效滑动距离为屏幕2分之一
// alpha = 滑动高度/(screenHeight/3f)
if (scrollY <= screenHeight / 2f) {
Log.d(">>>>>>>>>", "ScrollView划出高度:" + scrollY);
Log.d(">>>>>>>>>", "屏幕高度:" + screenHeight);
Log.d(">>>>>>>>>", "渐变值:" + (1 - scrollY / (screenHeight / 4f)));
// 渐变的过程 1~0
mTranslucentListener.onTranslucent(1 - scrollY / (screenHeight /4f));
}
}
}
}
注意: 这里只是更改了 mTranslucentListener.onTranslucent 里的 渐变值
Activty 里 把初始化 mToolBar.setAlpha(0); 去掉
XML
<com.rex.rxhttpdemo.MyScrollView
android:id="@+id/id_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize"
>
</com.rex.rxhttpdemo.MyScrollView>
xml 加入 paddingtop .
注意:
android:clipChildren=“false”
android:clipToPadding="false"
这俩个属性 如果不加会有留白
来源:https://blog.csdn.net/weixin_43841463/article/details/113308344


猜你喜欢
- 1.算法效率算法效率分析分为两种:第一种是时间效率,第二种是空间效率。时间效率被称为时间复杂度,而空间效率被称作空间复杂度。时间复杂度主要衡
- AES简介AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预
- 本文实例讲述了C#计算矩阵的逆矩阵方法。分享给大家供大家参考。具体如下:1.代码思路1)对矩阵进行合法性检查:矩阵必须为方阵2)计算矩阵行列
- 使用 LogcatLogcat是日常开发的重要组成部分。如果您看到其中一个“强制关闭”或&l
- 问题1springboot注册 * 过滤器方法注册 * :在启动类中注册bean@EnableWebMvc@Configurationsta
- 本文实例讲述了JAVA基于数组实现的商品信息查询功能。分享给大家供大家参考,具体如下:综合一维数组和二维数组的相关知识,以及数组排序的多种算
- 一、什么是网关限流:在微服务架构中,网关层可以屏蔽外部服务直接对内部服务进行调用,对内部服务起到隔离保护的作用,网关限流,顾名思义,就是通过
- 项目概况:Spring Cloud搭的微服务,使用了eureka,FeignClient,现在遇到FeignClient调用接口时不支持上传
- 在用java的io流读写文件时,总是被它的各种流能得很混乱,有40多个类,理清啦,过一段时间又混乱啦,决定整理一下!以防再忘Java输入/输
- 本文实例讲述了winform实现创建最前端窗体的方法。分享给大家供大家参考。具体实现方法如下:一、需求:1).需要这个窗体始终处于前端而且可
- java 线程锁在Java线程中运用synchronized关键字来达到同步的 synchronized可以锁方法,锁类,锁对象,锁代码块方
- 组件在容器(比如Jframe)中的位置和大小是由布局管理器来决定的。所有的容器都会使用一个布局管理器,通过它来自动进行组件的布局管理。种类j
- 添加Hibernate配置文件提示 解压hibernate.jar包 在org\hibernate目录下找到hibernate-config
- 1. interrupt知识点 以下总结基于JDK8本文不会完整说明interrupt,只会罗列一些比较重要的点。完整了解Thre
- 本文实例讲述了C#实现json格式转换成对象并更换key的方法。分享给大家供大家参考。具体分析如下:由于是不标准的序列化对象类型,因此你无法
- 本文实例讲述了Java Swing中JTable渲染器与编辑器用法。分享给大家供大家参考,具体如下:JTable的内容、外观、事件响应在很大
- 1. 概述官方JavaDocsApi: javax.swing.JPanelJPanel,面板。JPanel 是在开发中使用频率非常高的一般
- JUnit是Java中最有名的单元测试框架,用于编写和运行可重复的测试,多数Java的开发环境都已经集成了JUnit作为单元测试的工具。好的
- 项目地址:gitee.com/baojh123/rp…netty-study 这个项目是没用到的,可以删掉,主要是测试
- 在使用xutils时遇到不能添加以来的问题,花了很长时间终于解决,网上添加依赖的方法很多,在此针对个人出现的问题作下笔记。我本想使用jar包