Android realizes Gaussian Blur effect and is compatible with lower versions

1、 Effect demonstration

Gaussian Blur effect is used in the project. I have consulted some materials. Considering the performance problem, I finally choose to use the renderscript library provided by Android. There are many similar methods to use renderscript to realize Gaussian blur on the Internet. Most of them are summarized in a mess. Here is a sorting for students with similar needs to refer to and learn.

(project renderings)

Briefly describe the implementation idea of the project renderings:

① Load the layout of the defined XML

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

③ Compress and Gaussian blur the bitmap object

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

⑤ Add the layout that has been added to the fuzzy graph object to the popuwindow and manage the pop-up method of sub entries

2、 Gaussian blur for renderscript

There are many methods to realize Gaussian Blur effect, which can be implemented in Java, NDK or the method recommended in this paper (also using JNI). As for why we choose to use renderscript to realize it, there must be some advantages.

Advantages: renderscript mode is extremely fast, about 100 times faster than Java mode and 20 times faster than NDK mode (different picture quality test results are different for reference)

Disadvantages: api17 above is effective. (however, Google has provided the method of downward compatibility, which will be introduced later in the article.)

The following is the core code using renderscript:

The annotation in this method is very clear, but it should be noted that bluescript seTradius(); Method. When setting the ambiguity, the maximum radius can only be set to 25 f. It may be that the direct processing of the picture will lead to poor blur effect, so the maximum effective value is set to 25. However, if you want to achieve a deeper blur effect, you can compress the picture first and reduce the quality of the picture to achieve a more blurred effect.

The following is the image compression processing method:

The above method is to use renderscript to realize the core code block of Gaussian blur and what should be paid attention to. However, we still need to pay attention to the compatibility problem. As mentioned above, this method is only effective when it is applicable to API 17 or above. Then the problem comes, and we need to deal with the API downward compatibility problem.

3、 Handling API downward compatibility problems and points for attention

After the Gaussian blur is realized according to the above method, run to see the effect and feel full of achievement. At this time, boss just comes with the customer, young man, to help the customer install a latest version (the customer's mobile phone system version is Android 4.0). After installation, a little........... This is so embarrassing!

When tracking bugs, some students may have the following error messages:

Exception information I:

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 2:

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 comes with librsjni.com for phones above 4.2 So and librssupport So library, while some mobile phones below 4.2 do not have these two JNI libraries. So we have to import these two JNI into our project. So where are the files?

The following is my file path: C: \ tools \ Android SDK \ build tools \ 23.0 3\renderscript\lib\packaged,renderscript-v8. The jar package is located in the renderscript \ lib directory.

That is, the four directories under build tools \ versions \ renderscript \ lib \ packaged in the Android SDK path. Note the jar package and It is best to select the latest version of so, such as 24.0 X86-64 adaptation has been added to 0.

Well, here you can be perfectly compatible with versions below 4.2. In addition, there is another most important point to note. I have been troubled by this detail for at least 2 hours. Now I think it hurts. After you finish all the above things, please note that you must change the import package path: import Android Replace renderscript with import Android support. v8. renderscript。 The details are as follows:

Last thing to note, don't forget this if code is confused in your project. OK, that's it.

4、 Attached:

In addition, we share two GitHub that deal with Gaussian blur:

https://github.com/wl9739/BlurredView https://github.com/robinxdroid/Blur

The above is all the description of Android realizing Gaussian Blur effect and compatible with low versions introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>