Android ScrollView实现悬浮窗滚动渐变

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
首先自定义ScrollView
MyScrollView


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

/**
 * @Author:Administrator
 * @E-mail: victory52@163.com
 * @Date:2020/8/6 9:43
 * @Description:描述信息
 */
public class MyScrollView extends ScrollView {
    private OnScrollListener onScrollListener;

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onScrollListener != null) {
            onScrollListener.onScroll(t);
        }
    }

    public interface OnScrollListener {
        public void onScroll(int scrollY);
    }
}

在布局中使用MyScrollView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.utils.MyScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical">

                <View
                    android:id="@+id/imageview"
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:background="@android:color/holo_blue_dark" />
				<!--滑动时展示的title(内固定view)-->
                <LinearLayout
                    android:id="@+id/ll_filter"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:background="#00EAFF"
                    android:orientation="horizontal"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center" />

                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1000dp"
                    android:background="@android:color/holo_green_light" />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:background="#f00" />
                    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                </RelativeLayout>
            </LinearLayout>
			<!--滑动之后在最上面展示的title(外固定view)-->
            <LinearLayout
                android:id="@+id/ll_top"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="#FF0000"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="外固定View" />
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view_home_title"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_50"
                    android:layout_centerHorizontal="true"
                    android:layout_marginLeft="@dimen/dp_24"
                    android:layout_marginTop="@dimen/dp_12"
                    >

                </androidx.recyclerview.widget.RecyclerView>
            </LinearLayout>

        </FrameLayout>
    </com.utils.MyScrollView>

</LinearLayout>

kotlin
先写ScrollView监听
scrollView.setOnScrollListener(this)
颜色渐变,获取imageview的高度来设置渐变

	override fun onScroll(scrollY: Int) {
        val mTop = scrollY.coerceAtLeast(ll_filter.top)
        ll_top.layout(0, mTop, ll_top.width, mTop + ll_top.height)

        /*
        * 颜色渐变(不需要渐变可以把下面的删掉)
        * */
        var imageHeight = imageview.getHeight()
        if (scrollY <= imageHeight) {
            val scale: Float = scrollY.toFloat() / imageHeight
            val alpha = 255 * scale
            // 只是layout背景透明(仿知乎滑动效果)白色透明
            ll_top.setBackgroundColor(Color.argb(alpha.toInt(), 255, 255, 255))
            //                          设置文字颜色,黑色,加透明度
            //textView.setTextColor(Color.argb(alpha.toInt(), 0, 0, 0))
            Log.e("111", "y > 0 && y <= imageHeight")
        }
    }

以上就是悬浮头部,加渐变(本文只用于本人记录)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值