Android 监听手指滑动,Toolbar颜色渐变

20 篇文章 1 订阅

Step 1:布局文件

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fitsSystemWindows="true"
              android:orientation="vertical">

    <com.xxx.MyScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        android:clipChildren="false"
        android:clipToPadding="false"
        >

      ......
    </com.xxx.MyScrollView>
    <include
        layout="@layout/toolbar_layout"
        android:visibility="visible"/>

</RelativeLayout>
 

Step 2:自定义scrollview的code,因为scrollview的滑动监听不兼容低版本,重写方法

 

public class MyScrollView extends ScrollView {

    AlphaChangeListener alphaChangeListener;

    private float xLast, yLast, xDistance, yDistance;

    // ScrollView的子View, 也是ScrollView的唯一一个子View
    private View contentView;

    // 用于记录正常的布局位置
    private Rect originalRect = new Rect();

    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);
    }

    @Override
    protected void onFinishInflate() {
        if (getChildCount() > 0) {
            contentView = getChildAt(0);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);


        if (contentView == null)
            return;

        // ScrollView中的唯一子控件的位置信息, 这个位置信息在整个控件的生命周期中保持不变
        originalRect.set(contentView.getLeft(), contentView.getTop(),
                contentView.getRight(), contentView.getBottom());
    }

    /**
     * 在这里解决滑动上下滑动,左右滑动冲突
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance) {
                    return false;   //表示向下传递事件
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    public void setAlphaChangeListener(AlphaChangeListener alphaChangeListener) {
        this.alphaChangeListener = alphaChangeListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (alphaChangeListener != null) {
            int scrollY = getScrollY();
            int screen_height = getContext().getResources().getDisplayMetrics().heightPixels;
            if (scrollY <= screen_height / 3f) {//0~1f,而透明度应该是1~0f
                alphaChangeListener.alphaChanging( scrollY / (screen_height / 3f));//alpha=滑出去的高度/(screen_height/3f)}
            }
        }
    }
}

 

Step 3:外部使用的接口回调

public interface AlphaChangeListener {
     void alphaChanging(float alpha);
}

Step 4:activity中的使用

public class xxx extends BaseFragment implements AlphaChangeListener {
   
    @BindView(R.id.scrollView)
    MyScrollView scrollView;
    private Banner banner;
    private Toolbar toolbar;

 	......


    @Override
    protected void initView(View view) {
        toolbar = view.findViewById(R.id.toolbar);
        TextView toolbarTitle = toolbar.findViewById(R.id.tv_title);
        toolbarTitle.setText("");
        toolbar.setVisibility(View.GONE);
        scrollView.setAlphaChangeListener(this);

    }
    @Override
    public void alphaChanging(float alpha) {
        toolbar.setVisibility(View.VISIBLE);
        toolbar.setAlpha(alpha);
    }
}

 

 

搞定,完美……

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个小狼娃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值