Skip to content Skip to sidebar Skip to footer

Set The Background Image In Blur Effect In Android App

Im trying to get my background image on the list view blurred, but I tried following the tutorials and it does not work. Anyone please advise, thanks. main activity.java public cl

Solution 1:

I recently came across 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;
} 

Use the above code snippet in the image view as shown below.

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

Dont forget to add below lines in build.gradle file

renderscriptTargetApi 18
renderscriptSupportModeEnabled true

Reference : http://javatechig.com/android/how-to-create-bitmap-blur-effect-in-android-using-renderscript


Post a Comment for "Set The Background Image In Blur Effect In Android App"