Darken/Dim/Blur Activity when navigation drawer is open

风格不统一 提交于 2019-12-23 15:55:32

问题


I am using LDrawer in my project. I need to make the activity which is hosting the navigation drawer darken/dim/blur when the navigation drawer is opened.

I have gone through similar questions on Stackoverflow and I have not found a satisfiying answer.

Is there any simple trick to make the activity's layout darken/dim/blur when I open my NavigationDrawer

I have the RelativeLayout of my activity defined as

RelativeLayout myActivity = (RelativeLayout) findViewById(R.id.myActivity)

I have the toggle code for NavigationDrawer below

public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
               //I want to manipulate myActivity's Layout here
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }
        return super.onOptionsItemSelected(item);
    }

回答1:


Is there any simple trick to make the activity's layout Darken/Dim/Blur when I open my NavigationDrawer ?

Yes, you can simply change the background image of your activity as Blur when you open the drawer.

Other way is change the alpha or your activity simply by setalpha() and pass the value how much change you want.

If you have list control on your activity and you want to make that blur then below code will work.

Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;

Bitmap blurBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.invitation_blur_bg);

        Rect rect = new Rect(0, 0, blurBitmap.getWidth(), blurBitmap.getHeight());

Implement the OnScrollListener on your activity or fragment where you want to apply this.

@Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {

        for (int i = 0; i < listview.getChildCount(); i++) {
            View c = listview.getChildAt(i);

            Rect r = new Rect(); 
            c.getHitRect(r);

            RelativeLayout llItemBg = ((RelativeLayout ) c.findViewById(R.id.root_layout of list item);

            Drawable d = new BitmapDrawable(getResources(), cropImage(r,c));

            itemBackground.setBackgroundDrawable(d);

        }
    }
    private Bitmap cropImage(Rect r, View c2) {

        Bitmap bmOverlay = Bitmap.createBitmap(screenWidth - 60,
                c2.getHeight(), Bitmap.Config.ARGB_8888);
        Rect rect1=new Rect(0, 0, bmOverlay.getWidth(), bmOverlay.getHeight());
        Paint p = new Paint();
        Canvas c = new Canvas(bmOverlay);
        rect.right = r.right;
        rect.top = rect.top;
        rect.bottom = r.bottom / 2;

        c.drawBitmap(blurBitmap, r, rect1, p);

        return bmOverlay;
    }


来源:https://stackoverflow.com/questions/30339888/darken-dim-blur-activity-when-navigation-drawer-is-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!