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
0
投稿
猜你喜欢
- spring 多文件配置:1、properties文件2、YAML文件一、properties文件在 Spring Boot 中, 多环境配
- jwt简介冒泡排序:(Bubble Sort)是一种简单的交换排序。之所以叫做冒泡排序,因为我们可以把每个元素当成一个小气泡,根据气泡大小,
- 拿到了项目框架工程代码却没有uml图,那么方法之间的调用关系功能流转就不容易看出来,那么如何产生类图呢,记忆里方法有下:1.rose逆向工程
- 相信大家肯定都在电商网站买过东西,当我们看中一件喜欢又想买的东西时,这时候你又不想这么快结账,这时候你就可以放入购物车;就像我们平时去超市买
- 一:获取根目录的方法取得控制台应用程序的根目录方法方法1、Environment.CurrentDirectory 取得或设置当前工作目录的
- 以前只知道控件的onTouchEvent()事件,它的动作有MotionEvent.ACTION_DOWN、MotionEvent.ACTI
- 1、jdbc1) 含义:JDBC是java语言连接数据库,Java Date Base Connectivity2) jdbc的本质:在编程
- Android应用强制更新的用途十分广泛,特别上刚上线的应用肯定会存在或多或少的bug,特别是涉及移动支付这一块的内容,如果出错了会造成比较
- Interceptor讲到Interceptor,相信熟悉struts2的童鞋肯定不会陌生了,struts2可以自定义 * 进行自己想要的一
- 本文实例为大家分享了java实现酒店管理系统的具体代码,供大家参考,具体内容如下编写环境:MyEclipse2014+sql server2
- 从java14开始, switch语句有了一个很大的调整, 这就让swicth语句有了更多的操作和选择,在代码上,更加的简便灵活.switc
- 该工具包含是封装了jedis,包含redis.properties和jedisPool,序列化使用的是protostuff,map类型操作使
- 开发中有时候需要自己封装分页排序时,List如何对某一属性排序呢,分享一个小实例,大家共勉,希望能对大家有用,请多多指教。1.Student
- 本文实例讲述了Android编程之交互对话框。分享给大家供大家参考,具体如下:1. 在Android SDK中,虽然有许多的窗口,有些类似M
- 测试是开发的一个非常重要的方面,可以在很大程度上决定一个应用程序的命运。良好的测试可以在早期捕获导致应用程序崩溃的问题,但较差的测试往往总是
- 最近公司要求开发工具要用Idea,作为一个eclipse的老员工,记录一下Idea中遇到的坑刚开始用Idea从Git上导入一个项目时,遇到了
- C#一些延时函数sleep延时方法System.Threading.Thread.Sleep(1000); //毫秒实现的是非独占性延时函数
- 1、锁优化在JDK6之前,通过synchronized来实现同步效率是很低的,被synchronized包裹的代码块经过javac编译后,会
- Singleton是众多设计模式中最容易理解的一种,也是众多设计模式中较为重要的一种设计模式。接下来我们看看具体介绍。Singleton模式
- 把spring-boot项目按照平常的web项目一样发布到tomcat容器下一、修改打包形式在pom.xml里设置 <packagin