android 标题栏颜色渐变和阴影,ScrollView上下滑动监听,及判断scrollView是否滚动到底部

1、创建  ScrollListener 接口监听滑动距离

public interface ScrollListener {
    void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY);
}

 

view阴影属性

android:elevation="4dp"

2、重写 ScrollView  自定义  ScrollListenerView

 

public class ScrollListenerView extends ScrollView {
    private ScrollListener scrollViewListener = null;

    public ScrollListenerView(Context context) {
        super(context);
    }

    public ScrollListenerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollListenerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollListener(ScrollListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldX, int oldY) {
        super.onScrollChanged(x, y, oldX, oldY);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldX, oldY);
        }
    }
}

 

判断scrollView是否滚动到底部方法

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);
    if (scrollViewListener != null) {
        scrollViewListener.onScrollChanged(this, x, y, oldX, oldY);
    }

    if (getScrollY() + getHeight() >= computeVerticalScrollRange()) {
        Log.d("lgq", "------滚动到最下方------");
    } else {
        Log.d("Lgq", "没有到最下方");
    }
}

 

3、在layout xml文件使用ScrollListenerView

<com.tianxinyw.mapclient.views.ScrollListenerView
    android:id="@+id/slv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
</RelativeLayout>
    </com.tianxinyw.mapclient.views.ScrollListenerView>

上下层标题渐变色布局,titlelitwo是下层

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_popup"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/titlelitwo"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:background="@color/homeiconokc"
        android:gravity="center"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/titletetwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="天鑫计费2"
            android:textColor="@color/white"
            android:textSize="16dp"
            android:textStyle="bold"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/lishititleli"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:background="@color/homeiconokc"
        android:gravity="center"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/lishititlete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="天鑫计费"
            android:textColor="@color/white"
            android:textSize="16dp"
            android:textStyle="bold"/>
    </LinearLayout>

 

 

4、Activity 实现滑动距离监听接口

public class HomeFragment extends BaseFragment implements ScrollListener{

初始化 

@BindView(R.id.slv)
ScrollListenerView slv;

设置监听

slv.setScrollListener(this);

实现方法:

@Override
public void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY) {
    if (y - oldY > 0&&y>0) {
        float f = (y + 0f) / 400;//滑动距离350px
        if (f > 1) {
            f = 1f;
        }
        if (f < 0) {
            f = 0;
        }
        if (f > 0 && f < 0.3) {
            f = (float) 0.333;
        }
        Log.i("lgq", "......向上滑。。。" + f + ".......y====" + y);
        lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 - f * 1 * 0xff)));
        lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 - f * 1 * 0xff)));
        titlelitwo.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (f * 1 * 0xff)));
        titletetwo.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (f * 1 * 0xff)));
    } else if (y-oldY<0&&y>=0){
        float f = (y + 0f) / 400;//滑动距离350px
        if (f > 1) {
            f = 1f;
        }
        if (f < 0) {
            f = 0;
        }
        Log.e("lgq", "......向xia滑。。。" + f + ".......y====" + y + "......zhi====" + (1 - f * 1 * 0xff) + ".....yy===" + f * 1 * 0xff);
        if (y <= 0||f<0.01) {
            lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 * 1 * 0xff)));
            lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 * 1 * 0xff)));
            return;
        }
        lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 - f * 1 * 0xff)));
        lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 - f * 1 * 0xff)));
        titlelitwo.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (f * 1 * 0xff)));
        titletetwo.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (f * 1 * 0xff)));
    }

}

工具方法:

/**
 * 修改颜色透明度
 * @param color
 * @param alpha
 * @return
 */
public static int changeAlpha(int color, int alpha) {
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);

    return Color.argb(alpha, red, green, blue);
}

 

实现效果:向上滑动渐变颜色,显示隐藏view

 

 

demo链接:https://download.csdn.net/download/meixi_android/10966003

 

 

附:左滑右滑手势

 //渐变色
    float x1 = 0;
    float x2 = 0;
    private int nowpersion = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            Log.i("lgq","ssssMainActivity_dispatchTouchEvent==="+ev.getX());
            x1 = ev.getX();
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            x2 = event.getX();
            float h = x1 - x2;

//            Log.i("lgq","eee==滑动距离aaa===="+(h)+"......"+nowpersion+"...."+x1);

            float f = (h + 0f) / 640;//滑动距离350px
            if (f > 1) {
                f = 1f;
            }
            if (f < 0) {
                f = 0;
            }
            if (nowpersion == 0 && h < 0) {
                return false;
            } else if (nowpersion == 0 && h > 0) {
                if (f < 0.4) {
                    return false;
                }
                TextView textView = tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tab_iv);
                TextView textView0 = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab_iv);
                textView.setTextColor(changeAlpha(ContextCompat.getColor(MainActivity.this, R.color.textlan), (int) (f * 1 * 0xff)));
                textView0.setTextColor(changeAlpha(ContextCompat.getColor(MainActivity.this, R.color.texthui), (int) (f * 1 * 0xff)));
            }
        }

        return false;
    }

    /**
     * 修改颜色透明度
     * @param color
     * @param alpha
     * @return
     */
    public  int changeAlpha(int color, int alpha) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);

        return Color.argb(alpha, red, green, blue);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值