Android quickly achieves dynamic blur effects

Source: Internet
Author: User

Write in front

Now, more and more apps are using the blur effect, which is called Gaussian Blur. As we all know, fuzzy rendering on the Android platform is a fairly CPU-intensive operation, which is unavoidable once it is handled poorly. In general, given the efficiency, the best way to render a picture is to use OpenGL, followed by the use of C++/C, where Java code is the least efficient and the slowest. But after Android launched Renderscript, we had a choice, and the tests showed that the rendering efficiency of using rederscript was comparable to that of using c++/c, but using renderscript was much simpler than using JNI! At the same time, the Android team provides a library of renderscript support that can be used on low-version Android platforms.

However, before using renderscript, for blurring a picture, it should be noted that we should try not to use the original size resolution of the picture, it is best to reduce the proportion of the picture, this small rendering efficiency is higher, and faster.

What is Renderscript

Renderscript is a low-level, high-performance programming language for 3D rendering and processing intensive computing (3D playback, and CPU-intensive computing). Android has consistently fared poorly in drawing performance and has improved since the introduction of the NDK, while the Renderscript has been released in Honeycomb after the framework, greatly increasing the execution and computing power of the Android native language. Now the online introduction of Renderscript's article is very few, with a blog, you can better understand the language.

a detailed description of the Android Renderscript and some useful documentationIf you need more information, you can view the official documentation RenderscriptThe realization of dynamic blurbefore using, make the following definition in Module build.gradle :
Mainactivity.java
Package Com.jackie.blurimage;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.widget.imageview;import Android.widget.seekbar;import Android.widget.textview;public class Mainactivity extends Appcompatactivity {private Im    Ageview Mblurimage, Moriginimage;    Private SeekBar Mseekbar;    Private TextView mseekprogress;    private int malpha;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initview ();        InitData ();    Initevent ();        } private void Initview () {mblurimage = (ImageView) Findviewbyid (r.id.blur_image);        Moriginimage = (ImageView) Findviewbyid (r.id.origin_image);        Mseekbar = (SeekBar) Findviewbyid (R.id.seek_bar);    Mseekprogress = (TextView) Findviewbyid (r.id.seek_progress); } private void InitData () {//Get pictures Bitmap Originbitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.blur);        Bitmap Blurbitmap = Blurutils.blur (this, originbitmap);        Fill the blurred image and the original Mblurimage.setimagebitmap (BLURBITMAP);    Moriginimage.setimagebitmap (ORIGINBITMAP);        } private void Initevent () {Mseekbar.setmax (100); Mseekbar.setonseekbarchangelistener (New Seekbar.onseekbarchangelistener () {@Override public void on                ProgressChanged (SeekBar SeekBar, int progress, Boolean fromuser) {malpha = progress;                Moriginimage.setalpha ((int) (255-malpha * 2.55));            Mseekprogress.settext (string.valueof (Malpha));            } @Override public void Onstarttrackingtouch (SeekBar SeekBar) {} @Override    public void Onstoptrackingtouch (SeekBar SeekBar) {}}); }}
activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "&    Gt <framelayout android:layout_width= "Match_parent" android:layout_weight= "1" android:layout_height= "            0DP "> <imageview android:id=" @+id/blur_image "android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:scaletype= "Centercrop" android:src= "@drawable/blu            R "/> <imageview android:id=" @+id/origin_image "android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:scaletype= "Centercrop"/> </FrameLayout> < LinearLayout android:layout_width= "match_parent" android:layout_height= "80DP" android:orientation= "ve Rtical "> <seEkbar android:id= "@+id/seek_bar" android:layout_width= "Match_parent" Android:layout_heigh t= "Wrap_content" android:layout_marginleft= "16DP" android:layout_marginright= "16DP" Andro            id:layout_margintop= "@dimen/activity_vertical_margin"/> <textview android:id= "@+id/seek_progress" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_g ravity= "center" android:text= "0" android:textsize= "24sp"/> </linearlayout></linearlay Out>

from the code above you can see , two pictures are placed on the framelayout, and the transparency of the image is changed dynamically to achieve the dynamic blur effect.
blurutils.java

Package Com.jackie.blurimage;import Android.content.context;import Android.graphics.bitmap;import Android.renderscript.allocation;import Android.renderscript.element;import Android.renderscript.RenderScript; Import android.renderscript.scriptintrinsicblur;/** * Created by Jackie on 2017/1/21.    * Gaussian Blur tool class */public class Blurutils {/** * Picture scale */private static final float scale_degree = 0.4f;    /** * Maximum blur (between 0.0 and 25.0) */private static final float Blur_radius = 25f; /** * Blur Picture * @param context context * @param bitmap need blurred picture * @return obfuscated picture */Public Static Bitmap Blur (Context context,bitmap Bitmap) {//Calculate picture shrink length int width = Math.Round (bitmap.getwidth () * S        Cale_degree);        int height = Math.Round (bitmap.getheight () * scale_degree);        The reduced image as pre-rendered picture Bitmap Inputbitmap = Bitmap.createscaledbitmap (Bitmap, width, height, false); Create a rendered input picture Bitmap Outputbitmap = Bitmap.createbiTMap (INPUTBITMAP);        Create Renderscript Kernel object Renderscript renderscript = renderscript.create (context); Creates a blur effect of the Renderscript tool object Scriptintrinsicblur Scriptintrinsicblur = Scriptintrinsicblur.create (RenderScript, Ele ment.        U8_4 (Renderscript));         /** * Because Renderscript does not use VMS to allocate memory, you need to use the allocation class to create and allocate memory space.         * When creating allocation objects, the memory is empty, and the data needs to be populated with CopyTo ().        */Allocation Inputallocation = Allocation.createfrombitmap (Renderscript, Inputbitmap);        Allocation outputallocation = Allocation.createfrombitmap (Renderscript, Outputbitmap);        Set the blur degree of rendering, 25f is the maximum fuzziness Scriptintrinsicblur.setradius (Blur_radius);        Sets the input memory Scriptintrinsicblur.setinput (inputallocation) of the Scriptintrinsicblur object;        Save the Scriptintrinsicblur output data to the output memory Scriptintrinsicblur.foreach (outputallocation);        Populate the data into allocation Outputallocation.copyto (OUTPUTBITMAP);    return outputbitmap; }}

As below, sister paper one!







Android quickly achieves dynamic blur effects

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.