导航栏随滑动透明变化及悬浮重点讲述

效果显示:

随着滑动设置顶部标题栏透明,主要是在滑动监听里面做操作:

 mScrollView.setListener(new CompatibleScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(View v, int x, int y, int oldx, int oldy) {
                if (y < 10) {
                    mActionBarView.setWhileTheme(R.mipmap.ico_love_white);
                    mTabLayoutView2.setVisibility(View.GONE);
                } else {
                    float percent = y * 1f / (mBgImgView.getMeasuredHeight()-mActionBarView.getMeasuredHeight());
                    int alpha = (int) (255 * (percent > 1 ? 1f : percent));
                    int color = Color.argb(alpha, 255, 255, 255);
                    mActionBarView.setBlackTheme(R.mipmap.ico_love_black, color);
                    //是否显示tab置顶
                    mTabLayoutView2.setVisibility(alpha == 255 ? View.VISIBLE : View.GONE);
                }

            }

            @Override
            public void onScrollBottom(View v) {
                LogUtils.e("滑动到底部");
            }
        });

色值的计算:

float percent = y * 1f / (mBgImgView.getMeasuredHeight() - mActionBarView.getMeasuredHeight());
int alpha = (int) (255 * (percent > 1 ? 1f : percent));
int color = Color.argb(alpha, 255, 255, 255);

​​​​​​​mBgImgView是大图控件,mActionBarView是标题栏,这里的效果是大图滑到标题栏底部就不需要透明,所以范围只就需要
​​​​​​​
mBgImgView.getMeasuredHeight() - mActionBarView.getMeasuredHeight()之后才能开始计算percent  

因为透明度分为256阶(0-255),所以得到百分比之后需要乘255,之后才是真正的透明度值,上面color就是加上透明度的白色色值

至于上面悬浮,其实是有两条mTabLayoutView

mTabLayoutView1一直存在,mTabLayoutView2只是不透明的时候才显示处理,这里只是用了障眼法

猜你喜欢

转载自blog.csdn.net/u014476720/article/details/81507768