Blur Effect to Image View in Android using RenderScript – {Working Example}

Please follow and like us:

blur effect to imageview in android using renderscript working example

blur-effect-to-imageview-in-android-using-renderscript

Learn how to set blur effect to image view or bitmap in your android application using RenderScript with an easy explanation. you can also check our other post to set “blur effect to image view without render script“. Let’s explore to implement blur effect to image view in android suing render script.

Blur Effect to Image View in Android using RenderScript

 

Download: Blur Effect to Image View in Android using RenderScript Example

 

activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java file

package com.nkdroid.blur.effect.with.renderscript;


import android.graphics.Bitmap;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
import android.util.Log;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private ImageView bgImage;
    private SeekBar seekBarBlur;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekBarBlur= (SeekBar) findViewById(R.id.seekBar);
        bgImage= (ImageView) findViewById(R.id.imageView);


        new CountDownTimer(1000, 1000) {

            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {
                bgImage.setImageResource(R.drawable.bs);
                bgImage.setDrawingCacheEnabled(true);
                Bitmap bitmap = bgImage.getDrawingCache();
                Bitmap blurred = blurRenderScript(bitmap, 5);//second parametre is radius
                bgImage.setImageBitmap(blurred);

            }
        }.start();
        initBlurEffect();


    }


    private void initBlurEffect() {

        seekBarBlur.setMax(25);

        seekBarBlur.setKeyProgressIncrement(1);
        seekBarBlur.setProgress(5);
        seekBarBlur.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                bgImage.setImageResource(R.drawable.bs);
                bgImage.setDrawingCacheEnabled(true);
                Bitmap bitmap = bgImage.getDrawingCache();
                if (i == 0) {
                    bgImage.setImageResource(R.drawable.bs);
                } else {
                    Log.e("seek......", i + "");

                    Bitmap blurred = blurRenderScript(bitmap, i);//second parametre is radius
                    bgImage.setImageBitmap(blurred);
                    bgImage.invalidate();

                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

        try {
            smallBitmap = RGB565toARGB888(smallBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }


        Bitmap bitmap = Bitmap.createBitmap(
                smallBitmap.getWidth(), smallBitmap.getHeight(),
                Bitmap.Config.ARGB_8888);

        RenderScript renderScript = RenderScript.create(MainActivity.this);

        Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap);
        Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
                Element.U8_4(renderScript));
        blur.setInput(blurInput);
        blur.setRadius(radius); // radius must be 0 < r <= 25
        blur.forEach(blurOutput);

        blurOutput.copyTo(bitmap);
        renderScript.destroy();

        return bitmap;

    }


    private Bitmap RGB565toARGB888(Bitmap img) throws Exception {
        int numPixels = img.getWidth() * img.getHeight();
        int[] pixels = new int[numPixels];

        //Get JPEG pixels.  Each int is the color values for one pixel.
        img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());

        //Create a Bitmap of the appropriate format.
        Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);

        //Set RGB pixels.
        result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
        return result;
    }


}

AndroidMainifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nkdroid.blur.effect.with.renderscript" >

    <application
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.nkdroid.blur.effect.with.renderscript"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        renderscriptTargetApi 18
        renderscriptSupportModeEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}

 

Download: Blur Effect to Image View in Android using RenderScript Example

blur effect to imageview in android example

Hope, you like our simple Android demo of setting blur effect to image view or bitmap in android using RenderScript.

 

nirav kalola