代码控制toolbar透明效果

gif展示

需求

全面屏效果,透明状态栏和toolbar.滑动后toolbar出现,状态栏变色.
其实最简单的方式可以使用coordinatorlayout,我想用代码实现所以...说代码吧:

布局准备

备注:LivePosterIv是一个自定义的imageview,依照你自己的需求替换就行




    

        

            


                

                    

                    

                        

                        

                            

                            

                            

                            

                            
                        
                        
                        


                    


                


                


                    

                    

                    
                

            
        
    

    

        

            

        

        

            

            
            
        


    


代码实现

1.初始化
   @Override
   protected void setContentView() {
       setContentView(R.layout.activity_label);
       x.view().inject(this);
       int statusBarHeight = getStatusBarHeight(this);//工具类内方法
       titleGuanzhu.getBackground().mutate().setAlpha(0);//不想显示的都设置透明
       titleGuanzhu_yes.getBackground().mutate().setAlpha(0);
       back_text.setTextColor(Color.argb((int) 0, 0, 0, 0));  //文字透明度((int) (alpha * 255));
       toolbar.getBackground().mutate().setAlpha(0);
       RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 84);
       layoutParams.setMargins(0, statusBarHeight, 0, 0);
       toolbar.setLayoutParams(layoutParams);
   }
提供一个状态栏工具类,可以写在baseActivity内
public class StatusBarUtils {

    public static void setStatusBarColor(Activity activity , int color){
        Window window = activity.getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            //设置Flags  适配手机
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);
        }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            //首先先设置全屏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //获取decorView
            ViewGroup decorView = (ViewGroup) window.getDecorView();
            //获取contentView
            View statusBar = decorView.findViewById(android.R.id.content);

            //创建View
            View view = new View(activity);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity)));
            statusBar.setBackgroundColor(color);
            decorView.addView(statusBar);

            ViewGroup activityView = (ViewGroup) activity.findViewById(android.R.id.content);
            activityView.getChildAt(0).setFitsSystemWindows(true);
        }
    }

    public static void setActivityTranslucent(Activity activity){
        Window window = activity.getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            //获取decorView
            ViewGroup decorView = (ViewGroup) window.getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);


        }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            //设置全屏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    public static void closeActivityTranslucent(Activity activity){
        Window window = activity.getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            //设置Flags  适配手机
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.BLACK);

        }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            //首先先设置全屏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //获取decorView
            ViewGroup decorView = (ViewGroup) window.getDecorView();
            //获取contentView
            View statusBar = decorView.findViewById(android.R.id.content);

            //创建View
            View view = new View(activity);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity)));
            statusBar.setBackgroundColor(Color.BLACK);
            decorView.addView(statusBar);

            ViewGroup activityView = (ViewGroup) activity.findViewById(android.R.id.content);
            activityView.getChildAt(0).setFitsSystemWindows(true);
        }
    }
    //获取状态栏高度
    public static int getStatusBarHeight(Activity activity) {
        // 插件式换肤:怎么获取资源的,先获取资源id,根据id获取资源
        Resources resources = activity.getResources();
        int statusBarHeightId = resources.getIdentifier("status_bar_height","dimen","android");
        return resources.getDimensionPixelOffset(statusBarHeightId);
    }
}

2.根据scrollerview滚动改变toolbar样式,card是那张大大的背景图,我要获取他的高度所以要用post方法.

        card.post(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void run() {
                height = card.getMeasuredHeight();
                scrollview.setScrollViewListener(new MyScrollview2.ScrollViewListener() {
                    @Override
                    public void onScrollChanged(MyScrollview2 scrollView, int l, int t, int oldl, int oldt) {
                        int mTransEndY = height;
                        int mTransViewHeight = toolbar.getMeasuredHeight();
                        if (t > height / 2) {
                            back_text.setAlpha(1);
                            back_text.setTextColor(Color.argb((int) (1 * 255), 0, 0, 0));  //文字透明度((int) (alpha * 255));
                            mBackImage.setImageResource(R.drawable.bigback);
                            titleGuanzhu.getBackground().mutate().setAlpha((int) (1 * 255));
                            titleGuanzhu_yes.getBackground().mutate().setAlpha((int) (1 * 255));
                            toolbar.getBackground().mutate().setAlpha((int) (1 * 255));
                            //不设置背景色就是透明的了
                            setStatusBarColor(Color.argb((int) (1 * 255), 255, 255, 255));
                            StatusBarUtil.StatusBarLightMode(LabelActivity.this);
                        }
                        if (t < height / 2) {
                            setStatusBarColor(Color.argb((int) (0 * 255), 0, 0, 0));
                            back_text.setAlpha(0);
                            back_text.setTextColor(Color.argb((int) (0 * 255), 0, 0, 0));  //文字透明度((int) (alpha * 255));
                            mBackImage.setImageResource(R.drawable.back_white);
                            titleGuanzhu.getBackground().mutate().setAlpha((int) (0 * 255));
                            titleGuanzhu_yes.getBackground().mutate().setAlpha((int) (0 * 255));
                            toolbar.getBackground().mutate().setAlpha((int) (0 * 255));
                            StatusBarUtil.transparencyBar(LabelActivity.this);
//                          m_right_tv.setTextColor(Color.WHITE;
                        }
                        if (mTransEndY == 0) {
                            return;
                        }
                        float alpha = (float) t / (mTransEndY - mTransViewHeight);
                        if (alpha <= 0) {
                            alpha = 0;
                        }
                        if (alpha > 1) {
                            alpha = 1;
                        }
                        
                    }
                });
            }
        });

你可能感兴趣的:(代码控制toolbar透明效果)