直播源码,悬浮窗滚动渐变色效果
直播源码,悬浮窗滚动渐变色效果实现的相关代码
首先自定义ScrollView
MyScrollView
1 | <br>import android.content.Context;<br>import android.util.AttributeSet;<br>import android.widget.ScrollView;<br> /**<br> * @Author:Administrator<br> * @E-mail: victory52@163.com<br> * @Date:2020/8/6 9:43<br> * @Description:描述信息<br> */ <br> public class MyScrollView extends ScrollView {<br> private OnScrollListener onScrollListener;<br> public MyScrollView(Context context) {<br> this(context, null);<br> }<br> public MyScrollView(Context context, AttributeSet attrs) {<br> this(context, attrs, 0);<br> }<br> public MyScrollView(Context context, AttributeSet attrs, int defStyle) {<br> super(context, attrs, defStyle);<br> }<br> public void setOnScrollListener(OnScrollListener onScrollListener) {<br> this.onScrollListener = onScrollListener;<br> }<br> @Override<br> protected void onScrollChanged(int l, int t, int oldl, int oldt) {<br> super.onScrollChanged(l, t, oldl, oldt);<br> if (onScrollListener != null) {<br> onScrollListener.onScroll(t);<br> }<br> }<br> public interface OnScrollListener {<br> public void onScroll(int scrollY);<br> }<br>} |
在布局中使用MyScrollView
1 | <br><?xml version= "1.0" encoding= "utf-8" ?><br><LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" <br> android:id= "@+id/ll_parent" <br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" <br> android:orientation= "vertical" ><br> <com.utils.MyScrollView<br> android:id= "@+id/scrollView" <br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" ><br> <FrameLayout<br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" ><br> <LinearLayout<br> android:layout_width= "match_parent" <br> android:layout_height= "wrap_content" <br> android:descendantFocusability= "blocksDescendants" <br> android:orientation= "vertical" ><br> <View<br> android:id= "@+id/imageview" <br> android:layout_width= "match_parent" <br> android:layout_height= "500dp" <br> android:background= "@android:color/holo_blue_dark" /><br><!--滑动时展示的title(内固定view)--><br> <LinearLayout<br> android:id= "@+id/ll_filter" <br> android:layout_width= "match_parent" <br> android:layout_height= "45dp" <br> android:background= "#00EAFF" <br> android:orientation= "horizontal" <br> android:gravity= "center" ><br> <TextView<br> android:layout_width= "wrap_content" <br> android:layout_height= "wrap_content" <br> android:gravity= "center" /><br> </LinearLayout><br> <View<br> android:layout_width= "match_parent" <br> android:layout_height= "1000dp" <br> android:background= "@android:color/holo_green_light" /><br> <View<br> android:layout_width= "match_parent" <br> android:layout_height= "500dp" <br> android:background= "#f00" /><br> <br> <RelativeLayout<br> android:layout_width= "match_parent" <br> android:layout_height= "wrap_content" ><br> <androidx.recyclerview.widget.RecyclerView<br> android:id= "@+id/recyclerView" <br> android:layout_width= "match_parent" <br> android:layout_height= "wrap_content" /><br> </RelativeLayout><br> </LinearLayout><br><!--滑动之后在最上面展示的title(外固定view)--><br> <LinearLayout<br> android:id= "@+id/ll_top" <br> android:layout_width= "match_parent" <br> android:layout_height= "45dp" <br> android:background= "#FF0000" <br> android:orientation= "horizontal" ><br> <TextView<br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" <br> android:gravity= "center" <br> android:text= "外固定View" /><br> <androidx.recyclerview.widget.RecyclerView<br> android:id= "@+id/recycler_view_home_title" <br> android:layout_width= "match_parent" <br> android:layout_height= "@dimen/dp_50" <br> android:layout_centerHorizontal= "true" <br> android:layout_marginLeft= "@dimen/dp_24" <br> android:layout_marginTop= "@dimen/dp_12" <br> ><br> </androidx.recyclerview.widget.RecyclerView><br> </LinearLayout><br> </FrameLayout><br> </com.utils.MyScrollView><br></LinearLayout> |
kotlin
先写ScrollView监听
scrollView.setOnScrollListener(this)
颜色渐变,获取imageview的高度来设置渐变
1 | <br>override fun onScroll(scrollY: Int) {<br> val mTop = scrollY.coerceAtLeast(ll_filter.top)<br> ll_top.layout(0, mTop, ll_top.width, mTop + ll_top.height)<br> /*<br> * 颜色渐变(不需要渐变可以把下面的删掉)<br> * */ <br> var imageHeight = imageview.getHeight()<br> if (scrollY <= imageHeight) {<br> val scale: Float = scrollY.toFloat() / imageHeight<br> val alpha = 255 * scale<br> // 只是layout背景透明(仿知乎滑动效果)白色透明<br> ll_top.setBackgroundColor(Color.argb(alpha.toInt(), 255, 255, 255))<br> // 设置文字颜色,黑色,加透明度<br> //textView.setTextColor(Color.argb(alpha.toInt(), 0, 0, 0))<br> Log.e("111", "y > 0 && y <= imageHeight")<br> }<br> } |
以上就是 直播源码,悬浮窗滚动渐变色效果实现的相关代码,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现