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 <= 25privatestaticfinalfloatBLUR_RADIUS=25f;

public Bitmap blur(Bitmap image) {
if (null == image) returnnull;

BitmapoutputBitmap= Bitmap.createBitmap(image);
finalRenderScriptrenderScript= RenderScript.create(this);
AllocationtmpIn= Allocation.createFromBitmap(renderScript, image);
AllocationtmpOut= Allocation.createFromBitmap(renderScript, outputBitmap);

//Intrinsic Gausian blur filterScriptIntrinsicBlurtheIntrinsic= 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.

ImageViewimageView= (ImageView) findViewById(R.id.imageView);
Bitmapbitmap= BitmapFactory.decodeResource(getResources(),R.drawable.nature);
BitmapblurredBitmap= 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"