preface

I’ve used a lot of Gaussian blur, but it’s still best to use it this way. My current approach is to use RenderScript-v8.jar for Gaussian blur, which, combined with zooming and opacity changes, is very efficient at achieving all levels of Gaussian blur.

The effect




Gaussian. GIF

implementation

The idea of gaussian blur in the example is to first place the gaussian blur image, make two ImageViews, the top layer is the original image that can change the transparency, and the bottom layer is the image after gaussian blur. The image to be blurred is then captured from the view, scaled during the capture, then gaussian blurred the scaled image, and then added to the underlying ImageView. Only two core methods are posted below, and specific examples can be viewed

/** public bitmap blurBitmap(bitmap, blurBitmap, blurBitmap) Context context,float radius) { if(radius<1){ radius=1; }else if(radius>25){ radius=25; OutBitmap = bitmap.createBitMap (bitmap.getwidth (), bitmap.getheight (), bitmap.getheight (), Bitmap.Config.ARGB_8888); // The class that provides the Renderscript context must be created before any other RS class can be created. This class controls Renderscript initialization, resource management, RenderScript rs = renderscript. create(context); / / create a gaussian blur object ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur. Create (rs, Element U8_4 (rs)); // Create Allocations, whose class is the main way of passing data to the RenderScript kernel, / / make a back-up type to store a given type Allocation allIn = Allocation. CreateFromBitmap (rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); // Set the blurscript. setRadius(radius); // Perform the Renderscript blurScript.setInput(allIn); blurScript.forEach(allOut); // Copy the final bitmap created by the out Allocation to the outBitmap allOut.copyTo(outBitmap); // recycle the original bitmap bitmap.recycle(); // After finishing everything, we destroy the Renderscript. rs.destroy(); return outBitmap; } /** * Public bitmap getBitmapFromView(view v,int scaleFactor) { if (v == null) { return null; } Bitmap screenshot; screenshot = Bitmap.createBitmap(v.getWidth()/scaleFactor, v.getHeight()/scaleFactor, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(screenshot); c.translate(-v.getLeft() / scaleFactor, -v.getTop() / scaleFactor); c.scale(1f / scaleFactor, 1f / scaleFactor); v.draw(c); return screenshot; }Copy the code

Import the renderscript – v8. Jar

First go to the directory \build-tools\25.0.0\renderscript\lib\ of the AndroidSDK (I’m using version 25.0.0 here, but it could be any other version), which contains renderscript-v8.jar, as shown in the figure below




SDK lib directory. PNG

Then add it to the project local lib directory. Then go to the build-tools\25.0.0\renderscript\lib\packagedtools\25.0.0\renderscript\lib\packaged\ as shown in the figure




SDK so file directory. PNG

We just need to copy the armeabi-v7a directory into the project, and if we need to debug on the emulator, we need to copy the x86 directory into the project. As shown in figure




Libs directory. PNG

Build again and start running. If you can’t find the. So file, don’t worry. Add the following code to Android for Gradle

sourceSets {
        main {
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
    }Copy the code

The last

There are inappropriate or incorrect places in the article, please clearly point out, I will improve as soon as possible, so as not to mislead others.