模糊的效果处理可以有几种方式:Java、jni、Android的RenderScript API,Java代码的一种比较快的一种模糊算法实现fastBlur效果不太理想,还是比RenderScript慢些,所以最终采用Android官方的API实现RenderScript support v8的支持包,Android support v8支持包是在...\build-tools\21.1.2\renderscriptSDK的这里的,在Android Studio中使用support v8:在build.gradle的defaultConfig中加上下面两句即可,
//Let's create an empty bitmap with the same size of the bitmap we want to blur Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//Instantiate a new Renderscript RenderScript rs = RenderScript.create(applicationContext);
//Create an Intrinsic Blur Script using the Renderscript ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps Allocation allIn = Allocation.createFromBitmap(rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); //Set the radius of the blur 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();