Transparent blurry view which blurs layout underneath

后端 未结 5 505
[愿得一人]
[愿得一人] 2020-11-30 18:27

I\'ve got a Linearlayout which I\'ve made transparent, and now I\'m looking for a way to give it a Blur effect, so what\'s ever underneath it gets blurry. Just like the Wind

5条回答
  •  情歌与酒
    2020-11-30 19:26

    step 1. cut the part of background image in bitmap which is to be blured.

    step 2. Blur that part of bitmap.

    step 3. set bitmap as a background.

    java method

    private void applyBlur(final View image, final View layout) {
    image.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                image.getViewTreeObserver().removeOnPreDrawListener(this);
                image.buildDrawingCache();
    
                Bitmap bmp = image.getDrawingCache();
    
                Bitmap overlay = Bitmap.createBitmap((int) (layout.getMeasuredWidth()),
                        (int) (layout.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
    
                Canvas canvas = new Canvas(overlay);
    
                canvas.translate(-layout.getLeft(), -layout.getTop());
                canvas.drawBitmap(bmp, 0, 0, null);
    
                RenderScript rs = RenderScript.create(getActivity());
    
                Allocation overlayAlloc = Allocation.createFromBitmap(
                        rs, overlay);
    
                ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
                        rs, overlayAlloc.getElement());
    
                blur.setInput(overlayAlloc);
    
                blur.setRadius(20);
    
                blur.forEach(overlayAlloc);
    
                overlayAlloc.copyTo(overlay);
    
                layout.setBackground(new BitmapDrawable(
                        getResources(), overlay));
    
                rs.destroy();
                return true;
            }
        });
    }
    

    ...

    now call this function:

    calling method

    applyBlur(detail_main_image, titleLayout);

    ...

    //where detail_main_image is image on which i have to show blur part //and titleLayout is view on which i have to set background blur.

提交回复
热议问题