Android RenderScript suggest change

Blur an image

This example demonstrates how to use Renderscript API to blur an image (using Bitmap). This example uses ScriptInstrinsicBlur provided by android Renderscript API (API >= 17).

public class BlurProcessor {
    private RenderScript rs;
    private Allocation inAllocation;
    private Allocation outAllocation;
    private int width;
    private int height;

    private ScriptIntrinsicBlur blurScript;

    public BlurProcessor(RenderScript rs) { = rs;
    public void initialize(int width, int height) {
        blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setRadius(7f); // Set blur radius. 25 is max

        if (outAllocation != null) {
            outAllocation = null;
        // Bitmap must have ARGB_8888 config for this type
        Type bitmapType = new Type.Builder(rs, Element.RGBA_8888(rs))
            .setMipmaps(false) // We are using MipmapControl.MIPMAP_NONE
        // Create output allocation
        outAllocation = Allocation.createTyped(rs, bitmapType);

        // Create input allocation with same type as output allocation
        inAllocation = Allocation.createTyped(rs, bitmapType);

    public void release() {
        if (blurScript != null) {
            blurScript = null;
        if (inAllocation != null) {
            inAllocation = null;

        if (outAllocation != null) {
            outAllocation = null;

    public Bitmap process(Bitmap bitmap, boolean createNewBitmap) {
        if (bitmap.getWidth() != width || bitmap.getHeight() != height) {
            // Throw error if required
            return null;
        // Copy data from bitmap to input allocations

        // Set input for blur script

        // process and set data to the output allocation
        if (createNewBitmap) {
            Bitmap returnVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            return returnVal;

        return bitmap;

Each script has a kernel which processes the data and it is generally invoked via forEach method.

public class BlurActivity extends AppCompatActivity {
    private BlurProcessor blurProcessor;

    public void onCreate(Bundle savedInstanceState) {
        // setup layout and other stuff

        blurProcessor = new BlurProcessor(Renderscript.create(getApplicationContext()));

    private void loadImage(String path) {
        // Load image to bitmap
        Bitmap bitmap = loadBitmapFromPath(path);
        // Initialize processor for this bitmap
        blurProcessor.initialize(bitmap.getWidth(), bitmap.getHeight());
        // Blur image
        Bitmap blurImage = blurProcessor.process(bitmap, true); // Use newBitamp as false if you don't want to create a new bitmap

This concluded the example here. It is advised to do the processing in a background thread.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents