直播原始碼,懸浮窗滾動漸變色效果

zhibo系統開發發表於2022-03-01

直播原始碼,懸浮窗滾動漸變色效果實現的相關程式碼

首先自定義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="
    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")
        }
    }

以上就是 直播原始碼,懸浮窗滾動漸變色效果實現的相關程式碼,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2861679/,如需轉載,請註明出處,否則將追究法律責任。

相關文章