android滑动改变状态栏颜色及透明度

RecyclerView根据滑动改变状态栏

 //滑动监听事件
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            //dy:每一次竖直滑动增量 向下为正 向上为负
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                final float density = mContext.getResources().getDisplayMetrics().density;

                //滑动的距离
                mDistanceY += dy;
                //toolbar的高度
                int toolbarHeight = (int) (214f * density) - mSearchLinear.getHeight();
                //当滑动的距离 <= toolbar高度的时候,改变Toolbar背景色的透明度,达到渐变的效果
                if (mDistanceY <= toolbarHeight) {
                    mSearchKefu.setImageResource(R.mipmap.icon_kf_white);
                    mSearchSaoyisao.setImageResource(R.mipmap.icon_sys_white);
//                    searchLocationIma.setBackgroundResource(R.mipmap.icon_location_white);
                    if (isIma) {
                        searchLocationImaBlack.setVisibility(View.GONE);
                        searchLocationIma.setVisibility(View.VISIBLE);
                    } else {
                        searchLocationText.setTextColor(Color.WHITE);
                        searchLocationText.setCompoundDrawables(null, null, drawable2, null);
                    }
                    mSearchEdit.setTextColor(Color.WHITE);
//                    mSearchLinearEdit.setBackgroundResource(R.drawable.bg_search);

                    setStateBarTextColor(false);
                    barState = "白";

                    float scale = (float) mDistanceY / toolbarHeight;
                    float alpha = scale * 255;
                    mSearchLinear.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                } else if (mDistanceY >= toolbarHeight) {
                    mSearchLinear.setBackgroundResource(R.color.white);
//                    searchLocationIma.setBackgroundResource(R.mipmap.icon_location);
                    if (isIma) {
                        searchLocationImaBlack.setVisibility(View.VISIBLE);
                        searchLocationIma.setVisibility(View.GONE);
                    } else {
                        searchLocationText.setTextColor(Color.BLACK);
                        searchLocationText.setCompoundDrawables(null, null, drawable1, null);
                    }

                    mSearchKefu.setImageResource(R.mipmap.icon_kf_black);
                    mSearchSaoyisao.setImageResource(R.mipmap.icon_sys_black);
                    mSearchEdit.setTextColor(Color.parseColor("#3B4664"));

//                    mSearchLinearEdit.setBackgroundResource(R.drawable.bg_search_hide);

                    setStateBarTextColor(true);
                    barState = "黑";
                } else {
                    //上述虽然判断了滑动距离与toolbar高度相等的情况,但是实际测试时发现,标题栏的背景色
                    //很少能达到完全不透明的情况,所以这里又判断了滑动距离大于toolbar高度的情况,
                    //将标题栏的颜色设置为完全不透明状态
//                    setStateBarTextColor(true);
//                    barState = "黑";
//                    mSearchLinear.setBackgroundResource(R.color.white);
//'
//                    searchLocationIma.setBackgroundResource(R.mipmap.icon_location);
//                    mSearchKefu.setImageResource(R.mipmap.icon_kf_black);
//                    mSearchSaoyisao.setImageResource(R.mipmap.icon_sys_black);
//                    mSearchEdit.setTextColor(Color.GRAY);
//                    searchLocationText.setTextColor(Color.GRAY);
                }
            }
        });

根据ScrollView滑动改变

    //设置滑动
        scrollView.setOnScrollistener(new MyScrollView.OnScrollistener() {
            @Override
            public void onScroll(int startY, int endY) {
                changeAphla(startY, endY);
            }
        });



   /**
     * 根据内容窗体的移动改变标题栏背景透明度
     *
     * @param startY scrollview开始滑动的y坐标(相对值)
     * @param endY   scrollview结束滑动的y坐标(相对值)
     */
    private void changeAphla(int startY, int endY) {
        //获取标题高度
        int titleHeight = titleFrame.getMeasuredHeight();
        //获取背景高度
        int backHeight = doctorHomeTxBg.getMeasuredHeight();

        //获取控件的绝对位置坐标
        int[] location = new int[2];
        doctorHomeTxBg.getLocationInWindow(location);
        //从屏幕顶部到控件顶部的坐标位置Y
        int currentY = location[1];
        //表示回到原位(滑动到顶部)
        if (currentY >= 0) {
            titleFrame.getBackground().mutate().setAlpha(0);
        }

        //颜色透明度改变
        if (currentY < titleHeight && currentY >= -(backHeight - titleHeight)) {
            //计算出滚动过程中改变的透明值
            double y = Math.abs(currentY) * 1.0;
            double height = (backHeight - titleHeight) * 1.0;
            int changeValue = (int) (y / height * 255);

            //判断是向上还是向下
            if (endY > startY) {    //向上;透明度值增加,实际透明度减小
                titleFrame.getBackground().mutate().setAlpha(changeValue);
            } else if (endY < startY) {     //向下;透明度值减小,实际透明度增加
                titleFrame.getBackground().mutate().setAlpha(changeValue);
            }
        }
        //背景移除屏幕
        if (currentY < -(backHeight - titleHeight)) {
            titleFrame.getBackground().mutate().setAlpha(255);
            titleLeftBack.setImageResource(R.mipmap.jt_left);
            titleRight.setImageResource(R.mipmap.icon_more);
            title.setTextColor(Color.BLACK);
            setStateBarTextColor(true);
        } else {
            titleLeftBack.setImageResource(R.mipmap.icon_back_left_white);
            titleRight.setImageResource(R.mipmap.icon_more_white);
            title.setTextColor(Color.WHITE);
            setStateBarTextColor(false);
        }
    }

自定义ScrollView

public class MyScrollView extends NestedScrollView {

    private OnScrollistener onScrollistener;

    public OnScrollistener getOnScrollistener() {
        return onScrollistener;
    }

    public void setOnScrollistener(OnScrollistener onScrollistener) {
        this.onScrollistener = onScrollistener;
    }

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

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

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

    public interface OnScrollistener {

        void onScroll(int startY, int endY);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        onScrollistener.onScroll(oldt, t);
        super.onScrollChanged(l, t, oldl, oldt);
    }
}

改变状态栏字体颜色

  /**
     * 修改状态栏字体颜色
     *
     * @param dark
     */
    private void setStateBarTextColor(boolean dark) {
        View decor = getWindow().getDecorView();
        if (dark) {
            //黑
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            //白
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

 

你可能感兴趣的:(android,android,scrollview,recyclerview,状态栏字体颜色)