Over a million developers have joined DZone.

How To Create a Bitmap Blur Effect In Android Using RenderScript

Learn how to achieve the popular blur effect on your Android apps

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Android RenderScript framework API can be used for performing computationally intensive tasks at high performance. RenderScript was primarily developed to use with data-parallel computation, although serial computationally intensive workloads can benefit as well. The RenderScript runtime will parallelize work across all processors available on a device, such as multi-core CPUs, GPUs, or DSPs, allowing you to focus on expressing algorithms rather than scheduling work or load balancing.

RenderScript is especially useful for applications performing image processing, computational photography, or computer vision. There are two ways we can access the Android RenderScript framework APIs:

  • Directly using android.renderscript API classes. These classes are available from Android 3.0 (API level 11) and higher.
  • Alternatively, you can use android.support.v8.renderscript support package classes. The support library classes are available for devices running Android 2.2 (API level 8) and higher.

How to use RenderScript

In order to use the Support Library RenderScript APIs, you must have Android SDK Tools revision 22.2 or higher and SDK Build-tools revision 18.1.0 or higher.

After you have the above two minimum development tools, you need to update the settings for the Android build process to include the RenderScript APIs. In Android Studio project add the following configurations to project build.gradlefile.

defaultConfig {
    applicationId "com.javatechig"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

    // Add the following two lines
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true

The following code snippets can be used create a bitmap blur effect in Android using RenderScript API.

//Set the radius of the Blur. Supported range 0 < radius <= 25
private static final float BLUR_RADIUS = 25f;

public Bitmap blur(Bitmap image) {
    if (null == image) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

    //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    return outputBitmap;

You can use the above code snippet to blur an ImageView as follows.

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
Bitmap blurredBitmap = blur(bitmap);


Bitmap Blur Effect In Android Using RenderScript

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Nilanchala Panigrahy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}