1

In my app I have a LinearLayout which is transparent and it is attached to WindowManager .I want to make that LinearLayout Blur so that everything behind the LinearLayout will look blurred.I had gone through various sources from that I had got how to make a ImageView blur and I had done that successfully using RenderScript Api here is the code:

 private static final float BLUR_RADIUS = 25f;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

    //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}


and finally adding it with `ImageView`

    ImageView imageView=(ImageView)findViewById(image-id);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
    Bitmap blurredBitmap = blur(bitmap);
    imageView.setImageBitmap(blurredBitmap);

but sadly it blurs ImageView only.Any solution for blurring a layout or modifiying the above code to use it with Layout.

Hi everyone I had got to know how to blur an ImageView successfully but how can I blur an transparent LinearLayout.Is there any solution for this please help me

Adarsh
  • 165
  • 2
  • 16

1 Answers1

1

I created an example project to give an example of a possible process to use (main code).

The purpose of this example is to show how RenderScript can be used to blur a generic view. What the example does (when the button "Blur it!" gets clicked) is:

1) Blurs the chosen view

The chosen view, for this example, must be contained inside a bigger container (ex. a LinearLayout).

The blur process:

  • Gets a screenshot of the original view.

    Bitmap getViewScreenshot(View v) {
        v.setDrawingCacheEnabled(true);
        Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false);
    
        return b;
    }
    
  • Instantiates all RenderScript allocations (one for input and one for blur output).

    allocOriginalScreenshot = Allocation.createFromBitmap(mRS, viewScreenshot);
    // Creates an allocation where to store the blur results
    allocBlurred = Allocation.createTyped(mRS, allocOriginalScreenshot.getType(), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
    
  • Creates a TextureView to display the blur result.
  • Substitutes the new TextureView with the original view. In this process, the new view gets saved inside the "tag" field of the original view, so that later the original view can be substituted back with the new TextureView.

    void replaceView(View originalView, View newView) {
        originalView.setTag(newView);
    
        newView.setLayoutParams(new FrameLayout.LayoutParams(originalView.getLayoutParams()));
    
        ViewGroup parent = (ViewGroup) originalView.getParent();
        int index = parent.indexOfChild(originalView);
        parent.removeView(originalView);
    
        parent.addView(newView, index);
    }   
    
  • Blurs the screenshot using the ScriptIntrinsicBlur class.

    void executeBlur() {
        Log.d(TAG, "Executing blur");
    
        scriptIntrinsicBlur.setInput(allocOriginalScreenshot);
        scriptIntrinsicBlur.forEach(allocBlurred);
    
        allocBlurred.ioSend();
    }
    

2) Display a simple dialog

3) Unblur the original view

The unblur process removes the TextureView from the layout and restores the original View.

Reference: RenderScript: parallel computing on Android, the easy way, Dali library

cmaster11
  • 527
  • 3
  • 6
  • thanks a lot for your help but I have heard that using RenderScript may cause memory issue.And sometime it will lag on low end memory device.Is it true? – Adarsh Aug 11 '16 at 02:21
  • You can get memory issues if you create Allocations and you don't destroy them, but it is not going to happen with this method :) – cmaster11 Aug 11 '16 at 06:36
  • Also, the performance is OK even on low end devices, still better than anything else on Android! – cmaster11 Aug 11 '16 at 06:37
  • OK then it will work fine even in low end memory device:) – Adarsh Aug 11 '16 at 08:40
  • Let's say that RenderScript parallelizes every job in any case, on any device. This means that it performs always better than single core processes. – cmaster11 Aug 11 '16 at 08:43
  • hi i have tried your code I am implementing a layout with an background image in it when I click on blur button it gives me error like this: `Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 7641 (ple.adarsh.test)` what does the error mean i can't understand – Adarsh Aug 11 '16 at 13:32
  • This would require some more info. Are you using my project, or you did create one on your own, first of all? Secondly, where are you testing this code? What device/emulator and which Android version? – cmaster11 Aug 11 '16 at 13:36
  • yes i am using your code as it is on MainActivity.I had implemented a small linear layout inside my mainActivity so that I can blur that linear layout only.I am testing my app on Moto e device which is running on lollipop 5.1 – Adarsh Aug 11 '16 at 13:54
  • Can you copy inside a gist or [http://pastebin.com/](http://pastebin.com/) your current code, both of MainActivity.java and the layout.xml files? – cmaster11 Aug 11 '16 at 13:55
  • Can you please join me at [http://www.disposablechat.com/chat/RenderScript+discussion?password=](http://www.disposablechat.com/chat/RenderScript+discussion?password=) – cmaster11 Aug 11 '16 at 14:08
  • hi,I have tried your code to blur a layout but I'm getting null pointer exception please see my new question http://stackoverflow.com/questions/38939811/error-while-trying-to-blur-relativelayout-in-android/38939970?noredirect=1#comment65235328_38939970 – Adarsh Aug 14 '16 at 07:32