Android for Gaussian Blur effect and low version compatibility

Source: Internet
Author: User

Android for Gaussian Blur effect and low version compatibility


Hello, long time no see, long time No blog, summed up the following reasons have three, first, become lazy, second, become lazy, third, or become lazy, because any don't update blog reason is an excuse!


first, the effect demonstration

The project uses the Gaussian blur effect, consulted some data, considering the performance problem is finally choose to use the Android Renderscript Library to implement, about using Renderscript to achieve Gaussian blur online there are many similar methods, most of the summary of the relatively chaotic, Here is to do a tidy bar, for similar needs of students to reference and study.




( project )

Simple description of project implementation ideas :

① loading the layout of the defined XML

② using the screenshot method to get the bitmap object of the current window

③ compressing and Gaussian blurring of bitmap objects

④ the processed Blur graph object as the background of the layout loaded in the ①

⑤ adds a layout that has been added to the Blur graph object to Popuwindow and handles the popup of the child entry


second, the application of renderscript to achieve Gaussian blur

There are many ways to achieve the Gaussian blur effect, can be implemented in Java, you can use the NDK to achieve, you can use the method recommended in this article (also using the method of JNI), as for why choose to use renderscript way to achieve, there must be some advantages.

Advantages: Renderscript way, very fast, about 100 times times the speed of Java mode, NDK mode 20 times times the speed (different picture quality test results are different, for reference)

Disadvantages: API17 above is valid. (but Google has provided a backwards-compatible approach, which is described later in this article)

Here is the core code that uses the Renderscript method:

