title.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/top_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical"> <View android:id="@+id/viewTop" android:layout_width="match_parent" android:layout_height="25dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="45dp"> <ImageView android:id="@+id/toolbar_iv_back" android:layout_width="45dp" android:layout_height="match_parent" android:layout_gravity="center" android:padding="14dp" android:src="@drawable/img_back" /> <TextView android:id="@+id/toolbar_tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="45dp" android:layout_marginRight="60dp" android:ellipsize="end" android:gravity="center" android:lines="1" android:text="我是标题" android:textSize="17sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true"> <TextView android:id="@+id/toolbar_tv_menu" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:lines="1" android:paddingLeft="10dp" android:paddingRight="10dp" android:textSize="14sp" /> <ImageView android:id="@+id/toolbar_iv_menu" android:layout_width="45dp" android:layout_height="match_parent" android:padding="14dp" android:visibility="gone" /> </LinearLayout> </RelativeLayout> <View android:id="@+id/view_line" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/background" /> </LinearLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.cy.myapplication.MyScrollView android:id="@+id/scroller" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="2000dp" android:background="@color/colorAccent" /> </LinearLayout> </com.cy.myapplication.MyScrollView> <include layout="@layout/title" /> </RelativeLayout>
MyScrollView 自定义滚动View
package com.cy.myapplication; import android.content.Context; import android.os.Build; import android.os.Handler; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; import androidx.annotation.RequiresApi; /** * Created by ShinnyYang on 2020/8/18. */ public class MyScrollView extends ScrollView { private OnScrollListener onScrollListener; /** * 主要是用在用户手指离开MyScrollView,MyScrollView还在继续滑动,我们用来保存Y的距离,然后做比较 */ private int lastScrollY; public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /** * 设置滚动接口 * @param onScrollListener */ public void setScrolListener(OnScrollListener onScrollListener){ this.onScrollListener = onScrollListener; } /** * 用于用户手指离开MyScrollView的时候获取MyScrollView滚动的Y距离,然后回调给onScroll方法中 */ private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { int scrollY = MyScrollView.this.getScrollY(); if(onScrollListener != null){ onScrollListener.onScroll(scrollY); } //此时的距离和记录下的距离不相等,在隔5毫秒给handler发送消息 if(lastScrollY != scrollY){ lastScrollY = scrollY; handler.sendMessageDelayed(handler.obtainMessage(), 5); } } }; /** * 重写onTouchEvent, 当用户的手在MyScrollView上面的时候, * 直接将MyScrollView滑动的Y方向距离回调给onScroll方法中,当用户抬起手的时候, * MyScrollView可能还在滑动,所以当用户抬起手我们隔20毫秒给handler发送消息,在handler处理 * MyScrollView滑动的距离 */ @Override public boolean onTouchEvent(MotionEvent ev) { if(onScrollListener != null){ onScrollListener.onScroll(lastScrollY = this.getScrollY()); } if(ev.getAction() == MotionEvent.ACTION_UP){ handler.sendMessageDelayed(handler.obtainMessage(), 20); } return super.onTouchEvent(ev); } public interface OnScrollListener{ /** * 回调方法, 返回MyScrollView滑动的Y方向距离 * @param scrollY * 、 */ public void onScroll(int scrollY); } }
Main里面实现
private LinearLayout top_bg; private MyScrollView scroller;
private void initView() { top_bg = (LinearLayout) findViewById(R.id.top_bg); scroller = (MyScrollView) findViewById(R.id.scroller); top_bg.getBackground().setAlpha(0); scroller.setScrolListener(this); }
@Override public void onScroll(int scrollY) { if (scrollY < 100) { top_bg.getBackground().setAlpha(0); } else if (scrollY >= 100 && scrollY < 860) { top_bg.getBackground().setAlpha((scrollY - 100) / 3); } else { top_bg.getBackground().setAlpha(255); } }
<color name="white">#ffffff</color> <color name="black">#000000</color> <color name="background">#f6f6f6</color>