Android Blur View (Blur background behind the view)

后端 未结 4 590
暖寄归人
暖寄归人 2020-12-24 15:10

I am trying to make the bottom part of an Image blur for the view on top it like in the image.

I tried blurring it using Rendenscript but I am not able to blur only

4条回答
  •  一生所求
    2020-12-24 16:03

    Try applyBlur method:

    private void applyBlur(ImageView image) {
        image.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                image.getViewTreeObserver().removeOnPreDrawListener(this);
                image.buildDrawingCache();
    
                Bitmap bmp = image.getDrawingCache();
                blur(bmp, text);
                return true;
            }
        });
    }
    
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void blur(Bitmap bkg, View view) {
        long startMs = System.currentTimeMillis();
    
        float radius = 20;
    
        Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()),
                (int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(overlay);
    
        canvas.translate(-view.getLeft(), -view.getTop());
        canvas.drawBitmap(bkg, 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(radius);
    
        blur.forEach(overlayAlloc);
    
        overlayAlloc.copyTo(overlay);
    
        view.setBackground(new BitmapDrawable(
                getResources(), overlay));
    
        rs.destroy();
    }
    

提交回复
热议问题