/************************* Gaussian Blur processing * @param bitmap* @param context* @return ***********************/public static bitmap Blurbitmap (Bitmap Bitmap, Context context) {//Create an empty Bitmap Bitmap outbitmap with the need to create a gaussian blur Bitmap = Bitmap.create        Bitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Initializes the Renderscript, which provides renderscript context, which must be created before creating other RS classes, which control the initialization of Renderscript, resource management and release Renderscript rs = Render        Script.create (context);        Create a Gaussian blur object Scriptintrinsicblur Blurscript = Scriptintrinsicblur.create (RS, Element.u8_4 (RS)); Create allocations, which is the primary way to pass data to the Renderscript kernel and develop a backing type to store the given type Allocation Allin = Allocation.createfrombitmap (RS, bi        TMAP);        Allocation Allout = Allocation.createfrombitmap (RS, outbitmap);        Set the degree of ambiguity (note: Radius can only be set to 25.F max) Blurscript.setradius (15.F);        Perform the Renderscript blurscript.setinput (Allin);        Blurscript.foreach (Allout); Copy the final bitmap created by theAllocation to the Outbitmap Allout.copyto (OUTBITMAP);        Recycle the original bitmap bitmap.recycle ();        After finishing everything, we destroy the Renderscript.        Rs.destroy ();    return outbitmap; }

The comments in this method are described clearly, but it is important to note that Blurscript.setradius (); method, this method sets the degree of ambiguity radius maximum can only set 25.f, may be the image directly processing will lead to fuzzy effect is not good, so the value of the maximum effective setting of 25, but if you want to achieve a deeper blur effect, you can first compress the picture, Reduce the quality of the picture to achieve a more blurred effect .

Below is the image compression processing method:

/** * Compress Image by Pixel, this would modify image width/height.     * * @param imgpath Image path * @param pixelw target pixel of width * @param pixelh target pixel of height  * @return */public static Bitmap ratio (String imgpath, float pixelw, float pixelh) {bitmapfactory.options        newopts = new Bitmapfactory.options ();        Start to read the picture, at this time set the Options.injustdecodebounds back to true, that is, read-only side unread content newopts.injustdecodebounds = true;        Newopts.inpreferredconfig = config.rgb_565;        Get Bitmap info, but notice this bitmap is null now bitmap bitmap = Bitmapfactory.decodefile (imgpath,newopts);        Newopts.injustdecodebounds = false;        int w = newopts.outwidth;        int h = newopts.outheight;    float ww = PIXELW;    Set the width to 120f, you can obviously see the picture reduced by float hh = pixelh; When you set the height to 240f, you can see that the picture shrinks//zoom ratio, because it is fixed scale, only one of the data is high or wide calculation can be int is = 1;//means not scaling if (W > H &&amp ; W > WW) {//If the width is large, the size is fixed according to the widthPut be = (int) (NEWOPTS.OUTWIDTH/WW);        } else if (W < h && h > hh) {//If height is high, scale is = (int) (NEWOPTS.OUTHEIGHT/HH) According to the fixed width size;        } if (be <= 0) is = 1; Newopts.insamplesize = be;//Set scaling//start compressing the picture, notice that the options.injustdecodebounds is set back to false bitmap = Bitmapfacto        Ry.decodefile (Imgpath, newopts); Compress a good proportion of the size of the mass compression//return compress (bitmap, maxSize);    Here again the significance of quality compression, instead of consuming resources, delete return bitmap; }
The above method is to useRenderscript to realize the core code block of Gaussian blur and the place to be noticed. However, there is still a need to pay attention to compatibility issues, the above mentioned that the method only applies to API17 above the effective, then the problem comes, need to deal with the API backward compatibility problem. third, deal with the API backward compatibility problems and points of attention

In accordance with the above method to achieve Gaussian blur, run a look at the effect, full of achievement, at this time, the boss just bring customers over, lad, to help customers install a new version (Customer mobile phone system version for Android4.0), loaded after a bit ... This special is embarrassing ...!!!!!!

When tracking a bug, some students may receive the following error message:

Exception Information One:

09-21 15:07:34.417:e/androidruntime (4476): Android.support.v8.renderscript.RSRuntimeException:Error loading RS JNI Library:java.lang.UnsatisfiedLinkError:Couldn ' t load rssupport:findlibrary returned null ..........

Exception Information Two:

Java.lang.NoClassDefFoundError:android.renderscript.ScriptIntrinsicBlur ..... .........

Solution:

the error message is android.support.v8.renderscript.RSRuntimeException:Error loading and  Java.lang.UnsatisfiedLinkError:Couldn ' t load rssupport from loader Dalvik.system.PathClassLoader is due to a phone with more than 4.2 librsjni.so and librssupport.so libraries, while some phones below 4.2 do not have these two JNI libraries. So we have to import these two jni into our project. So where is the file?

here is my file path:C:\Tools\android-sdk\build-tools\23.0.3\renderscript\lib\packaged , The Renderscript-v8.jar package is located under the Renderscript\lib directory .

That is: Under the Android SDK path build-tools\ the four directories under each version \renderscript\lib\packaged, It is important to note that the selection of jar packages and. So versions is best chosen up-to-date, such as the addition of x86-64 in 24.0.0.


Well, it will be perfectly compatible with the version below 4.2.  In addition, there is one of the most important points of attention, I have been troubled by this detail for at least 2 hours, and now think of the egg pain, after you have done everything above, please be aware of the import package path must be replaced:Import Replace the android.renderscript with the import android.support.v8.renderscript. Specific as follows:

Import Android.support.v8.renderscript.allocation;import Android.support.v8.renderscript.element;import Android.support.v8.renderscript.renderscript;import Android.support.v8.renderscript.ScriptIntrinsicBlur;
the most important point to note is that if the code in the project adds a confuse, don't forget this. OK, here we go.


Iv.. Attached:

Also share two of GitHub dealing with Gaussian blur:

Https://github.com/wl9739/BlurredView

Https://github.com/robinxdroid/Blur


"Reprint Annotated Gao_chun blog:http://blog.csdn.net/gao_chun/article/details/52193511"


Android for Gaussian Blur effect and low version compatibility

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.