The Android API is Gaussian Blur and is compatible with low versions

Source: Internet
Author: User

First, using the Android API to achieve Gaussian blurThere are a lot of good articles about the Gaussian blur online, which is not repeated here. First look at an effect


The effect has been seen, just to see how to achieve it. The first is the core code that implements the Gaussian blur
<span style= "White-space:pre" ></span>public static Bitmap Blurbitmap (Bitmap Bitmap, Context context) {// Create an empty Bitmapbitmap Outbitmap = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight () with the need to create a Gaussian blur Bitmap, config.argb_8888);//Initialize Renderscript, this class provides renderscript context, you must first create this class before creating other RS classes, he controls the initialization of Renderscript, resource management, Release Renderscript rs = renderscript.create (context);//Create Gaussian Blur object Scriptintrinsicblur Blurscript = Scriptintrinsicblur.create (RS, Element.u8_4 (RS));//Create allocations, this class is the primary method of passing data to the Renderscript kernel, and develop a backing type to store the given type Allocation Allin = Allocation.createfrombitmap (rs, bitmap); Allocation Allout = Allocation.createfrombitmap (RS, outbitmap);//Set fuzziness Blurscript.setradius (25.F);//Perform the Renderscriptblurscript.setinput (Allin); Blurscript.foreach (allout);//Copy The final bitmap created by the Allocation to the Outbitmapallout.copyto (OUTBITMAP);//Recycle the original bitmapbitmap.recycle ();//After finishing Everything, we destroy the Renderscript.rs.destroy (); return OUTBITMap;} 
Then the call part
<span style= "White-space:pre" ></span>public static Bitmap Getblurbitmap (View Rootview, context context) { try {if (Rootview = = NULL | | context = = NULL) {return null;} Rootview.setdrawingcacheenabled (TRUE); Bitmap Drawingcache = Rootview.getdrawingcache (); Bitmap Bgbitmap = Bitmap.createbitmap (Drawingcache); return Bitmaputil.blurbitmap (Bgbitmap, context);} catch (Exception e) {e.printstacktrace ();} return null;}
This allows the Gaussian blur effect to be achieved, and then the blur effect is displayed on the interface. Second, compatible with the low versionAndroid has a very troublesome thing is to need a compatible version of a lot, according to the way above, in 4.2 and above the version can be easily out of effect, but in the lower version will be abnormal: java.lang.NoClassDefFoundError: Android.renderscript.ScriptIntrinsicBlur. This problem is caused by the fact that the API is not backwards compatible, and now we are going to solve it. In fact, Google has already provided a solution, we just need to use it. 1, introduced Renderscript-v8.jar (Put the jar package in the Libs directory, add to build path, generally only need to introduce) if only the jar package, the project will not error, but the operation will be problematic:android.support.v8.renderscript.RSRuntimeException:Error loading RS JNI library:java.lang.UnsatisfiedLinkError:Couldn ' t load rssupport:findlibrary returned null; As you can see from the error, the JNI library is not found2, in the Libs directory to build three directories: ARMEABI-V7A, MIPS, x86, and then put librsjni.so and librssupport.so respectively in the corresponding directory can beNote: These resources are included in the SDK, mine is: Sdk\build-tools\android-4.4.2\renderscript, you can find your own directory here can be perfectly compatible with the version of the 4.2 below. Third, the description1, because the company has been using eclipse, so this solution is also in Eclipse above, Android studio should be more simple than this, for specific reference: Click to open the link2, online There are many ways to achieve the Gaussian blur, I personally tried some, but the effect is not scriptintrinsicblur, because Google'sRenderscript has a very efficient graphics ability, about Renderscript want to learn more about the students please Google

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Android API is Gaussian Blur and is compatible with low versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.