Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)
作者:Rexqqqqq 时间:2023-11-21 23:56:29
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
标签:Android,滑动Scrollview,标题栏渐变
0
投稿
猜你喜欢
Spring Boot从Controller层进行单元测试的实现
2023-07-21 03:07:10
SpringBoot动态Feign服务调用详解
2022-05-20 13:26:23
C#中Equals方法的常见误解
2023-03-10 21:59:23
springboot项目如何防止XSS攻击
2021-10-17 10:03:02
ADO.NET实体数据模型详细介绍
2023-10-16 12:15:41
Java数据结构之链表相关知识总结
2023-11-02 00:29:28
Springboot如何使用mybatis实现拦截SQL分页
2021-08-15 13:16:56
android开发之调用手机的摄像头使用MediaRecorder录像并播放
2021-12-27 12:50:18
Android操作SQLite基本用法
2022-04-28 20:42:02
使用Springboot封装一个自适配的数据单位转换工具类
2022-06-07 07:12:14
C#怎样实现文件下载断点续传
2023-02-23 19:11:41
android多媒体音乐(MediaPlayer)播放器制作代码
2022-01-06 01:13:20
C#索引属性用法实例分析
2023-02-02 14:15:19
IntelliJ IDEA修改新建文件自动生成注释的user名
2021-12-29 04:27:38
将InputStream转化为base64的实例
2023-04-24 02:30:59
C#中Socket与Unity相结合示例代码
2022-09-15 23:24:15
浅谈关于Mybatis的mapper-locations配置问题
2023-09-24 06:06:16
Android 处理OnItemClickListener时关于焦点颜色的设置问题
2021-09-02 01:47:00
Java多线程ThreadAPI详细介绍
2023-04-14 12:19:18
浅谈JVM垃圾回收之哪些对象可以被回收
2021-08-14 18:57:47