How To Create a Bitmap Blur Effect In Android Using RenderScript
Learn how to achieve the popular blur effect on your Android apps
Join the DZone community and get the full member experience.
Join For FreeAndroid 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 the Android Studio project add the following configurations to the project build.gradle
file.
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 to 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));
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
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);
imageView.setImageBitmap(blurredBitmap);
Result:
Published at DZone with permission of Nilanchala Panigrahy, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments