Android 滑动渐变 扩展效果 免费专区




    

        


            

                

                    

                    

 

public class FreeActivity extends BaseActivity {

    private int llTopRootHeight;
    private int changeHeight;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_free;
    }

    @Override
    public boolean setFitSystemWindow() {
        return false;
    }

    @Override
    protected void initBar() {

        // 设置允许布局超出状态栏
        BarUtils.transparentStatusBar(this);

        // 设置白底黑子模式
        BarUtils.setStatusBarLightMode(this, false);

    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initView() {
        // 设置自定义顶上去的布局为状态栏的高度 覆盖效果
        mViewBinding.viewStatusBar2.getLayoutParams().height = BarUtils.getStatusBarHeight();
        //设置默认隐藏
        mViewBinding.lltop.setAlpha(0);

        BarUtils.addMarginTopEqualStatusBarHeight(mViewBinding.frame);

        //设置顶部图片的高度 防止图片拉伸
//        int ivTopHeight = ScreenUtils.getAppScreenWidth() * 486 / 750;
//        mViewBinding.ivTop.getLayoutParams().height = ivTopHeight;


        //布局里边butFlag 的高度
        int dimension = (int) getResources().getDimension(R.dimen.x100);

        //获取控件的试图观察者,以便通过试图观察者得到控件的宽高参数
        ViewTreeObserver viewTreeObserver = mViewBinding.ivTop.getViewTreeObserver();
        //添加观察者监听
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                //回调监听后首先移除该监听 占资源
                mViewBinding.ivTop.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //得到高度
                changeHeight = mViewBinding.ivTop.getHeight() - dimension;
                mViewBinding.scrollview.setObserveHeight(changeHeight);//这里需要计算下图向上的覆盖距离

                //获取控件的试图观察者,以便通过试图观察者得到控件的宽高参数
                ViewTreeObserver viewTreeObserverTop = mViewBinding.llTopRoot.getViewTreeObserver();
                //添加观察者监听
                viewTreeObserverTop.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        //回调监听后首先移除该监听 占资源
                        mViewBinding.llTopRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        //得到高度
                        llTopRootHeight = mViewBinding.llTopRoot.getHeight() - dimension;
                    }
                });

            }
        });

        //动态设置marginTop
//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mViewBinding.llZongzhi.getLayoutParams();
//        layoutParams.topMargin = -180;
//        mViewBinding.llZongzhi.setLayoutParams(layoutParams);
//
//
//        mViewBinding.scrollviewFragmy.setObserveHeight(ivTopHeight - 180);//这里需要计算下图向上的覆盖距离
//
//        LinearLayout.LayoutParams layoutParamsb = (LinearLayout.LayoutParams) mViewBinding.butFlag.getLayoutParams();
//        layoutParams.topMargin = -180;
//        mViewBinding.butFlag.setLayoutParams(layoutParams);

    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

        mViewBinding.butFlag2.setVisibility(View.INVISIBLE);
        mViewBinding.butFlag.setVisibility(View.VISIBLE);
        mViewBinding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mViewBinding.ivBack1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        // 监听高度变化 获取 对应的透明度
        mViewBinding.scrollview.setAlphaChangeListener(new ObserveAlphaScrollView.AlphaChangeListener() {
            @Override
            public void alphaChanging(float alpha, int height) {

                mViewBinding.lltop.setAlpha(alpha);

                if (alpha == 1) {
                    mViewBinding.butFlag2.setVisibility(View.VISIBLE);
                    mViewBinding.butFlag.setVisibility(View.INVISIBLE);

                    //设置黑字模式  黑图
                    BarUtils.setStatusBarLightMode(FreeActivity.this, true);
                    mViewBinding.ivShare.setImageResource(R.drawable.share_black);
                    mViewBinding.ivBack.setImageResource(R.drawable.toleftblack);
                } else {

                    //设置白字模式  白图
                    BarUtils.setStatusBarLightMode(FreeActivity.this, false);
                    mViewBinding.ivShare.setImageResource(R.drawable.share_white);
                    mViewBinding.ivBack.setImageResource(R.drawable.toleftwhite);

                    if (llTopRootHeight <= height) {
                        mViewBinding.butFlag2.setVisibility(View.INVISIBLE);
                        mViewBinding.butFlag.setVisibility(View.VISIBLE);
                    }

                    if (alpha <= llTopRootHeight/changeHeight) {
                        mViewBinding.butFlag2.setVisibility(View.INVISIBLE);
                        mViewBinding.butFlag.setVisibility(View.VISIBLE);
                    }

                }

            }


        });

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Android 滑动渐变 扩展效果 免费专区)