Visit Sponsor

Written by 12:00 pm Android

Android Bitmap Blur Effect – RenderScript & Alternatives

In Android development, applying a blur effect to a bitmap is a common UI requirement—whether for background images, dialogs, or photo filters. Historically, Android’s RenderScript API delivered high-performance Gaussian blur processing on bitmaps, but with newer Android versions, alternatives such as RenderEffect and third-party libraries have become more relevant.

In this guide, you’ll learn how to create a bitmap blur effect, how to set up your project, and what modern approaches are available.

What Is a Bitmap Blur Effect?

A bitmap blur effect softens the visual detail of an image. A gaussian blur algorithm calculates new pixel values based on surrounding pixel contributions, creating a smooth, “out-of-focus” look. RenderScript’s intrinsic blur is hardware-accelerated relative to pure Java implementations, meaning better performance for large images.

RenderScript for Bitmap Blur

📌 Note: RenderScript and its intrinsic blur API (ScriptIntrinsicBlur) were added in API level 17, but the API is deprecated as of Android API level 31. Google now recommends alternatives like RenderEffect for modern blurring.

Project Setup for RenderScript

Add the following inside your app module build.gradle:

android {
    defaultConfig {
        // Required for RenderScript support
        renderscriptTargetApi 18
        renderscriptSupportModeEnabled true
    }
}

This enables RenderScript support and ensures backward compatibility on older devices.

Blur a Bitmap Using RenderScript

This example shows how to blur a bitmap using RenderScript’s intrinsic gaussian blur filter:

Step-by-Step Code

public Bitmap blurBitmap(Context context, Bitmap source, float radius) {
    // Make sure radius is 0 < radius <= 25
    if (source == null) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

    RenderScript rs = RenderScript.create(context);

    Allocation input = Allocation.createFromBitmap(rs, source);
    Allocation output = Allocation.createFromBitmap(rs, outputBitmap);

    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    blurScript.setRadius(radius);
    blurScript.setInput(input);
    blurScript.forEach(output);

    output.copyTo(outputBitmap);

    rs.destroy();
    return outputBitmap;
}

Explanation:

  • RenderScript.create(context) initializes the computation engine.
  • ScriptIntrinsicBlur is the gaussian blur processor.
  • Blur radius must be between 0 and 25 (values outside will throw an exception).

Use the returned bitmap in an ImageView like so:

Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
Bitmap blurred = blurBitmap(this, original, 15f);
imageView.setImageBitmap(blurred);

Performance Considerations

RenderScript’s blur is significantly faster than loops or Java-only code because it uses optimized native pathways and hardware acceleration when available.

A common optimization pattern is to blur a scaled-down version of the bitmap and then scale up the result:

float scale = 0.5f;
Bitmap scaled = Bitmap.createScaledBitmap(source,
        Math.round(source.getWidth() * scale),
        Math.round(source.getHeight() * scale), false);

Bitmap blurred = blurBitmap(context, scaled, 20f);

This reduces processing cost while preserving visual smoothness.

Modern Alternatives (API 31+)

With Android 12 and later, the RenderEffect API allows view-level blurs without manual bitmap manipulation and is the recommended replacement for RenderScript blur on newer devices.

Example:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    view.setRenderEffect(
        RenderEffect.createBlurEffect(10f, 10f, Shader.TileMode.CLAMP)
    );
}

This applies a blur to the entire view content instead of a bitmap, enabling live UI blurs without copying bitmaps.

Best Practices

From production experience:

  • Use RenderScript blur for static images and offline processing.
  • Prefer RenderEffect for UI blurring (live effects).
  • Always destroy RenderScript contexts and allocations after use to free native memory.
  • For large images, consider down-sampling before blur to improve speed.
  • Cache blurred bitmaps when used repeatedly to avoid recomputation.

Common Issues and Solutions

IllegalArgumentException (radius out of range)
➡ Blur radius must be between 0 and 25 — exclusive upper bound.

Performance lag on large bitmaps
➡ Down-sample before blur. Smaller bitmaps mean faster processing.

Deprecated API complaint on modern devices
➡ RenderScript intrinsic blur is deprecated as of API 31. Use RenderEffect or third-party libraries on newer Android versions.

Summary

Creating a bitmap blur effect enhances user interfaces like background dialogs, wallpapers, or previews. You can achieve high-quality blur with RenderScript’s intrinsic gaussian blur filter, and modern Android versions offer RenderEffect for view-level blur without bitmap duplication. Choosing the right approach based on your Android version ensures both performance and maintainability.

Visited 5 times, 1 visit(s) today
Close