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="我是標(biāo)題"
? ? ? ? ? ? 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? 自定義滾動(dòng)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;
? ? /**
? ? * 主要是用在用戶(hù)手指離開(kāi)MyScrollView雳窟,MyScrollView還在繼續(xù)滑動(dòng)伐蒂,我們用來(lái)保存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);
? ? }
? ? /**
? ? * 設(shè)置滾動(dòng)接口
? ? * @param onScrollListener
? ? */
? ? public void setScrolListener(OnScrollListener onScrollListener){
? ? ? ? this.onScrollListener = onScrollListener;
? ? }
? ? /**
? ? * 用于用戶(hù)手指離開(kāi)MyScrollView的時(shí)候獲取MyScrollView滾動(dòng)的Y距離裸违,然后回調(diào)給onScroll方法中
? ? */
? ? private Handler handler = new Handler() {
? ? ? ? public void handleMessage(android.os.Message msg) {
? ? ? ? ? ? int scrollY = MyScrollView.this.getScrollY();
? ? ? ? ? ? if(onScrollListener != null){
? ? ? ? ? ? ? ? onScrollListener.onScroll(scrollY);
? ? ? ? ? ? }
? ? ? ? ? ? //此時(shí)的距離和記錄下的距離不相等音同,在隔5毫秒給handler發(fā)送消息
? ? ? ? ? ? if(lastScrollY != scrollY){
? ? ? ? ? ? ? ? lastScrollY = scrollY;
? ? ? ? ? ? ? ? handler.sendMessageDelayed(handler.obtainMessage(), 5);
? ? ? ? ? ? }
? ? ? ? }
? ? };
? ? /**
? ? * 重寫(xiě)onTouchEvent欠动, 當(dāng)用戶(hù)的手在MyScrollView上面的時(shí)候贸营,
? ? * 直接將MyScrollView滑動(dòng)的Y方向距離回調(diào)給onScroll方法中骇吭,當(dāng)用戶(hù)抬起手的時(shí)候橙弱,
? ? * MyScrollView可能還在滑動(dòng),所以當(dāng)用戶(hù)抬起手我們隔20毫秒給handler發(fā)送消息燥狰,在handler處理
? ? * MyScrollView滑動(dòng)的距離
? ? */
? ? @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{
? ? ? ? /**
? ? ? ? * 回調(diào)方法棘脐, 返回MyScrollView滑動(dòng)的Y方向距離
? ? ? ? * @param scrollY
? ? ? ? *? ? ? ? ? ? ? 、
? ? ? ? */
? ? ? ? public void onScroll(int scrollY);
? ? }
}
Main里面實(shí)現(xiàn)
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>